Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

patrickrgaffney/path-param-matcher

Open more actions menu

Repository files navigation

path-param-matcher

Tests

Parse URL route paths into a RegExp with a syntax similar to chi. Conceptually similar to path-to-regexp, although simpler.

Install

npm install --save path-param-matcher

Usage

const parser = require('path-param-matcher')

/** Basic routes. */
parser('/')
/**
 * => new RegExp(/^\/$/)
 */

/** Trailing slashes must be explicit. */
parser('/some/thing/')
/**
 * => new RegExp(/^\/some\/thing\/$/)
 */

/** Named placeholders. */
parser('/{some}/{thing}')
/**
 * => new RegExp(/^\/(?<some>[^/]+)\/(?<thing>[^/]+)$/)
 */

/** Placeholders can provide their own regex. */
parser('/date/{yyyy:\\d\\d\\d\\d}/{mm:\\d\\d}/{dd:\\d\\d}')
/**
 * => new RegExp(/^\/date\/(?<yyyy>\d\d\d\d)\/(?<mm>\d\d)\/(?<dd>\d\d)$/)
 */

/** Anonymous placeholders. */
parser('/date/{:\\d\\d\\d\\d}/')
/**
 * => new RegExp(/^\/date\/(?:\d\d\d\d)\/$/)
 */

API

parser(path)

  • returns RegExp
  • throws TypeError

path: String

A TypeError will be thrown if the path meets any of the following criteria:

  • It is not a String.
  • It does not begin with a /.
  • A placeholder regex contains a /, {, or }.

License

MIT © Pat Gaffney

About

Convert URL route paths into a RegExp

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.