-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a5eb180
commit f8de0e3
Showing
6 changed files
with
277 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,113 +1,52 @@ | ||
const routington = require('routington') | ||
const symbol = require('es6-symbol') | ||
const assert = require('assert') | ||
const xtend = require('xtend') | ||
const trie = require('./trie') | ||
|
||
const sym = symbol('wayfarer') | ||
|
||
module.exports = wayfarer | ||
module.exports = Wayfarer | ||
|
||
// create a router | ||
// str -> obj | ||
function wayfarer (dft) { | ||
dft = sanitizeUri(dft) || '' | ||
const routes = routington() | ||
const subrouters = routington() | ||
function Wayfarer (dft) { | ||
if (!(this instanceof Wayfarer)) return new Wayfarer(dft) | ||
|
||
emit._subrouters = subrouters | ||
emit._default = defaultFn | ||
emit._routes = routes | ||
emit[sym] = true | ||
emit._sym = sym | ||
const _default = (dft || '').replace(/^\//, '') | ||
const _trie = trie() | ||
|
||
emit._trie = _trie | ||
emit.emit = emit | ||
emit.on = on | ||
emit._wayfarer = true | ||
|
||
return emit | ||
|
||
// define a path | ||
// define a route | ||
// (str, fn) -> obj | ||
function on (path, cb) { | ||
assert.equal(typeof path, 'string') | ||
function on (route, cb) { | ||
assert.equal(typeof route, 'string') | ||
assert.equal(typeof cb, 'function') | ||
path = sanitizeUri(path) || '' | ||
const node = cb[sym] ? subrouters.define(path)[0] : routes.define(path)[0] | ||
if (Array.isArray(node.cb)) node.cb.push(cb) | ||
else node.cb = [cb] | ||
return emit | ||
} | ||
|
||
// match and call a route | ||
// str -> null | ||
function emit (path, params, parentDefault) { | ||
path = sanitizeUri(path) || '' | ||
params = params || {} | ||
|
||
const sub = matchSub(path) | ||
if (sub) path = sub.path | ||
route = route || '/' | ||
|
||
const localDft = routes.match(dft) || parentDefault | ||
const match = sub ? sub.matched : routes.match(path) || localDft | ||
assert.ok(match, 'path ' + path + ' did not match') | ||
params = xtend(params, match.param) | ||
|
||
// only nested routers need a path | ||
match.node.cb.forEach(function (cb) { | ||
sub ? cb(path, params, localDft) : cb(params) | ||
}) | ||
} | ||
if (cb && cb._wayfarer && cb._trie) { | ||
_trie.mount(route, cb._trie.trie) | ||
} else { | ||
const node = _trie.create(route) | ||
node.cb = cb | ||
} | ||
|
||
// match the default route | ||
// obj? -> null | ||
function defaultFn (params) { | ||
emit(dft, params) | ||
return emit | ||
} | ||
|
||
// match a mounted router | ||
// str -> obj|null | ||
function matchSub (path) { | ||
var match = null | ||
const split = path.split('/') | ||
var count = split.length | ||
|
||
var n = 0 | ||
while (n < split.length) { | ||
var arr = [] | ||
var ln = split.length - n | ||
var cnt = -1 | ||
// match and call a route | ||
// (str, obj?) -> null | ||
function emit (route) { | ||
assert.notEqual(route, undefined, "'route' must be defined") | ||
|
||
while (++cnt < ln) { | ||
arr.push(split[cnt]) | ||
} | ||
const node = _trie.match(route) | ||
if (node && node.cb) return node.cb(node.params) | ||
|
||
var nw = arr.join('/') | ||
var imatch = subrouters.match(nw) | ||
if (imatch) { | ||
match = imatch | ||
count = arr.length | ||
break | ||
} | ||
n++ | ||
} | ||
const dft = _trie.match(_default) | ||
if (dft && dft.cb) return dft.cb(dft.params) | ||
|
||
if (!match) return | ||
|
||
while (count--) { | ||
split.shift() | ||
} | ||
|
||
path = split.join('/') | ||
const ret = { | ||
matched: match, | ||
path: path | ||
} | ||
return ret | ||
throw new Error("route '" + route + "' did not match") | ||
} | ||
} | ||
|
||
// strip leading `/` | ||
// str -> str | ||
function sanitizeUri (str) { | ||
str = str || '' | ||
return str.replace(/^\//, '') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.