Skip to content

crysalead-js/string-placeholder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

string-placeholder

Build Status

A small library that replaces placeholders from string templates.

API

Replacing some variable placeholders

template('My name is ${name} and I am ${age} years old.', {
  name: 'Bob',
  age: '65'
});
// My name is Bob and I am 65 years old.

or by using an array:

template('My name is ${0} and I am ${1} years old.', ['Bob', '65']);
// My name is Bob and I am 65 years old.

Cleaning placeholders

Using the 'clean' option cleans up undefined placeholders.

template('Choose between: ${a}, ${b} or ${c}.', { a: 'option1' }, { clean: true });
// Choose between: option1.

You can configure the regular expression matching gaps with the 'gap' option.

Custom placeholders

The placeholder delimiters can be configured through the 'before' and 'after' options like the following:

template('My name is [:name] and I am [:age] years old.', {
  name: 'Bob',
  age: '65'
}, {
  before: '[:',
  after: ']'
});
// My name is Bob and I am 65 years old.

Escape char

By default the escape char is '\\' but you can configure it using the 'espace' option.

About

Replaces placeholders into a string template

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published