@funjs/route-parser
v1.4.0
Published
Simple and fast functional route parser...
Downloads
64
Maintainers
Readme
What is it?
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.
How do I install it?
npm install --save @funjs/route-parser
or
yarn add @funjs/route-parser
How do I use it?
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' }
How do I customize delimiters and named segment?
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' }
What can I use in my routes?
| 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
How does it work?
For performance reasons the matching is done by generating regulars expressions.
How to run the benchmark?
cd benchmark
npm install
node index.js
TODO:
- [x] RegExp Matching
- [x] Named parameters
- [x] named parameters Options
- [ ] Reverse Matching
- [x] Customizables delimiters and named segments
- [ ] Querystrings