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

templatestring

v5.0.1

Published

a string template parser

Downloads

522

Readme

Template String

Parse string templates. By default it uses the ${} tag. This provides functionality similar to ES6 template strings with standard js strings. This can be particularly useful when you don't have direct control of the string. For example, reading from a file or database where you can't cast to a string template. The tag marker for templates can be configured using an options object specifying a start and end pattern. See Advanced for examples. The module can also be found here on npm

Example

var templatestring = require('templatestring');
templatestring("hi ${name}, I'm ${package.name}", {name: "user", package: {name: "parser"}}); 
// expect "hi user, I'm parser".

Caching

New!

Templatestring will perform greedy caching on any parsed templates. Any subsequent runs of the template are 10x faster than the first run. Set the cache length to any integer to control maximum memory consumption, if needed.

var templatestring = require('templatestring').cache_length(10000);
templatestring("hi ${name}, I'm ${package.name}", {name: "user", package: {name: "parser"}}); 
// expect "hi user, I'm parser".

#Details Multi-level objects work as template names.

var templatestring = require('templatestring');
templatestring("${foo.bar}", {foo: {bar: "hi"}}); 
// expect "hi"

If you call a property that doesn't exist, you'll get undefined

var templatestring = require('templatestring');
templatestring("${foo.bar}", {foo:"bar"}); 
// expect "undefined"

If you construct a call to the object that would cause an error, expect an error.

var templatestring = require('templatestring');
templatestring("${foo.bar}", {}); 
// expect Error cannot read property "bar" of undefined.

It even works with brace notation

var templatestring = require('templatestring');
templatestring("${['some property']} or ${[0]}", {'some property': "this", 0: "that"}); 
// expect "this or that"

#Advanced You can also define your own matching pattern by passing an options argument.
By default the pattern start:\\$\\{ and end \\} is used which matches start: ${ end: }.

You could, for example, also use start: \\{\\{ and end \\}\\} for start: {{ end: }}.

The extra back slashes are to escape special characters in regex.
The first is to escape the escape character when converting to string. The second is to escape the special regex character when converting to regex.

var templatestring = require('templatestring');
templatestring("I can use {{template}}", {template: "handlebars!"}, {start: "\\{\\{", end: "\\}\\}"}); 
// expect "I can use handlebars!"

Another example: <%%> templates.

var templatestring = require('templatestring');
templatestring("I can use <%template%>", {template: "<%-style templates!"}, {start: "<%", end: "%>"}); 
// expect "I can use <%-style templates!"

Another example: python templates.

var templatestring = require('templatestring');
templatestring("I {0} {1} {2} brackets", ["can", "use", "python" ], {start: "\\{", end: "\\}"}); 
// expect "I can use python brackets"

Safely encode results with encodeURIComponent.

var templatestring = require('templatestring');
templatestring("encode ${specialChars}", {specialChars: '%$` '}, {encode: true}); 
// expect "encode %25%24%60%20"

Safely encode results for postgres.

var templatestring = require('templatestring');
templatestring("encode ${specialChars}", {specialChars: '%$`\'\' '}, {encode: 'postgres'}); 
// expect "encode %25%24%60\'\'\'\'%20"

#Caveat Template parser does not 'eval()' anything and is restricted to checking properties of the namespace of the data object passed to it. As such, it should be safe to use with un-trusted inputs.
However, as a consequence, property names with ['s or .'s are not supported

var templatestring = require('templatestring');
var str = "This is ${.could}. This is also ${[work}.";
var obj = {'.could': 'defined', '[work': 'defined'};

templatestring(str, obj); 
// expect 'This is undefined. This is also undefined.'