Skip to content

Mini routing system based on regular expressions.

Notifications You must be signed in to change notification settings

raphaelbastide/miniroutes

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

miniroutes

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) {
  console.log(route); // matched 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 it with minihash:

var miniroutes = require('miniroutes');
var minihash = require('minihash');

var routes = [ /* … */ ];

var hash = minihash('!/', miniroutes(routes, function(route) {
  console.log(route);
}));

License

MIT

About

Mini routing system based on regular expressions.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%