A package that helps you process template strings against values
- 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
First, install the package using npm:
npm install -S templatestringparser
or
yarn add templatestringparser
Package can be used like so:
$ npm install templatestring
$ node
> console.log(require('templatestringparser')('Hi {name}, Welcome!', {name: 'Dave'}));
Hi Dave, Welcome!
var templateParser = require('templatestringparser')
var template = 'Hello {name}, welcome to my Platform!'
console.log(templateParser(template, { name: 'Dave' })) // Hello Dave, Welcome to my Platform!
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!
Jump to Configurations #1
Jump to Configurations #2
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.
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!
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!
- Fork, improve, make pull request
- raise issues for suggestions, things to fix and avenues for improvement
- Follow me on Twitter for updates: https://twitter.com/ajalaabdulsamii