matchTemplate()function
matchTemplate(template: string, target: string, caller: AnyCaller = matchTemplate): TemplateMatches | undefined
| Param | Type | |
|---|---|---|
template | string | The template string, e.g. :name-${country}/{city}. required |
target | string | The string containing values, e.g. Dave-UK/Manchester. required |
caller | AnyCaller | Function to attribute a thrown error to (defaults to matchTemplate itself). Defaults to matchTemplate |
| Return | |
|---|---|
TemplateMatches | undefined | An object containing values (e.g. { name: "Dave", country: "UK", city: "Manchester" }), or undefined if no match. |
| Throws | |
|---|---|
ValueError | If two template placeholders are not separated by at least one character. |
Match a template against a target string with no separator semantics.
- Turn
:year-:monthand2016-06... etc into{ year: "2016"... }. - Non-catchall placeholders match non-empty values; catchall placeholders (
**,:name*,{...name}, etc.) allow empty values.
Examples
matchTemplate(":name-${country}/{city}", "Dave-UK/Manchester") // { name: "Dave", country: "UK", city: "Manchester" }