string-interp
v0.3.6
Published
Shell-style string interpolation
Downloads
1,435
Readme
string-interp
A small library to do shell-style string interpolation, with extensions to make it useful to prepare messages shown to the user.
This is the library used to replace placeholders in Thingpedia formatted strings.
Usage
const interp = require('string-interp');
// returns "foo bar baz"
interp('foo $p1 ${p2}', { p1: 'bar', p2: 'baz' }, { locale: 'en-US' });
Syntax
The syntax recognizes parameters preceded by a dollar sign $
.
In the short form, the parameter name follows the dollar sign immediately. In this case, all identifier-like letters following the dollar sign are considered part of the parameter name. Example:
interp('$a $a$aa$a,a', { a: '1', aa: '2' }) === '1 121,a'
In the long form, the parameter name is wrapped in {}
. Use this form if the parameter is immediately
followed by another letter or number. Example:
interp('${a} ${a}a ${aa}', { a: '1', aa: '2' }) === '1 1a 2'
In the long form the parameter can be followed by an option, separated with :
. Options affect how
the parameter value is transformed into a string. Example:
interp('${a} ${a:url}', { a: '/' }) === '/ %2F'
Available options
General options:
url
: URL-encode the value with encodeURIComponent
Options for Date
:
iso-date
: format aDate
object as an ISO 8661 string.