Skip to content

Commit

Permalink
fix wildcard param handling (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshuawuyts authored Jul 27, 2017
1 parent 344b650 commit 53e443a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
32 changes: 32 additions & 0 deletions test/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,38 @@ tape('trie', function (t) {
r('/foo/bar/beep/boop')
})

t.test('wildcards dont conflict with params', function (t) {
t.plan(3)
var router

router = wayfarer()
router.on('/*', function (params) {
t.fail('wildcard called')
})
router.on('/:match', function (params) {
t.pass('param called')
})
router('/foo')

router = wayfarer()
router.on('/*', function (params) {
t.fail('wildcard called')
})
router.on('/:match/foo', function (params) {
t.pass('param called')
})
router('/foo/foo')

router = wayfarer()
router.on('/*', function (params) {
t.pass('wildcard called')
})
router.on('/:match/foo', function (params) {
t.fail('param called')
})
router('/foo/bar')
})

t.test('safe decodeURIComponent', function (t) {
t.plan(1)
var r = wayfarer('/404')
Expand Down
16 changes: 8 additions & 8 deletions trie.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ Trie.prototype.match = function (route) {
if (trie.nodes.hasOwnProperty(thisRoute)) {
// match regular routes first
return search(index + 1, trie.nodes[thisRoute])
} else if (trie.name) {
// match named routes
try {
params[trie.name] = decodeURIComponent(thisRoute)
} catch (e) {
return search(index, undefined)
}
return search(index + 1, trie.nodes['$$'])
} else if (trie.wildcard) {
// match wildcards
try {
Expand All @@ -79,14 +87,6 @@ Trie.prototype.match = function (route) {
}
// return early, or else search may keep recursing through the wildcard
return trie.nodes['$$']
} else if (trie.name) {
// match named routes
try {
params[trie.name] = decodeURIComponent(thisRoute)
} catch (e) {
return search(index, undefined)
}
return search(index + 1, trie.nodes['$$'])
} else {
// no matches found
return search(index + 1)
Expand Down

0 comments on commit 53e443a

Please sign in to comment.