strep
v1.0.0
Published
Tiny and fast string replacer utility.
Downloads
3
Maintainers
Readme
String Replacer
Tiny and fast string replacer utility.
Usage
const strep = require('strep'),
great = strep('{1} {0}!')
console.log(great([ 'World', 'Hello' ]))
Hello World!
Objects are supported as well:
const format = strep('{first} {last} ({nick})'),
name = {
nick: 'Johnny',
first: 'John',
last: 'Doe'
}
console.log(format(name))
John Doe (Johnny)
Custom placeholders
By default the following regexp is employed to find placeholders in a format string: /{([a-zA-Z0-9_]+)}/g
You are able to override it globally or per call to use different placeholders.
strep.pattern = /\{{\s*([a-z]+)\s*}}/g // override globally
const mustache = strep('Hello {{ name }}!'),
bash = strep('$0 is my $1', /\$([0-9]+)/) // override just for this call
console.log(bash([ 'Buggy', 'cat' ]))
console.log(mustache({ name: 'Buggy' }))
Buggy is my cat.
Hello Buggy!
Note: mind the capturing group in those regexps. Those are required to work properly.
Installation
With npm:
npm install strep
Tests & benchmarks
Run unit tests:
npm test
Run unit tests and create coverage report:
npm run cover
Run benchmark:
npm run benchmark