Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A package that helps you process template strings against values

License

NotificationsYou must be signed in to change notification settings

jalasem/templatestringparser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A package that helps you process template strings against values

Features

  • 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

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'}));HiDave,Welcome!

es5

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

es6 and later

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

Features in Detail

openingbracket andclosingbracket

Jump toConfigurations #1

autotrim

Jump toConfigurations #2

parse deeply nested values

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.

Configurations

1 openingbracket and closingbracket

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!

2 trim

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!

Contributing

  • Fork, improve, make pull request
  • raise issues for suggestions, things to fix and avenues for improvement

Keeping up to date

About

A package that helps you process template strings against values

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors3

  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp