-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathget-all-routes.js
36 lines (31 loc) · 997 Bytes
/
get-all-routes.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
33
34
35
36
/* eslint-disable node/no-deprecated-api */
var assert = require('assert')
module.exports = getAllRoutes
var transform = function (trie, previous) {
var prev = previous || '/'
var routes = {}
var nodes = trie.nodes
Object.keys(nodes).forEach(function (key) {
var path = (prev === '/' ? prev : prev + '/') + (key === '$$' ? ':' + trie.name : key)
var cb = nodes[key].cb
if (cb !== undefined) {
routes[path] = cb
}
if (Object.keys(nodes[key].nodes).length !== 0) {
var obj = transform(nodes[key], path)
Object.keys(obj).forEach(function (r) {
routes[r] = obj[r]
})
}
})
return routes
}
// walk a wayfarer trie
// (obj, fn) -> null
function getAllRoutes (router) {
assert.equal(typeof router, 'function', 'wayfarer.getAllRoutes: router should be an function')
var trie = router._trie
assert.equal(typeof trie, 'object', 'wayfarer.getAllRoutes: trie should be an object')
var tree = trie.trie
return transform(tree)
}