diff --git a/README.md b/README.md index 6913b35..d7f7471 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,9 @@ Routes can register multiple callbacks. See [`routington.define()`](https://github.com/pillarjs/routington#nodes-node--routerdefineroute) for all route options. +### router.default(params) +Trigger the default route. Useful to trigger errors externally. + ### router(route) Match a route and execute the corresponding callback. Alias: `router.emit()`. diff --git a/index.js b/index.js index 983835d..500a54a 100644 --- a/index.js +++ b/index.js @@ -15,6 +15,7 @@ function wayfarer (dft) { const mounts = routington() emit[sym] = true + emit.default = defaultFn emit.emit = emit emit.on = on @@ -51,6 +52,12 @@ function wayfarer (dft) { }) } + // match the default route + // obj? -> null + function defaultFn (params) { + emit(dft, params) + } + // match a mounted router // str -> obj|null function matchSub (path) { diff --git a/test.js b/test.js index 871c0b1..b245dbb 100644 --- a/test.js +++ b/test.js @@ -41,7 +41,7 @@ test('.emit() should throw if no matches are found', function (t) { t.throws(r1.bind(r1, '/woops'), /path/) }) -test('.emi() should allow multiple handlers', function (t) { +test('.emit() should allow multiple handlers', function (t) { t.plan(2) const r1 = wayfarer() @@ -100,6 +100,18 @@ test('.emit() should allow nesting', function (t) { r7('/foo/bin/bar/baz') }) +test('.default() should trigger the default route', function (t) { + t.plan(5) + const r = wayfarer('/404') + r.on('/404', function (param) { + t.pass('called') + t.equal(typeof param, 'object') + if (param.foo) t.equal(param.foo, 'bar') + }) + r.default() + r.default({ foo: 'bar' }) +}) + test('aliases', function (t) { t.plan(1) const r = wayfarer()