Skip to content

Commit

Permalink
Merge e249d93 into ff25c87
Browse files Browse the repository at this point in the history
  • Loading branch information
tornqvist committed Mar 20, 2018
2 parents ff25c87 + e249d93 commit 97386dc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
13 changes: 4 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,21 @@ function Wayfarer (dft) {

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

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

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

return emit

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

// match and call a route
Expand Down Expand Up @@ -71,7 +66,7 @@ function Wayfarer (dft) {

function Route (matched) {
this.cb = matched.cb
this.route = matched.cb.route
this.route = matched.route
this.params = matched.params
}
}
26 changes: 18 additions & 8 deletions test/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,23 +336,33 @@ tape('router', function (t) {
t.test('should expose .route property', function (t) {
t.plan(1)
var r = wayfarer()
r.on('/foo', function () {
t.equal(this.route, '/foo', 'exposes route property')
r.on('/foo', function () {})
t.equal(r.match('/foo').route, '/foo', 'exposes route property')
})

t.test('should be called with self', function (t) {
t.plan(1)
var r = wayfarer()
r.on('/foo', function callback () {
t.equal(this, callback, 'calling context is self')
})
r('/foo')
})

t.test('should not mutate callback parameter', function (t) {
t.plan(4)
t.test('can register callback on many routes', function (t) {
t.plan(6)
var r = wayfarer()
var routes = ['/foo', '/bar']
r.on('/foo', callback)
r.on('/bar', callback)
r('/foo')
r('/bar')
for (var i = 0, len = routes.length, matched; i < len; i++) {
matched = r.match(routes[i])
t.equal(matched.cb, callback, 'matched callback is same')
t.equal(matched.route, routes[i], 'matched route is same')
r(routes[i])
}
function callback () {
t.notEqual(this, callback, 'callback was proxied')
t.equal(this.route, routes.shift(), 'proxy exposes route property')
t.equal(this, callback, 'calling context is same')
}
})
})
2 changes: 1 addition & 1 deletion trie.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function Trie () {

// create a node on the trie at route
// and return a node
// str -> null
// str -> obj
Trie.prototype.create = function (route) {
assert.equal(typeof route, 'string', 'route should be a string')
// strip leading '/' and split routes
Expand Down

0 comments on commit 97386dc

Please sign in to comment.