string-template-parser
String template parsing utilities.parseStringTemplate
uses the default configuration (i.e. variable
${
and variable end by }
, the escape character
is \
, a pipe is started with |
and a pipe parameter starts after
a :
, e.g. 'string ${var | pipe : parameter}'
).parseStringTemplateGenerator
returns a string parsing function
evaluateStringTemplate
takes a string and a list of variables and
evaluateParsedString
takes a parsed string object generated by the
parseStringTemplate
function and returns a concatenated string with
the variables replaced by the given values in the variable dictionary,
passed through the pipe functions if necessary. This function is useful
when not using the default parseStringTemplate
function, but one
generated by passing a parameter to parseStringTemplateGenerator
.
evaluateParsedString(parseStringTemplateGenerator()(input), ...args)
is equivalent to evaluateStringTemplate(input, ...args)
Usage
parseStringTemplate
import { parseStringTemplate } from 'string-template-parser';
parseStringTemplate('a ${v1|p:param} b ${v2} c');
/* returns:
{
literals: ['a ', ' b ', ' c'],
variables: [
{ name: 'v1', pipes: [{ name: 'p', parameters: ['param'] }],
{ name: 'v2', pipes: []}
]
}
*/
parseStringTemplateGenerator
import { parseStringTemplateGenerator } from 'string-template-parser';
const parseAngularStringTemplate = parseStringTemplateGenerator({
VARIABLE_START: /^\{\{\s*/,
VARIABLE_END: /^\s*\}\}/
});
parseAngularStringTemplate('a {{v1|p:param}} b {{v2}} c');
/* returns:
{
literals: ['a ', ' b ', ' c'],
variables: [
{ name: 'v1', pipes: [{ name: 'p', parameters: ['param'] }],
{ name: 'v2', pipes: []}
]
}
*/
evaluateStringTemplate
import { evaluateStringTemplate } from 'string-template-parser';
evaluateStringTemplate(
'x ${a|upper} y',
{a: 'string'},
{upper: value => value.toUpperCase()}
);
// returns 'x STRING y'
evaluateParsedString
import {
parseStringTemplateGenerator,
evaluateParsedString
} from 'string-template-parser';
const parseAngularStringTemplate = parseStringTemplateGenerator({
VARIABLE_START: /^\{\{\s*/,
VARIABLE_END: /^\s*\}\}/
});
evaluateParsedString(
parseAngularStringTemplate('x {{a|upper}} y'),
{a: 'string'},
{upper: value => value.toUpperCase()}
);
// returns 'x STRING y'