patrickrgaffney / path-param-matcher

Convert URL route paths into a RegExp

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

License:MIT License


Languages

Language:JavaScript 100.0%