-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
313 lines (255 loc) · 7.15 KB
/
index.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
'use strict'
/**
* JS Radix Router implementation
*/
const assert = require('assert')
// node types
const NORMAL_NODE = 0
const WILDCARD_NODE = 1
const PLACEHOLDER_NODE = 2
function _validateInput(path, strictPaths) {
assert(path, '"path" must be provided')
assert(typeof path === 'string', '"path" must be that of a string')
let pathEnd
// allow for trailing slashes to match by removing it
if (
!strictPaths &&
path.length > 1 &&
path[(pathEnd = path.length - 1)] === '/'
) {
path = path.slice(0, pathEnd)
}
return path
}
/**
* Node of the Radix Tree
* @constructor
*/
function Node(options) {
options = options || {}
this.type = options.type || NORMAL_NODE
// if placeholder node
this.paramName = options.paramName || null
this.parent = options.parent || null
this.children = {}
this.data = options.data || null
// keep track of special child nodes
this.wildcardChildNode = null
this.placeholderChildNode = null
}
function _getNodeType(str) {
let type
if (str[0] === ':') {
type = PLACEHOLDER_NODE
} else if (str === '**') {
type = WILDCARD_NODE
} else {
type = NORMAL_NODE
}
return type
}
function _findNode(path, rootNode) {
const sections = path.split('/')
const params = {}
let paramsFound = false
let wildcardNode = null
let node = rootNode
for (let i = 0; i < sections.length; i++) {
const section = sections[i]
if (node.wildcardChildNode !== null) {
wildcardNode = node.wildcardChildNode
}
// exact matches take precedence over placeholders
const nextNode = node.children[section]
if (nextNode !== undefined) {
node = nextNode
} else {
node = node.placeholderChildNode
if (node !== null) {
params[node.paramName] = section
paramsFound = true
} else {
break
}
}
}
if ((node === null || node.data === null) && wildcardNode !== null) {
node = wildcardNode
}
return {
node: node,
params: paramsFound ? params : undefined
}
}
function _getAllNodesWithData(node, resultArray) {
const keys = Object.keys(node.children)
for (let i = 0; i < keys.length; i++) {
const nextNode = node.children[keys[i]]
_getAllNodesWithData(nextNode, resultArray)
}
}
/**
* The Radix Router
* @constructor
*/
function RadixRouter(options) {
const self = this
self._rootNode = new Node()
self._strictMode = options && options.strict
self._staticRoutesMap = {}
// handle insertion of routes passed into constructor
const routes = options && options.routes
if (routes) {
routes.forEach(function(route) {
self.insert(route)
})
}
}
RadixRouter.prototype = {
/**
* Perform lookup of given path in radix tree
* @param { string } path - the path to search for
*
* @returns { object } The data that was originally inserted into the tree
*/
lookup: function(path) {
const self = this
path = _validateInput(path, self._strictMode)
// optimization, if a route is static and does not have any
// variable sections, retrieve from a static routes map
let staticPathNode
if ((staticPathNode = self._staticRoutesMap[path])) {
return staticPathNode.data
}
const result = _findNode(path, self._rootNode)
const node = result.node
const params = result.params
const data = (node !== null && node.data) || null
if (data !== null && params !== undefined) {
data.params = params
}
return data
},
/**
* Perform lookup of all paths that start with the given prefix
* @param { string } prefix - the prefix to match
*
* @returns { object[] } An array of matches along with any data that
* was originally passed in when inserted
*/
startsWith: function(prefix) {
const self = this
prefix = _validateInput(prefix, self._strictMode)
const sections = prefix.split('/')
let node = self._rootNode
const resultArray = []
const endSections = sections.length - 1
for (let i = 0; i < sections.length; i++) {
const section = sections[i]
if (node.data) {
resultArray.push(node.data)
}
let nextNode = node.children[section]
if (nextNode !== undefined) {
node = nextNode
} else if (i === endSections) {
const keys = Object.keys(node.children)
for (let j = 0; j < keys.length; j++) {
const key = keys[j]
if (key.startsWith(section)) {
nextNode = node.children[key]
if (nextNode.data) {
resultArray.push(nextNode.data)
}
_getAllNodesWithData(nextNode, resultArray)
}
}
}
}
return resultArray
},
/**
* Perform an insert into the radix tree
* @param { string } data.path - the prefix to match
*
* Note: any other params attached to the data object will
* also be inserted as part of the node's data
*/
insert: function(data) {
const self = this
let path = data.path
let isStaticRoute = true
path = _validateInput(path, self._strictMode)
const sections = path.split('/')
let node = self._rootNode
for (let i = 0; i < sections.length; i++) {
const section = sections[i]
const children = node.children
let childNode
if ((childNode = children[section])) {
node = childNode
} else {
const type = _getNodeType(section)
// create new node to represent the next
// part of the path
childNode = new Node({
type: type,
parent: node
})
node.children[section] = childNode
if (type === PLACEHOLDER_NODE) {
childNode.paramName = section.slice(1)
node.placeholderChildNode = childNode
isStaticRoute = false
} else if (type === WILDCARD_NODE) {
node.wildcardChildNode = childNode
isStaticRoute = false
}
node = childNode
}
}
// store whatever data was provided into the node
node.data = data
// optimization, if a route is static and does not have any
// variable sections, we can store it into a map for faster
// retrievals
if (isStaticRoute === true) {
self._staticRoutesMap[path] = node
}
return node
},
/**
* Perform a remove on the tree
* @param { string } data.path - the route to match
*
* @returns { boolean } A boolean signifying if the remove was
* successful or not
*/
remove: function(path) {
const self = this
path = _validateInput(path, self._strictMode)
let success = false
const sections = path.split('/')
let node = self._rootNode
for (let i = 0; i < sections.length; i++) {
const section = sections[i]
node = node.children[section]
if (!node) {
return success
}
}
if (node.data) {
const lastSection = sections[sections.length - 1]
node.data = null
if (Object.keys(node.children).length === 0) {
const parentNode = node.parent
delete parentNode[lastSection]
parentNode.wildcardChildNode = null
parentNode.placeholderChildNode = null
}
success = true
}
return success
}
}
module.exports = RadixRouter