multi-path-matcher
Finds and decodes best matching path in a set of routes
usage
With params
import { compile, matcher } from "multi-path-matcher";
const routes = [
{ path: "/a/b/c" },
{ path: "/a/b" },
{ path: "/d/:att1/e/:att2" },
{ path: "/d/:att1/e" },
{ path: "/" }
];
const compiled = compile(routes);
matcher(compiled "/a");
matcher(compiled, "/a/b");
matcher(compiled, "/a/b/c");
matcher(compiled, "/d/value1/e");
matcher(compiled, "/d/value1/e/value2?sort=asc");
matcher(compiled, "/");
With wildcards
import { compile, matcher } from "multi-path-matcher";
const routes = [
{ path: "/" },
{ path: "/*" },
{ path: "/about" },
{ path: "/login" }
];
const compiled = compile(routes);
matcher(compiled, "/");
matcher(compiled, "/index.html");
matcher(compiled, "/about");
matcher(compiled, "/login?param=1");
API
Table of Contents
CompiledRoute
Result of a path compilation
priorities for each path component
Type: Object
Properties
path
string of the routeregex
RegExp for later checking and params extractionkeys
string? all keys found in the routepriority
number order in which to check
Route
One single route
Type: Object
Properties
Match
Result of a match
Type: Object
Properties
route
Route? as given to the compiler, undefined if no matching route was foundparams
Object extracted from the path
PLAIN
Prioritiy for a plain path component
Type: number
WILDCARD
Prioritiy for a path component with a wildcard '*'
Type: number
PARAM
Prioritiy for a parameter path component
Type: number
compile
Compile a set of routes.
All properties of the original routes are preserved
Parameters
Returns Array<CompiledRoute>
pathToRegexp
Generate regex with priority.
Parameters
Returns CompiledRoute
matcher
Find best match for a given path.
Decodes params into an object.
Parameters
Returns Match match
install
With npm do:
npm install multi-path-matcher
license
BSD-2-Clause