my-router v2.0.0
my-router
is my simple router for me.
my-router
routes everything by URL path.
Installation
npm install my-router
Usage
var Router = Router; var router= patterns: ':id': /^\w+$/ ; // Add values to routerrouter;router;router;router; router;router; // Route by paths console;/* returns { 'params':{}, 'result': 'foo' } */ console;/* returns { 'params':{}, 'result': 'foobar' } */ console;/* returns { 'params':{ ':id': 'hoge' }, 'result': 'id' } */ console;/* returns { 'params':{ ':id': 'hoge' }, 'result': 'idbar' } */ console;/* returns null */ console;/* returns { 'params':{ ':num': '123', ':id': 'foo' }, 'result': 'idnum' } */
TypeScript declaration
Available.
Note that Router
is of type Router<T>
, where T
is the type of values routed.
APIs
router = new Router([options])
Creates new router.
options.patternPrefix
a character which is prefixed with the name of pattern-match segment. Defaults to:
options.patterns
a dictionary from the name of pattern-match segment to RegExp
router.add(path,value)
Mounts value
to path
.
result = router.route(path)
Gets value mounted at path
. Returns null
if none.
If value is available, returned object has:
params
dictionary of pattern matching results.result
value.
result = router.addPattern(seg,pattern)
Adds new pattern-match segment definition to router
.
Changelog
- v1.1.0 (2015-06-20)
- Add
router.addPattern
for me.
- Add
- v1.0.0
License
MIT