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

ez-string

v1.1.10

Published

A string template renderer for JavaScript without memory leaks.

Downloads

9

Readme

ez-string

Travis build status badge Code coverage status badge ESDoc coverage badge Number of tests Codacy code quality badge

A string template renderer for JavaScript without memory leaks. It supports referencing variables by position, by name. One can access properties and array elements.

API reference

Example

const render = require('ez-string').render;
let book;

// Format using variable's position
book = render('One of my favorite books is "{0}" by {1}.', 'The Name of the Wind', 'Patrick Rothfuss');
// book = 'One of my favorite books is "The Name of the Wind" by Patrick Rothfuss.'

// Format using variable's name
// Variable names must use A-Za-z0-9$_
book = render('One of my favorite books is "{title}" by {author}.',
    { title: 'The Name of the Wind', author: 'Patrick Rothfuss'});
// book = 'One of my favorite books is "The Name of the Wind" by Patrick Rothfuss.'

// Curly braces are escaped by using double braces
let example;
example = render('{{title}}');
// example = '{title}'

// One can use array indices
book = render('"{arr[0]}" was first published in {arr[1]}.',
    { arr: ['The Hobbit', 1937]});
// book = '"The Hobbit" was first published in 1937.'

// One can use object properties.
// Properties with names consisting of A-Za-z0-9$_ can be accessed using property notation
// Properties with other names can be accessed using index notation
book = render('"{book.title}" was written by {book.author["first name"]} {book.author["last name"]}. It was published in {book.year}.', { 
        book: {
            title: 'Marina',
            year : 1999,
            author: {
                'first name': 'Carlos',
                'last name': 'Zafon'
            }
        }
    });
// book = '"Marina" was written by Carlos Zafon. It was published in 1999.'

// If a property name contains a quote character or backslash, they need to be escaped.
// Quotes are prepended by a backslash
// Backslashes need to be doubled
example = render('{data["\\\\"]}', {
    data: {'\\': 'backslash'}
});
// example = 'backslash'
example = render('{data["\\""]}', {
    data: {'"': 'quote'}
});
// example = 'quote'

Installation

Using NodeJS:

$ npm i --save ez-string
const render = require('ez-string').render;
let book;

// Format using variable's position
book = render('One of the best books by {author} is "{title}".', {
    author: 'Stephen King',
    title: '11/22/63'
});
// book = 'One of the best books by Stephen King is "11/22/63".'

In a browser:

<!-- Load library which is UMD packed -->
<script src="ez-string.js"></script>

<script>
    let book;

    // Format using variable's position
    book = render('One of the best books by {author} is "{title}".', {
        author: 'Stephen King',
        title: '11/22/63'
    });
    // book = 'One of the best books by Stephen King is "11/22/63".'
</script>

Why ez-string

The most important reason is that this library doesn't leak memory.

Many of the existing template renderers perform a two-step process. They compile a string template into a JavaScript function and then execute it while passing context data. However, most users of such libraries rarely cache the resulting compiled function. Instead they may compile the same template again and again. Due to inticacies of NodeJS memory garbage collection. Such pattern usually results in a memory leak, as described by Meteor developers.

Library documentation is here.