getPlaceholders()function
Get list of placeholders named in a template string.
getPlaceholders(template: string): TemplatePlaceholders
Match placeholders out of a string, or render values into one. Useful when a full regular expression is overkill and you just want to extract named segments from a path or pattern.
Things to know:
:name, {name}, {{name}}, ${name}, [name], and * (anonymous, zero-indexed).**, :name*, {...name}) allow empty values and, in path mode, can span / separators.matchPathTemplate() and renderPathTemplate() are path-aware siblings of matchTemplate() and renderTemplate(). Non-catchall placeholders in path templates cannot span / segments.import { matchTemplate, matchPathTemplate, matchTemplates } from "shelving/util";
matchTemplate("places/{country}/{city}", "places/france/paris");
// { country: "france", city: "paris" }
matchTemplate(":year-:month", "2024-03");
// { year: "2024", month: "03" }
matchTemplate("*-*-*", "A-B-C");
// { "0": "A", "1": "B", "2": "C" }
matchTemplate("no-match/:x", "something-else"); // undefined
// Try several templates, return first match:
matchTemplates([":id@:domain", "places/:country/:city"], "places/france/paris");
// { country: "france", city: "paris" }
// Path-aware matching — non-catchall placeholder can't span `/`:
matchPathTemplate("/files/:name", "/files/report.pdf");
// { name: "report.pdf" }
matchPathTemplate("/files/:name", "/files/a/b"); // undefinedimport { renderTemplate, renderPathTemplate } from "shelving/util";
renderTemplate("blogs-:category-:slug", { category: "cheeses", slug: "stilton" });
// "blogs-cheeses-stilton"
renderTemplate("blogs-:category-:slug", "placeholder");
// "blogs-placeholder-placeholder"
renderTemplate("blogs-:category-:slug", p => p.toUpperCase());
// "blogs-CATEGORY-SLUG"
renderTemplate("*-*-*", ["A", "B", "C"]);
// "A-B-C"Missing values throw a RequiredError:
renderTemplate("{name}-{date}", { name: "Dave" });
// throws RequiredError — "date" placeholder not foundimport { getPlaceholders } from "shelving/util";
getPlaceholders("{username}@{domain}"); // ["username", "domain"]
getPlaceholders(":country/:city"); // ["country", "city"]
getPlaceholders("*-*"); // ["0", "1"]Get list of placeholders named in a template string.
getPlaceholders(template: string): TemplatePlaceholders
Match a template against a target string with no separator semantics.
matchTemplate(template: string, target: string, caller: AnyCaller = matchTemplate): TemplateMatches | undefined
Match a path-shaped template against a target path.
matchPathTemplate(template: AbsolutePath, target: AbsolutePath, caller: AnyCaller = matchPathTemplate): TemplateMatches | undefined
Match multiple templates against a target string and return the first match (no separator semantics).
matchTemplates(templates: Iterable<string> & NotString, target: string): TemplateMatches | undefined
Match multiple path-shaped templates against a target path and return the first match.
matchPathTemplates(templates: Iterable<AbsolutePath> & NotString, target: AbsolutePath): TemplateMatches | undefined
Turn ":year-:month" and { year: "2016"... } etc into "2016-06..." etc.
renderTemplate(template: string, values: TemplateValues, caller: AnyCaller = renderTemplate): string
Render a path-shaped template. Behaviourally identical to renderTemplate() — substitution doesn't need separator awareness — but provided as a sibling to matchPathTemplate() so callers can pair them.
renderPathTemplate(template: AbsolutePath, values: TemplateValues, caller: AnyCaller = renderPathTemplate): AbsolutePath
Things that can be converted to the value for a named placeholder.
PossibleString | ImmutableArray<unknown> | ImmutableDictionary<unknown> | ((placeholder: string) => string)
The output of matching a template is a dictionary in { myPlaceholder: "value" } format.
ImmutableDictionary<string>
List of {placeholders} found in a template string.
ImmutableArray<string>