Skip to content

fun-js/route-parser

Repository files navigation

travis build Dependency Status devDependency Status Codecov MIT License semantic-release

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:

  • RegExp Matching
  • Named parameters
  • named parameters Options
  • Reverse Matching
  • Customizables delimiters and named segments
  • Querystrings