-
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 53f3e1b
Showing
6 changed files
with
253 additions
and
180 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.emit = emit | ||
emit.on = on | ||
emit._trie = _trie | ||
emit.emit = emit.bind(this) | ||
emit.on = on.bind(this) | ||
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 this | ||
} | ||
|
||
// 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 | ||
|
||
while (++cnt < ln) { | ||
arr.push(split[cnt]) | ||
} | ||
|
||
var nw = arr.join('/') | ||
var imatch = subrouters.match(nw) | ||
if (imatch) { | ||
match = imatch | ||
count = arr.length | ||
break | ||
} | ||
n++ | ||
} | ||
// match and call a route | ||
// (str, obj?) -> null | ||
function emit (route) { | ||
assert.notEqual(route, undefined, "'route' must be defined") | ||
|
||
if (!match) return | ||
const node = _trie.match(route) || _trie.match(_default) | ||
assert.ok(node, "route '" + route + "' did not match") | ||
|
||
while (count--) { | ||
split.shift() | ||
} | ||
console.log(node) | ||
console.log(JSON.stringify(_trie)) | ||
|
||
path = split.join('/') | ||
const ret = { | ||
matched: match, | ||
path: path | ||
} | ||
return ret | ||
return node.cb(node.params) | ||
} | ||
} | ||
|
||
// 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.