Giter VIP home page Giter VIP logo

multi-path-matcher's Introduction

npm License Typed with TypeScript bundlejs 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

Route

One single route

Type: Object

Properties

CompiledRoute

Result of a path compilation priorities for each path component

Type: Route

Properties

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

Match

Result of a match

Type: Object

Properties

  • route Route? as given to the compiler, undefined if no matching route was found
  • params 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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.