string-template-parser
Advanced tools
Comparing version
{ | ||
"name": "string-template-parser", | ||
"version": "1.0.0-rc.0", | ||
"version": "1.0.0-rc.1", | ||
"description": "Parsing & evaluating utilities for string templates", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/souldreamer/string-template-parser.git", |
@@ -5,3 +5,3 @@ # string-template-parser | ||
`parseStringTemplate` uses the default configuration (i.e. variable | ||
- `parseStringTemplate` uses the default configuration (i.e. variable | ||
start is marked by `${` and variable end by `}`, the escape character | ||
@@ -11,6 +11,20 @@ is ` \ `, a pipe is started with `|` and a pipe parameter starts after | ||
`parseStringTemplateGenerator` returns a string parsing function | ||
- `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 | ||
@@ -39,5 +53,9 @@ | ||
import { parseStringTemplateGenerator } from 'string-template-parser'; | ||
const myParseStringTemplate = parseStringTemplateGenerator({VARIABLE_START: /^\{\{/, VARIABLE_END: /^\}\}/}); | ||
myParseStringTemplate('a {{v1|p:param}} b {{v2}} c'); | ||
const parseAngularStringTemplate = parseStringTemplateGenerator({ | ||
VARIABLE_START: /^\{\{\s*/, | ||
VARIABLE_END: /^\s*\}\}/ | ||
}); | ||
parseAngularStringTemplate('a {{v1|p:param}} b {{v2}} c'); | ||
/* returns: | ||
@@ -52,2 +70,36 @@ { | ||
*/ | ||
``` | ||
``` | ||
#### `evaluateStringTemplate` | ||
```typescript | ||
import { evaluateStringTemplate } from 'string-template-parser'; | ||
evaluateStringTemplate( | ||
'x ${a|upper} y', | ||
{a: 'string'}, | ||
{upper: value => value.toUpperCase()} | ||
); | ||
// returns 'x STRING y' | ||
``` | ||
#### `evaluateParsedString` | ||
```typescript | ||
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' | ||
``` |
98040
1.51%102
108.16%