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

eval-interpolation-sandbox

v0.3.0

Published

Javascript string interpolation with sandboxed JS code

Downloads

38

Readme

node-eval-interpolation-sandbox

Travis npm

Interpolate strings with sandboxed JS code

Example

parseTemplate = require('eval-interpolation-sandbox')

parseTemplate( "`name`", {name: "John"} )
// 'John'

parseTemplate( "`name.join(' ')`", {name: ["John", "Locke"]} )
// 'John Locke'

parseTemplate( "`++i`; `++i`; `++i`", {i:0} )
// '1; 2; 3'

context = { i:0, names: ["John", "Paul"] }
parseTemplate( "`++i` - `names.shift()`", context )
parseTemplate( "`++i` - `names.shift()`", context )
// '1 - John'
// '2 - Paul'

Install

npm install eval-interpolation-sandbox

API

To get the synchronous parser function with default options, just import the package root:

parseTemplate = require('eval-interpolation-sandbox')

result = parseTemplate( "`name.join(\" \")`", {name: ["Paul", "Jefferson"]} )  // 'Paul Jefferson'

To get the asynchronous parser, or set custom defalut options

ParserSync = parseTemplate.ParserSync
ParserAsync = parseTemplate.ParserAsync

parserAsync = new ParserAsync( {delimiter: ["\#{", "}"]} )
parserAsync.parseTemplate( "#{value}", {value: 42}, function(err,res){ console.log(res) } ) // '42'

ParserAsync([opts])

opts is an object with the default values of the same options passed to parseTemplate

parseTemplate( template, context, [opts,] callback )

template is the string containing the tokens that will be evaluated.

context is the object containing the values that will be available for the tokens.

callback(err,res)

opts is an object which can receive the following options:

  • sandbox (default: false)

    Clones the context before parsing each token, thereby isolating the context for each token.

  • ignore (default: false)

    Ignores errors while parsing the template. The callback will return a list of errors, and the formatted string with the placeholders where the errors occurred replaced by blank space.

  • delimiter (default: "`")

    Defines the symbol that delimits both sides of the placeholders within the string. Accepts a string, which will define both delimiters, or an array with two strings, which will define the left and right delimiters, respectively.

    parseTemplate( "\'value\'", {value: 42}, { delimiter: "\'" } )
    parseTemplate( "\#{value}", {value: 42}, { delimiter: ["\#{", "}"] } )

ParserSync([opts])

The same as ParserAsync, with parseTemplate being synchronous and not accepting the ignore option.

parseTemplate( template, context, [opts] )

opts: {sandbox=0, delimiter="`"}