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

templatestringparser

v1.1.0

Published

Process template strings

Downloads

10

Readme

templatestringparser

A package that helps you process template strings against values

Features

  • optional configuration of openingbracket and closingbracket
  • supports single and multiple brackets like {name} {{ name }} ${name} $(name) <name> <name/> <>name</> etc
  • auto trim excess white space between key value and brackets
  • parse deeply nested values e.g. {profile.name} {address.street} etc

Usage

First, install the package using npm:

npm install -S templatestringparser

or

yarn add templatestringparser

Package can be used like so:

REPL

$ npm install templatestring
$ node
> console.log(require('templatestringparser')('Hi {name}, Welcome!', {name: 'Dave'}));
Hi Dave, Welcome!

es5

var templateParser = require('templatestringparser')

var template = 'Hello {name}, welcome to my Platform!'
console.log(templateParser(template, { name: 'Dave' })) // Hello Dave, Welcome to my Platform!

es6 and later

import templatestringparser as templateParser from 'templatestringparser'

const template = 'Hello {name}, welcome to my Platform!'
console.log(templateParser(template, { name: 'Dave' })) // Hello Dave, Welcome to my Platform!

Features in Detail

openingbracket and closingbracket

Jump to Configurations #1

auto trim

Jump to Configurations #2

parse deeply nested values

var tsp = require('templatestringparser')

var template = 'Hello {profile.name}, welcome to my {profile.space}!'
template += ' I live at No.{profile.address.street.number} {profile.address.street.name},'
template += ' {profile.address.city}.'

var strObj = {
  profile: {
    name: 'Dave',
    space: 'World',
    address: {
      street: {
        number: 1,
        name: 'strictly'
      },
      city: 'boston'
    }
  }
}

console.log(tsp(template, strObj)) // Hello Dave, welcome to my World! I live at No.1 strictly, boston.

Configurations

1 openingbracket and closingbracket

By default templatestringparser uses single curly brackets { as openingbracket and } as closingbracket.

To override the default behavior, pass a 3rd argument of OBJECT TYPE specifying the openingbracket and closingbracket

import templatestringparser as tsp from 'templatestringparser'

var template = 'Hello ${name}, welcome to my Platform!'

var strObj = {
  name: 'Dave'
}

var config = {
  openingbracket: '${', // some common examples '{','{{','(','<','<>'
  closingbracket: '}' // some common examples '}','}}',')','>','/>','</>'
}

console.log(tsp(template, strObj, config)) // Hello Dave, Welcome to my Platform!

2 trim

The trim configuration will trim excess white space between key value and brackets. This is enabled by default. Use {trim: false} in configuration to disable auto trimming.

import templatestringparser as tsp from 'templatestringparser'

var template = 'Hello {    name  }, welcome to my { space }!'

var strObj = {
  name: 'Dave',
  space: 'World'
}

var config = {
  trim: true
}

console.log(tsp(template, strObj, config)) // Hello Dave, Welcome to my World!

Keeping up to date