Skip to content

Commit

Permalink
wayfarer: add .default()
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshuawuyts committed Jul 29, 2015
1 parent aa4f038 commit d732256
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()`.

Expand Down
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function wayfarer (dft) {
const mounts = routington()

emit[sym] = true
emit.default = defaultFn
emit.emit = emit
emit.on = on

Expand Down Expand Up @@ -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) {
Expand Down
14 changes: 13 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit d732256

Please sign in to comment.