miniroutes
Mini routing system based on regular expressions.
Last updated 7 years ago by bpierre .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install miniroutes 
SYNC missed versions from official npm registry.

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.

Current Tags

  • 0.1.1                                ...           latest (7 years ago)

3 Versions

  • 0.1.1                                ...           7 years ago
  • 0.1.0                                ...           7 years ago
  • 0.0.1                                ...           7 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (4)
Dependents (1)

Copyright 2014 - 2016 © taobao.org |