multi-path-matcher

finds and decodes best matching path in a set of routes

Usage no npm install needed!

<script type="module">
  import multiPathMatcher from 'https://cdn.skypack.dev/multi-path-matcher';
</script>

README

npm License minified size downloads GitHub Issues Build Status Styled with prettier Commitizen friendly Known Vulnerabilities Coverage Status

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");                   // undefined
matcher(compiled, "/a/b");                // routes[1]
matcher(compiled, "/a/b/c");              // routes[0]
matcher(compiled, "/d/value1/e");                 // routes[3] { att1: "value1" }
matcher(compiled, "/d/value1/e/value2?sort=asc"); // routes[2] { att1: "value1", att2: "value2" }
matcher(compiled, "/");                           // routes[4]

With wildcards

import { compile, matcher } from "multi-path-matcher";

const routes = [
  { path: "/" },
  { path: "/*" },
  { path: "/about" },
  { path: "/login" }
];

const compiled = compile(routes);

matcher(compiled, "/");                   // routes[0]
matcher(compiled, "/index.html");         // routes[1]
matcher(compiled, "/about");              // routes[2]
matcher(compiled, "/login?param=1");      // routes[3]

API

Table of Contents

CompiledRoutes

Result of the routes compilation

Type: Object

Properties

  • priority number higher number reflect more precise matches
  • keys Array<string> param names extractable from route
  • regex RegEx

Route

One single route

Type: Object

Properties

Match

Result of a match

Type: Object

Properties

  • route Array<Route> as given to the compiler, undefined if no matching route found
  • params Object extracted from the path

PLAIN

Prioritiy for a plain path component

Type: number

MATCH

Prioritiy for a path component with matching

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 CompiledRoutes

CompiledRoute

Result of a path compilation priorities for each path component

Type: Object

Properties

  • regex RegExp for later checking and params extration
  • keys Array<string> all keys found in the route
  • priority number order in which to check

pathToRegexp

Generate regex with priority

Parameters

Returns CompiledRoute

matcher

Find best match for a given path

Parameters

Returns Match match

install

With npm do:

npm install multi-path-matcher

license

BSD-2-Clause