Skip to content

bpierre/miniroutes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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: [],
//                   path: 'foo' }

routing('bar/param1');
// Console output: { name: 'bar',
//                   params: ['param1', null],
//                   path: 'bar/param1' }

routing('bar/param1/param2');
// Console output: { name: 'bar',
//                   params: ['param1', 'param2'],
//                   path: '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.

About

Mini routing system based on regular expressions

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published