Skip to content

Commit

Permalink
Merge 6ef24b8 into 2560018
Browse files Browse the repository at this point in the history
  • Loading branch information
tornqvist committed Feb 15, 2018
2 parents 2560018 + 6ef24b8 commit a8907a4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
11 changes: 8 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,26 @@ function Wayfarer (dft) {

// define a route
// (str, fn) -> obj
function on (route, cb) {
function on (route, fn) {
assert.equal(typeof route, 'string')
assert.equal(typeof cb, 'function')
assert.equal(typeof fn, 'function')

var cb = fn._wayfarer && fn._trie ? fn : proxy
route = route || '/'
cb.route = route

if (cb && cb._wayfarer && cb._trie) {
if (cb._wayfarer && cb._trie) {
_trie.mount(route, cb._trie.trie)
} else {
var node = _trie.create(route)
node.cb = cb
}

return emit

function proxy () {
return fn.apply(this, Array.prototype.slice.call(arguments))
}
}

// match and call a route
Expand Down
14 changes: 14 additions & 0 deletions test/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,4 +341,18 @@ tape('router', function (t) {
})
r('/foo')
})

t.test('should not mutate callback parameter', function (t) {
t.plan(4)
var r = wayfarer()
var routes = ['/foo', '/bar']
r.on('/foo', callback)
r.on('/bar', callback)
r('/foo')
r('/bar')
function callback () {
t.notEqual(this, callback, 'callback was proxied')
t.equal(this.route, routes.shift(), 'proxy exposes route property')
}
})
})

0 comments on commit a8907a4

Please sign in to comment.