matchTemplate()function

matchTemplate(template: string, target: string, caller: AnyCaller = matchTemplate): TemplateMatches | undefined
ParamType
templatestring
The template string, e.g. :name-${country}/{city}. required
targetstring
The string containing values, e.g. Dave-UK/Manchester. required
callerAnyCaller
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-:month and 2016-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" }