A 10X FASTER and functional route parser (run the benchmark for yourself), for Javascript in Node and the browser. Its api is inspired by route-parser, but is implemented in a functional way, don't rely in 'this' keyword.
npm install --save @funjs/route-parser
or
yarn add @funjs/route-parser
const Router = require('@funjs/route-parser');
const route = Router('/books/:section=(programming|romance|horror)/:title');
route.match('/books/programming/JavaScript-Good-Parts'); // { section: 'programming', title: 'JavaScript-Good-Parts' }
const Router = require('@funjs/route-parser', { delimiter: ':', namedSegment: '$' });
const route = Router('books:$section=(programming|romance|horror):$title');
route.match('books:programming:JavaScript-Good-Parts'); // { section: 'programming', title: 'JavaScript-Good-Parts' }
Example | Description |
---|---|
:name |
a named parameter to capture from the route up to / , ? , or end of string |
* |
a splat to capture from the route up to ? or end of string |
:name=(a|b|c) |
a named parameter group that doesn't have to be part of the query. Can contain nested optional groups, params, and splats |
anything else | free form literals |
Some examples:
/some/:thing
/users/:id/comments/:comment/rating/:rating
/*/foo/*
/books/:section=(Romance|Horror)/:title
For performance reasons the matching is done by generating regulars expressions.
cd benchmark
npm install
node index.js
- RegExp Matching
- Named parameters
- named parameters Options
- Reverse Matching
- Customizables delimiters and named segments
- Querystrings