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.
const interp = require('string-interp');
// returns "foo bar baz"
interp('foo $p1 ${p2}', { p1: 'bar', p2: 'baz' }, { locale: 'en-US' });
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'
General options:
url
: URL-encode the value with encodeURIComponent
Options for Date
:
iso-date
: format aDate
object as an ISO 8661 string.