Skip to content

Likstern/placeholder-substitutor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

placeholder-substitutor

A small library for substitution placeholders in string for Node.js

API

Replacing variable placeholders

substitute('My name is ${name} and I am ${age} years old.', {
  name: 'Maxsim',
  age: '25',
});
// My name is Maxsim and I am 25 years old.

Custom placeholders

The placeholder delimiters can be configured through the 'prefix' and 'suffix' options:

substitute('%a %b %a %a', (string, { a: 1, b: 2 }, { prefix: '%', suffix: '' });
// 1 2 1 1

Using Interpolation

Use string lookups like: $.*:

substitute('Little ${adjective: pigs}! Little ${adjective: pigs}!', {
  adjective: (match: string) => match.toLocaleUpperCase(),
});
// Little PIGS! Little PIGS!

Escape char

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

substitute('${a} ${b} ##${a} ${a}';
    const expected = '1 2 ${a} 1';
    const result = strSb(string, { a: 1, b: 2 }, { escape: '##' });

About

Substitution placeholders in string for Node.js

Resources

License

Stars

Watchers

Forks

Packages

No packages published