-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathwalk.js
32 lines (27 loc) · 900 Bytes
/
walk.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/* eslint-disable node/no-deprecated-api */
var assert = require('assert')
module.exports = walk
// walk a wayfarer trie
// (obj, fn) -> null
function walk (router, transform) {
assert.equal(typeof router, 'function', 'wayfarer.walk: router should be an function')
assert.equal(typeof transform, 'function', 'wayfarer.walk: transform should be a function')
var trie = router._trie
assert.equal(typeof trie, 'object', 'wayfarer.walk: trie should be an object')
// (str, obj) -> null
;(function walk (route, trie) {
if (trie.cb) {
trie.cb = transform(route, trie.cb)
}
if (trie.nodes) {
var nodes = trie.nodes
Object.keys(nodes).forEach(function (key) {
var node = nodes[key]
var newRoute = (key === '$$')
? route + '/:' + trie.name
: route + '/' + key
walk(newRoute, node)
})
}
})('', trie.trie)
}