miniroutes

0.1.1 • Public • Published

miniroutes Build Status

Mini routing system based on regular expressions.

miniroutes illustration

Usage

var miniroutes = require('miniroutes');
 
var paths = [
 
  // Match 'foo' and 'foo/'
  [ 'foo', /^foo\/?$/ ],
 
  // Match 'bar', 'bar/<anything>', 'bar/<anything>/<anything>'
  [ 'bar', /^bar(?:\/([^\/]+))?(?:\/([^\/]+))?\/?$/ ]
 
];
 
var routing = miniroutes(paths, function(route, previous) {
  // `route` is the matched route
  // `previous` is the previously matched route
  console.log(route);
});
 
routing('foo');
// Console output: { name: 'foo',
//                   params: [],
//                   value: 'foo' }
 
routing('bar/param1');
// Console output: { name: 'bar',
//                   params: ['param1', null],
//                   value: 'bar/param1' }
 
routing('bar/param1/param2');
// Console output: { name: 'bar',
//                   params: ['param1', 'param2'],
//                   value: 'bar/param1/param2' }

You can also combine miniroutes with minihash:

var miniroutes = require('miniroutes');
var minihash = require('minihash');
 
var routes = [ /* … */ ];
 
var hash = minihash('!/', miniroutes(routes, function(route, previous) {
  console.log(route, previous);
}));

Installation

$ npm install miniroutes

Browser compatibility

IE9+ and modern browsers.

Browser support

License

MIT

Special thanks

Illustration made by Raphaël Bastide with scri.ch.

Readme

Keywords

none

Package Sidebar

Install

npm i miniroutes

Weekly Downloads

2

Version

0.1.1

License

MIT

Last publish

Collaborators

  • bpierre