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

stringd

v2.2.0

Published

A string variable parser for JavaScript

Downloads

5,742

Readme

STRING-D

NodeJS String Variable Parser

NPM Version NPM Downloads

Create parsable strings using template formats by argument mapping.

Similar to printf but supports nesting and exclusive recursions

NPM

Installing

Via NPM:

npm install stringd

Usage

import stringd from 'stringd';

stringd('Hello :{name}', {name: 'World'});
// Hello World

stringd(':{last}, :{first} :{last}', {last: 'Doe', first: 'John'});
// Doe, John Doe

API

stringd(template, object[, ignore])

Parse the tmp string, replacing variable sections with flexibly defined values within the props object String tags within the ignore array are skipped in the process. (used specifically to avoid repetition/recursion)

Features

Multi parse methods

Whichever is more comfortable for you would work just fine

stringd('Hello :{name}', {name: 'World'});  // Hello World
stringd('Hello %{name}', {name: 'World'});  // Hello World
stringd('Hello ${name}', {name: 'World'});  // Hello World
stringd('Hello :{name%}', {name: 'World'}); // Hello World
stringd('Hello %{name%}', {name: 'World'}); // Hello World
stringd('Hello ${name%}', {name: 'World'}); // Hello World

Nesting

assert.equal('John Davis', stringd(':{name}', {
  name: ':{first} :{last}',
  last: 'Davis',
  first: 'John',
}));

Extended, Variable Passing

assert.equal(
  'Age Difference = [32 + 25]  = [57]',
  stringd(
    stringd('Age Difference = [:{age1} + :{age2}]  = [:{add(:{age1}, :{age2})}]', {
      age1: 32,
      age2: 25,
    }),
    {add: (_, data) => data.args.reduce((a, v) => a + +v.trim(), 0)},
  ),
);
assert.equal(
  'Hello, Guys; Hello, World. How are you doing?',
  stringd(':{greet(Guys, post=How are you doing?, World)}', {
    greet: (_, names) =>
      names.args
        .map(name => `Hello, ${name.trim()}`)
        .join('; ')
        .concat('. ', names.matched.post),
  }),
);

Functional Evaluation

assert.equal(
  'Hello John Davis, you have contributed $585 in the span of 13 months',
  stringd(':{name:parsed}', {
    name: ':{first} :{last}',
    last: 'Davis',
    first: 'John',
    greeting: 'Hello :{name}',
    months: 13,
    duration: ':{months} months',
    contribution: 45,
    // Functions get the entire template object as an argument
    // so you can do advanced processing
    cash: ({contribution, months}) => contribution * months,
    'name:parsed': `:{greeting}, you have contributed $:{cash} in the span of :{duration}`,
  })
);

Forward Padding, Space

assert.equal('   10', stringd(':5{val}', {val: 10}));

End Padding, Space

assert.equal('10   ', stringd(':-5{val}', {val: 10}));

Forward Padding, Zero

assert.equal('00010', stringd(':05{val}', {val: 10}));

End Padding, Zero

assert.equal('10000', stringd(':-05{val}', {val: 10}));

Complex Nesting with exclusive recursion

Recursive nesting is unparsed at the second level, otherwise, it continues until its done parsing the entire string

assert.equal(
  'James:{name} :{name} :{data} :{data} :{all} :{name} :{data}Jack James:{data}Jack James:{data}Jack :{misc} :{data} :{all} James:{data}Jack :{misc}',
  stringd(':{name} :{misc}', {
    name: ':{first}:{data}:{last}',
    first: 'James',
    last: 'Jack',
    misc: ':{data}',
    data: ':{name} :{all}',
    all: ':{name} :{misc} :{data} :{all} :{name} :{misc}',
  })
);

Iterative processing

assert.equal('      2364', stringd(stringd('::{pad}{price}', {pad: 10}), {price: 2364}))

Precedence

stringd replaces keys in order precedence. Keys that appear first are replaced first

stringd( // str key appears first
  stringd(':{tro:{str}}', {str: 'y', 'tro:{str}': ':{tron}'}),
  { tron: 'Movie', troy: 'Dude' }
); // Dude

stringd( // str key appears later
  stringd(':{tro:{str}}', {'tro:{str}': ':{tron}', str: 'y'}),
  { tron: 'Movie', troy: 'Dude' }
); // Movie

Development

Feel free to clone, use in adherance to the license. Pull requests are very much welcome.

git clone https://github.com/miraclx/stringd.git
cd stringd
npm install
# hack on code
npm test

Testing

Tests are executed with Jest. To use it, simple run npm install, it will install Jest and its dependencies in your project's node_modules directory and finally npm test.

To run the tests:

npm install
npm test

License

Apache 2.0 © Miraculous Owonubi (@miraclx) <[email protected]>