0.1.1 • Published 10 years ago

miniroutes v0.1.1

Weekly downloads
5
License
MIT
Repository
github
Last release
10 years ago

miniroutes Build Status

Mini routing system based on regular expressions.

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.