- Notifications
You must be signed in to change notification settings - Fork3
A package that helps you process template strings against values
License
jalasem/templatestringparser
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A package that helps you process template strings against values
- optional configuration ofopeningbracket andclosingbracket
- 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'}));HiDave,Welcome!
vartemplateParser=require('templatestringparser')vartemplate='Hello {name}, welcome to my Platform!'console.log(templateParser(template,{name:'Dave'}))// Hello Dave, Welcome to my Platform!
importtemplatestringparserastemplateParserfrom'templatestringparser'consttemplate='Hello {name}, welcome to my Platform!'console.log(templateParser(template,{name:'Dave'}))// Hello Dave, Welcome to my Platform!
Jump toConfigurations #1
Jump toConfigurations #2
vartsp=require('templatestringparser')vartemplate='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}.'varstrObj={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 defaulttemplatestringparser uses single curly brackets{
as openingbracket and}
as closingbracket.
To override the default behavior, pass a 3rd argument of OBJECT TYPE specifying theopeningbracket andclosingbracket
importtemplatestringparserastspfrom'templatestringparser'vartemplate='Hello ${name}, welcome to my Platform!'varstrObj={name:'Dave'}varconfig={openingbracket:'${',// some common examples '{','{{','(','<','<>'closingbracket:'}'// some common examples '}','}}',')','>','/>','</>'}console.log(tsp(template,strObj,config))// Hello Dave, Welcome to my Platform!
Thetrim
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.
importtemplatestringparserastspfrom'templatestringparser'vartemplate='Hello { name }, welcome to my { space }!'varstrObj={name:'Dave',space:'World'}varconfig={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
About
A package that helps you process template strings against values
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors3
Uh oh!
There was an error while loading.Please reload this page.