Skip to main content

PathPattern

The PathPattern interface is implemented by the path pattern objects that are created when using the path function.

Methods

build

Build a path.

function build(args: { [paramName: string]: any }): string;

Parameters

Accepts a single argument - an object containing the arguments for each route parameter.

The keys of the args object must match the names of the path pattern's route parameters. The values must also be of the correct type for each parameter. When a parameter accepts undefined arguments, omitting the argument is also allowed.

Return value

A path string matching the path pattern, with each of the parameters filled with a formatted version of the supplied argument.

match

Check if a path matches the pattern, and return arguments if the match is successful.

function match(path: string): { [paramName: string]: any } | undefined;

Parameters

Accepts a single argument - the path string to match against.

Return value

If the supplied path string matches the pattern, match will return an object with keys matching the names of the path pattern's route parameters. The string arguments will be parsed into values of the appropriate type for each parameter.

If the path does not match the pattern, match will return undefined.

test

Check if a path matches the pattern.

function test(path: string): boolean;

Parameters

Accepts a single argument - the path string to match against.

Return value

A boolean value indicating whether the path matched the pattern or not.

Type variables

The PathPattern type is a generic type:

PathPattern<Params extends Array<Param<string, any>>>

Params

The type of parameters contained in the path pattern.

Typically, you would not need to manually specify this type variable, as the parameter types are automatically inferred when calling the path function to create path patterns:

import { path } from "@snout/router-path";

const user = path`/user/${"id"}`; // user has a type of PathPattern<[Param<"id", string>]>