https://github.com/patrickrgaffney/path-param-matcher
Convert URL route paths into a RegExp
https://github.com/patrickrgaffney/path-param-matcher
node nodejs parameters path-parser router
Last synced: 11 months ago
JSON representation
Convert URL route paths into a RegExp
- Host: GitHub
- URL: https://github.com/patrickrgaffney/path-param-matcher
- Owner: patrickrgaffney
- License: mit
- Created: 2020-02-13T18:43:27.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-03-25T03:42:00.000Z (almost 6 years ago)
- Last Synced: 2025-01-16T10:53:05.690Z (about 1 year ago)
- Topics: node, nodejs, parameters, path-parser, router
- Language: JavaScript
- Size: 53.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# path-param-matcher

Parse URL route paths into a `RegExp` with a syntax similar to [`chi`](https://github.com/go-chi/chi). Conceptually similar to [`path-to-regexp`](https://github.com/pillarjs/path-to-regexp), although simpler.
## Install
```sh
npm install --save path-param-matcher
```
## Usage
```js
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(/^\/(?[^/]+)\/(?[^/]+)$/)
*/
/** Placeholders can provide their own regex. */
parser('/date/{yyyy:\\d\\d\\d\\d}/{mm:\\d\\d}/{dd:\\d\\d}')
/**
* => new RegExp(/^\/date\/(?\d\d\d\d)\/(?\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