Quickly parse & match URLs
npm install @poppinss/matchit> Quickly parse & match URLs. Forked from lukeed/matchit
[![circleci-image]][circleci-url] [![npm-image]][npm-url] [![license-image]][license-url] [![synk-image]][synk-url]
```
$ npm install --save matchit
`js
const { exec, match, parse } = require('matchit');
parse('/foo/:bar/:baz?');
//=> [
//=> { old:'/foo/:bar', type:0, val:'foo' },
//=> { old:'/foo/:bar', type:1, val:'bar' },
//=> { old:'/foo/:bar', type:3, val:'baz' }
//=> ]
const routes = ['/', '/foo', 'bar', '/baz', '/baz/:title','/bat/*'].map(parse);
match('/', routes);
//=> [{ old:'/', type:0, val:'/' }]
match('/foo', routes);
//=> [{ old:'/foo', type:0, val:'foo' }]
match('/bar', routes);
//=> [{ old:'bar', type:0, val:'bar' }]
match('/baz', routes);
//=> [{ old:'/baz', type:0, val:'baz' }]
let a = match('/baz/hello', routes);
//=> [{...}, {...}]
let b = exec('/baz/hello', a);
//=> { title:'hello' }
match('/bat/quz/qut', routes);
//=> [
//=> { old:'/bat/*', type:0, val:'bat' },
//=> { old:'/bat/', type:2, val:'' }
//=> ]
`
Returns: Array
The route is split and parsed into a "definition" array of objects. Each object ("segment") contains a val, type, and old key:
* old — The route's original valuetype
* — An numerical representation of the segment type.0
* - static1
* - parameter2
* - any/wildcard3
* - optional paramval
* — The current segment's value. This is either a static value of the name of a parameter
#### route
Type: String
A single URL pattern.
> Note: Input will be stripped of all leading & trailing / characters, so there's no need to normalize your own URLs before passing it to parse!
Returns: Array
Returns the route's encoded definition. See matchit.parse.
#### url
Type: String
The true URL you want to be matched.
#### routes
Type: Array
_All_ "parsed" route definitions, via matchit.parse.
> Important: Multiple routes will require an Array of matchit.parse outputs.
Returns: Object
Returns an object an object of key:val pairs, as defined by your route pattern.
#### url
Type: String
The URL (pathname) to evaluate.
> Important: This should be pathnames only as any querystrings will be included the response.
#### match
Type: Array
The route definition to use, via matchit.match.
> Running Node v10.13.0
`Parsing
matchit x 1,489,482 ops/sec ±2.89% (97 runs sampled)
regexparam x 406,824 ops/sec ±1.38% (96 runs sampled)
path-to-regexp x 83,439 ops/sec ±0.89% (96 runs sampled)
path-to-regexp.parse x 421,266 ops/sec ±0.13% (97 runs sampled)
[circleci-image]: https://img.shields.io/circleci/project/github/poppinss/matchit/master.svg?style=for-the-badge&logo=circleci
[circleci-url]: https://circleci.com/gh/poppinss/matchit "circleci"
[npm-image]: https://img.shields.io/npm/v/@poppinss/matchit.svg?style=for-the-badge&logo=npm
[npm-url]: https://npmjs.org/package/@poppinss/matchit "npm"
[license-image]: https://img.shields.io/npm/l/@poppinss/matchit?color=blueviolet&style=for-the-badge
[license-url]: license.md "license"
[synk-image]: https://img.shields.io/snyk/vulnerabilities/github/poppinss/matchit?label=Synk%20Vulnerabilities&style=for-the-badge
[synk-url]: https://snyk.io/test/github/poppinss/matchit?targetFile=package.json "synk"