Comparing version 6.6.2 to 6.6.3
33
index.js
@@ -15,4 +15,5 @@ var assert = require('assert') | ||
emit._trie = _trie | ||
emit.on = on | ||
emit.emit = emit | ||
emit.on = on | ||
emit.match = match | ||
emit._wayfarer = true | ||
@@ -44,4 +45,6 @@ | ||
function emit (route) { | ||
assert.notEqual(route, undefined, "'route' must be defined") | ||
var matched = match(route) | ||
var args = new Array(arguments.length) | ||
args[0] = matched.params | ||
for (var i = 1; i < args.length; i++) { | ||
@@ -51,18 +54,22 @@ args[i] = arguments[i] | ||
var node = _trie.match(route) | ||
if (node && node.cb) { | ||
args[0] = node.params | ||
var cb = node.cb | ||
return cb.apply(cb, args) | ||
} | ||
return matched.cb.apply(matched.cb, args) | ||
} | ||
function match (route) { | ||
assert.notEqual(route, undefined, "'route' must be defined") | ||
var matched = _trie.match(route) | ||
if (matched && matched.cb) return new Route(matched) | ||
var dft = _trie.match(_default) | ||
if (dft && dft.cb) { | ||
args[0] = dft.params | ||
var dftcb = dft.cb | ||
return dftcb.apply(dftcb, args) | ||
} | ||
if (dft && dft.cb) return new Route(dft) | ||
throw new Error("route '" + route + "' did not match") | ||
} | ||
function Route (matched) { | ||
this.cb = matched.cb | ||
this.route = matched.cb.route | ||
this.params = matched.params | ||
} | ||
} |
{ | ||
"name": "wayfarer", | ||
"version": "6.6.2", | ||
"version": "6.6.3", | ||
"description": "Composable trie based router", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -94,2 +94,5 @@ # wayfarer [![stability][0]][1] | ||
### matchedRoute = router.match(route) | ||
Matches a route and returns an object. The returned object contains the properties `{cb, params, route}`. This method does not invoke the callback of a route. If no route matches, the default route will be returned. If no default route matches, an error will be thrown. | ||
### val = router(route, [arg1, ...]) | ||
@@ -96,0 +99,0 @@ Match a route and execute the corresponding callback. Alias: `router.emit()`. |
@@ -5,3 +5,3 @@ var wayfarer = require('../') | ||
tape('trie', function (t) { | ||
tape('router', function (t) { | ||
t.test('should match a path', function (t) { | ||
@@ -66,2 +66,17 @@ t.plan(1) | ||
t.test('.match() should match paths', function (t) { | ||
t.plan(2) | ||
var r = wayfarer() | ||
r.on('/foo/bar', function () { | ||
t.fail('should not call callback') | ||
}) | ||
r.on('/foo/baz', noop) | ||
var bar = r.match('/foo/bar') | ||
t.equal(bar.route, '/foo/bar') | ||
var baz = r.match('/foo/baz') | ||
t.equal(baz.route, '/foo/baz') | ||
}) | ||
t.test('.emit() should match partials', function (t) { | ||
@@ -76,2 +91,10 @@ t.plan(1) | ||
t.test('.match() should match partials', function (t) { | ||
t.plan(1) | ||
var r = wayfarer() | ||
r.on('/:user', noop) | ||
var toby = r.match('/tobi') | ||
t.equal(toby.params.user, 'tobi') | ||
}) | ||
t.test('.emit() should match paths before partials', function (t) { | ||
@@ -90,4 +113,6 @@ t.plan(1) | ||
var r = wayfarer() | ||
r.on('/:user', noop) | ||
r.on('/:user', function () { | ||
t.fail('wrong callback called') | ||
}) | ||
r.on('/:user', function () { | ||
t.pass('called') | ||
@@ -163,16 +188,43 @@ }) | ||
r7('/foo/bin/bar') | ||
}) | ||
// var r10 = wayfarer() | ||
// var r11 = wayfarer() | ||
// var r12 = wayfarer() | ||
// r12.on('/:grandchild', function (param) { | ||
// t.equal(param.parent, 'bin', 'nested 3 levels with params') | ||
// t.equal(param.child, 'bar', 'nested 3 levels with params') | ||
// t.equal(param.grandchild, 'baz', 'nested 3 levels with parmas') | ||
// }) | ||
// r11.on('/:child', r12) | ||
// r10.on('/foo/:parent', r11) | ||
// r10('/foo/bin/bar/baz') | ||
t.test('.emit() should match nested partials of subrouters', function (t) { | ||
t.plan(3) | ||
var r1 = wayfarer() | ||
var r2 = wayfarer() | ||
var r3 = wayfarer() | ||
r3.on('/:grandchild', function (param) { | ||
t.equal(param.parent, 'bin', 'nested 3 levels with params') | ||
t.equal(param.child, 'bar', 'nested 3 levels with params') | ||
t.equal(param.grandchild, 'baz', 'nested 3 levels with parmas') | ||
}) | ||
r2.on('/:child', r3) | ||
r1.on('/foo/:parent', r2) | ||
r1('/foo/bin/bar/baz') | ||
}) | ||
t.test('.match() should return nested partials of subrouters', function (t) { | ||
t.plan(3) | ||
var r1 = wayfarer() | ||
var r2 = wayfarer() | ||
var r3 = wayfarer() | ||
r3.on('/:grandchild', noop) | ||
r2.on('/:child', r3) | ||
r1.on('/foo/:parent', r2) | ||
var matched = r1.match('/foo/bin/bar/baz') | ||
t.equal(matched.params.parent, 'bin') | ||
t.equal(matched.params.child, 'bar') | ||
t.equal(matched.params.grandchild, 'baz') | ||
}) | ||
t.test('.match() returns a handler of a route', function (t) { | ||
t.plan(1) | ||
var r = wayfarer() | ||
r.on('/:user', function () { | ||
t.pass('called') | ||
}) | ||
var toby = r.match('/tobi') | ||
toby.cb() | ||
}) | ||
t.test('nested routes should call parent default route', function (t) { | ||
@@ -289,3 +341,3 @@ t.plan(4) | ||
t.test('should expose .router property', function (t) { | ||
t.test('should expose .route property', function (t) { | ||
t.plan(1) | ||
@@ -292,0 +344,0 @@ var r = wayfarer() |
@@ -118,4 +118,3 @@ var mutate = require('xtend/mutable') | ||
} else { | ||
var headArr = split.splice(0, split.length - 1) | ||
var head = headArr.join('/') | ||
var head = split.join('/') | ||
key = split[0] | ||
@@ -122,0 +121,0 @@ node = this.create(head) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30524
678
157
15