npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sjtc

v0.0.7

Published

a simple javascript template compiler

Downloads

23

Readme

sjtc

sjtc is a simple javascript template compiler based on regexp. it can compile javascript template to be readability javascript function. it support the following features:

  • embed javascript code : <%...%>
  • insert variant: <%=...%>
  • escape charators: <%% equals <% and %%> equals %>
  • include file:
  • include file once:
  • support heredoc in embed code in the from "@(\w+) [\S\s]*? \1\b", for example:
<%
function render_user(u){
    return @eof  // you can use any words after @
        <li class="list-item user" data-order="#{u.order}">
            <span class="user-name">#{u.name}</span>
            <span class="user-age" >#{u.age}</span>
            <span class="user-gender">#{u.gender}</span>
        </li>
    eof.trim();
}
%>

the following is an example to explain how to use it:

the contents from render-user.html

<%
function render_user(u){
    return @eof 
        <li class="list-item user" data-order="#{u.order}">
            <span class="user-name">#{u.name}</span>
            <span class="user-age" >#{u.age}</span>
            <span class="user-gender">#{u.gender}</span>
        </li>
    eof.trim();
}
%>

the contents from tmpl.html

<!--#include_once file="render-user.html"-->

<%if(obj.users && obj.users.length > 0){%>
    <ul class="user-list">
        <% obj.users.forEach(function(u){ %>
            <%= render_user(u) %>
        <% }); %>
    </ul>
<%}else{%>
    <div class="no-result">
        <p>no user record!</p>
    </div>
<%}%>

the code used to compile the template

var fs = require('fs');
var sjtc = require('sjtc');
var file = __dirname + "/tmpl.html";

var tmplCode = sjtc.parse(file);
var tmplFn = new Function('return ' + tmplCode)();

var users  = [
    { order: 1,  name: 'user-1',  age: 24, gender: 'f'  },
    { order: 2,  name: 'user-2',  age: 24, gender: 'fm' },
    { order: 3,  name: 'user-3',  age: 23, gender: 'f'  },
    { order: 5,  name: 'user-4',  age: 24, gender: 'fm' },
    { order: 6,  name: 'user-5',  age: 24, gender: 'f'  },
    { order: 7,  name: 'user-6',  age: 34, gender: 'fm' },
    { order: 8,  name: 'user-7',  age: 32, gender: 'f'  },
    { order: 9,  name: 'user-8',  age: 52, gender: 'f'  },
    { order: 10, name: 'user-9',  age: 57, gender: 'f'  },
    { order: 11, name: 'user-10', age: 18, gender: 'f'  }
];

console.log( '\n----------generate code----------\n\n%s', tmplCode );
console.log( '\n----------generate html----------\n\n%s', tmplFn({ users: users }) );

the result code that generate by stjc

function (obj) {
    var __bf = [];

    with(obj){
        "use strict";

        function render_user(u){
            return (""
                + "<li class=\"list-item user\" data-order=\"" + u.order + "\">"
                +     "<span class=\"user-name\">" + u.name + "</span>"
                +     "<span class=\"user-age\" >" + u.age + "</span>"
                +     "<span class=\"user-gender\">" + u.gender + "</span>"
                + "</li>"
            ).trim();
        }

        if(obj.users && obj.users.length > 0){
            __bf.push("<ul class=\"user-list\">");

            obj.users.forEach(function(u){ 
                __bf.push(( render_user(u) ));

            }); 

            __bf.push("</ul>");

        }else{

            __bf.push(""
                + "<div class=\"no-result\">"
                +     "<p>no user record!</p>"
                + "</div>"
            );

        }

    }

    return __bf.join("");
}

while you can config the generate code style through the following config tiems:

  • func_name        default ''

  • extra_space       default 0

  • func_arg_name      default obj

  • input_tab_space     default 4

  • output_tab_space    default 4

  • output_buff_name    default __bf

  • always_wrap_insert   default false

  • first_line_no_space  default false