string-placeholder
DefinitelyTyped icon, indicating that this package has TypeScript declarations provided by the separate @types/string-placeholder package

1.0.2 • Public • Published

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.

Readme

Keywords

none

Package Sidebar

Install

npm i string-placeholder

Weekly Downloads

3,044

Version

1.0.2

License

MIT

Unpacked Size

11.1 kB

Total Files

8

Last publish

Collaborators

  • jails