string-template-parser
Parsing & evaluating utilities for string templates
Last updated 3 years ago by souldreamer .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install string-template-parser 
SYNC missed versions from official npm registry.

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 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'

Current Tags

  • 1.2.6                                ...           latest (3 years ago)

15 Versions

  • 1.2.6                                ...           3 years ago
  • 1.2.5                                ...           3 years ago
  • 1.2.4                                ...           3 years ago
  • 1.2.3                                ...           3 years ago
  • 1.2.2                                ...           3 years ago
  • 1.2.1                                ...           3 years ago
  • 1.2.0                                ...           3 years ago
  • 1.1.0                                ...           3 years ago
  • 1.0.0-rc.1                                ...           4 years ago
  • 1.0.0-rc.0                                ...           4 years ago
  • 1.0.0-alpha.5                                ...           4 years ago
  • 1.0.0-alpha.4                                ...           4 years ago
  • 1.0.0-alpha.3                                ...           4 years ago
  • 1.0.0-alpha.2                                ...           4 years ago
  • 1.0.0-alpha.1                                ...           4 years ago
Maintainers (1)
Downloads
Today 1
This Week 2
This Month 15
Last Day 1
Last Week 2
Last Month 45
Dependencies (0)
None
Dev Dependencies (12)

Copyright 2014 - 2017 © taobao.org |