arc-router
A purely functional router for independently evaluating a path against a list of routes
Install
$ npm install arc-router --save
Features
Basic Usage
const ArcRouter = require('arc-router');
let Journey = new ArcRouter({
'/!wallpaper/**filePath[/]/:fileName([^\.]*\.jpg)':'wallpaper',
'/!shared/!docs/2*filePath/#docId':'doc',
'/!shared/!docs':'docsFolder'
});
const example1 = Journey.travel('/wallpaper/2016/australia/sydney/beach.jpg');
expect(example1).toEqual({
'match':'wallpaper',
'wallpaper':'wallpaper',
'filePath':'2016/australia/sydney',
'fileName': 'beach.jpg'
});
const example2 = Journey.travel('/shared/docs/important/contracts/123');
expect(example2).toEqual({
'match':'doc',
'shared':'shared',
'docs':'docs',
'filePath':['important','contracts'],
'docId': '123'
});
const example2 = Journey.travel('/shared/docs');
expect(example2).toEqual({
'match':'docsFolder',
'shared':'shared',
'docs':'docs'
});
const falseMatch = Journey.travel('/shared/docs/fail');
const falseMatch = Journey.travel('/shared/docs/import/contracts/string/123');
API
new ArcRouter([routeMap:Object])
Create a new ArcRouter
object. Requires new
, routeMap is optional in the constructor.
.setMap(routeMap:Object)
Take an object in the form of {[ROUTE]:[MATCH_NAME]} and evaluate against it
.travel(routeString:String)
Accept a string to travel the route map with. Travel returns a routeData object, binding variables based on the map route patterns.
Testing
npm test