string-template-parser
String template parsing utilities.
-
parseStringTemplate
uses the default configuration (i.e. variable start is marked by${
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 that uses the supplied expressions from the configuration parameter to parse the string. -
evaluateStringTemplate
takes a string and a list of variables and one of pipe functions and returns a string where the variables are replaced with their values (transformed by the pipe functions if necessary). -
evaluateParsedString
takes a parsed string object generated by theparseStringTemplate
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 defaultparseStringTemplate
function, but one generated by passing a parameter toparseStringTemplateGenerator
.evaluateParsedString(parseStringTemplateGenerator()(input), ...args)
is equivalent toevaluateStringTemplate(input, ...args)
Usage
parseStringTemplate
; 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
; ; 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
; evaluateStringTemplate 'x ${a|upper} y', , ;// returns 'x STRING y'
evaluateParsedString
; ; evaluateParsedString parseAngularStringTemplate'x {{a|upper}} y', , ;// returns 'x STRING y'