-
Notifications
You must be signed in to change notification settings - Fork 3
/
t.js
456 lines (412 loc) · 13.7 KB
/
t.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// version 0.5.1 ([source](https://github.com/aaronj1335/t-js))
//
// t-js is freely distributable under the MIT license
//
// <a href="https://travis-ci.org/aaronj1335/t-js" target=_blank>
// <img src="https://api.travis-ci.org/aaronj1335/t-js.png?branch=master">
// </a>
//
// overview
// ========
// t.js is a tree-traversal library. its only assumption is that the trees it
// traverses are made up of objects with 'children' arrays:
//
// {
// children: [
// { },
// {
// children: [
// { },
// { }
// ]
// }
// ]
// }
//
// the actual property name is configurable. the traversals are entirely
// non-recursive, including the post-order traversal and `map()` functions,
// and it works inside the browser or out.
//
// testing
// -------
// there's a bunch of tests in `test/test.js`. you can run them along with the
// linter with:
//
// $ npm install && npm test
//
// or view them on most any system with a modern browser by opening the
// `index.html` file.
//
// documentation is generated with the `grunt docs` target.
//
(function() {
// usage
// -----
// the `t` interface is exported in either the browser or node.js. the library
// can be installed from [npm](http://search.npmjs.org/#/t):
//
// $ npm install t
//
var _dfsPostOrder,
t = {},
root = this,
isArray = function(o) {
return Object.prototype.toString.call(o) === '[object Array]';
},
getChildrenName = function (config) {
return config.childrenName || 'children';
};
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports)
exports = module.exports = t;
exports.t = t;
} else {
root.t = t;
}
// available functions
// ===================
// t.bfs()
// -------
// perform a breadth-first search, executing the given callback at each node.
//
// t.bfs(node, [config], function(node, par, ctrl) {
// /* ... */
// })
//
// - `node`:
// object where the search will start. this could also be an array of
// objects
// - `config`:
// you can define the name of the children property with
// `config.childrenName` (shoutout to @GianlucaGuarini)
// - `callback` (last argument):
// function to be executed at each node. the arguments are:
// - `node`: the current node
// - `par`: the current node's parent
// - `ctrl`: control object. this doesn't currently do anything.
//
// returns: the first `node` argument
//
t.bfs = function(node) {
var cur, callback, i, length, par, children,
isConfigSet = arguments.length === 3,
config = isConfigSet ? arguments[1] : {},
queue = isArray(node)? node.slice(0) : [node],
parents = [undefined],
childrenName = getChildrenName(config);
if (node == null) return node;
if (arguments.length >= 3) {
config = arguments[1];
callback = arguments[2];
} else {
config = {};
callback = arguments[1];
}
while (queue.length) {
cur = queue.shift();
par = parents.shift();
callback.call(cur, cur, par);
children = cur[childrenName] || [];
for (i = 0, length = children.length; i < length; i++) {
queue.push(children[i]);
parents.push(cur);
}
}
return node;
};
// t.dfs()
// -------
// perform a depth-first search, executing the given callback at each node.
//
// t.dfs(node, [config], function(node, par, ctrl) {
// /* ... */
// })
//
// in the pre-order case, `dfs()` doesn't process child nodes until after the
// callback. so if you need to traverse an unknown tree, say a directory
// structure, you can start with just the root, and add child nodes as you go
// by appending them to `this.children` in the callback function.
//
// - `node`:
// object where the search will start. this could also be an array of
// objects
// - `config`:
// if this is an object w/ the 'order' property set to 'post', a
// post-order traversal will be performed. this is generally worse
// performance, but the `callback` has access to the return values of its
// child nodes. you can define the name of the children property with
// `config.childrenName`
// - `callback` (last argument):
// function to be executed at each node. the arguments are:
// - `node`: the current node
// - `par`: the current node's parent
// - `ctrl`: control object. setting the `stop` property of this will end
// the search, setting the `cutoff` property of this will not visit any
// children of this node
// - `ret`: return values of child nodes. this is only set if `dfs()` is
// called with the `order` property set to `post`.
//
// returns: the first `node` argument
//
t.dfs = function(node) {
var cur, par, children, ctrl, i, ret,
isConfigSet = arguments.length === 3,
nodes = isArray(node)? node.slice(0).reverse() : [node],
config = isConfigSet ? arguments[1] : {},
callback = arguments[isConfigSet ? 2 : 1],
parents = [],
childrenName = getChildrenName(config);
if (typeof nodes[0] === 'undefined' && nodes.length === 1) return;
if (config.order === 'post') {
ret = _dfsPostOrder(nodes, config, callback);
return isArray(node)? ret : ret[0];
}
for (i = nodes.length-1; i >= 0; i--)
parents.push(undefined);
while (nodes.length > 0) {
cur = nodes.pop();
par = parents.pop();
ctrl = {};
callback.call(cur, cur, par, ctrl);
if (ctrl.stop) break;
children = (cur && cur[childrenName])? cur[childrenName] : [];
for (i = ctrl.cutoff? -1 : children.length-1; i >= 0; i--) {
nodes.push(children[i]);
parents.push(cur);
}
}
return node;
};
// t.map()
// -------
// given a tree, return a tree of the same structure made up of the objects
// returned by the callback which is executed at each node. think of the
// `underscore`'s `_.map()` function, or python's `map()`
//
// t.map(node, [config], function(node, par) {
// /* ... */
// })
//
// - `node`:
// object where the traversal will start. this could also be an array of
// objects
// - `config`:
// you can define the name of the children property with
// `config.childrenName`
// - `callback` (last argument):
// function to be executed at each node. this must return an object. the
// `map` function takes care of setting children. the arguments are:
// - `node`: the current node
// - `par`: the current node's parent. note that this is the parent from
// the new tree that's being created.
//
// returns: a new tree, mapped by the callback function
//
t.map = function() {
var node = arguments[0],
isConfigSet = arguments.length === 3,
config = isConfigSet ? arguments[1] : {},
filter = config.filter,
nodeFactory = arguments[isConfigSet ? 2 : 1],
ret = isArray(node)? [] : undefined,
last = function(l) { return l[l.length-1]; },
parentStack = [],
childrenName = getChildrenName(config);
t.dfs(node, config, function(n, par, ctrl) {
var curParent = last(parentStack),
newNode = nodeFactory(n, curParent? curParent.ret : undefined);
if (filter && ! newNode) {
ctrl.cutoff = true;
if (curParent && n === last(curParent.n[childrenName])) {
parentStack.pop();
if (curParent.ret[childrenName] &&
! curParent.ret[childrenName].length)
delete curParent.ret[childrenName];
}
return;
}
if (! par) {
if (isArray(node))
ret.push(newNode);
else
ret = newNode;
} else {
curParent.ret[childrenName].push(newNode);
if (n === last(curParent.n[childrenName])) {
parentStack.pop();
if (curParent.ret[childrenName] &&
! curParent.ret[childrenName].length)
delete curParent.ret[childrenName];
}
}
if (n[childrenName] && n[childrenName].length) {
newNode[childrenName] = [];
parentStack.push({n: n, ret: newNode});
}
});
return ret;
};
// t.filter()
// ----------
// given a tree, return a tree of the same structure made up of the objects
// returned by the callback which is executed at each node. if, however, at a
// given node the callback returns a falsy value, then the current node and all
// of its descendents will be pruned from the output tree.
//
// t.filter(node, [config], function(node, par) {
// /* ... */
// })
//
// - `node`:
// object where the traversal will start. this could also be an array of
// objects
// - `config`:
// you can define the name of the children property with
// `config.childrenName`
// - `callback` (last argument):
// function to be executed at each node. this must return an object or a
// falsy value if the output tree should be pruned from the current node
// down. the `filter` function takes care of setting children. the
// arguments are:
// - `node`: the current node
// - `par`: the current node's parent. note that this is the parent from
// the new tree that's being created.
//
// returns: a new tree, filtered by the callback function
//
t.filter = function(node) {
var isConfigSet = arguments.length === 3,
nodeFactory = arguments[isConfigSet ? 2 : 1],
config = isConfigSet ? arguments[1] : {};
return t.map(node, {
filter: true,
childrenName: config.childrenName
}, nodeFactory);
};
// t.stroll()
// ----------
//
// _a walk through the trees..._
//
// given two trees of similar structure, traverse both trees at the same time,
// executing the given callback with the pair of corresponding nodes as
// arguments.
//
// t.stroll(tree1, tree2, [config], function(node1, node2) {
// /* ... */
// })
//
// - `tree1`:
// the first tree of the traversal
// - `node2`:
// the second tree of the traversal
// - `config`:
// you can define the name of the children property with
// `config.childrenName`
// - `callback` (last argument):
// function to be executed at each node. the arguments are:
// - `node1`: the node from the first tree
// - `node2`: the node from the second tree
//
t.stroll = function(tree1, tree2) {
var i, node2,
isConfigSet = arguments.length === 4,
callback = arguments[ isConfigSet ? 3 : 2],
config = isConfigSet ? arguments[2] : {},
childrenName = getChildrenName(config),
nodes2 = isArray(tree2)? tree2.slice(0).reverse() : [tree2],
len = function(a) { return typeof a === 'undefined'? 0 : a.length; };
t.dfs(tree1, config, function(node1, par, ctrl) {
node2 = nodes2.pop();
callback(node1, node2);
if (node1 && node2 &&
len(node1[childrenName]) === len(node2[childrenName]))
for (i = (node2[childrenName] || []).length-1; i >= 0; i--)
nodes2.push(node2[childrenName][i]);
else
ctrl.cutoff = true;
});
};
// t.find()
// ----------
//
// given a tree and a truth test, return the first node that responds with a
// truthy value
//
// t.find(tree, [config], function(node, par) {
// /* ... */
// })
//
// - `tree`:
// the tree in which to find the node
// - `config`:
// you can define the name of the children property with
// `config.childrenName`
// - `callback` (last argument):
// function to be executed at each node. if this function returns a truthy
// value, the traversal will stop and `find` will return the current node.
// the arguments are:
// - `node`: the current node
// - `par`: the parent of the current node
//
// returns: the found node
//
t.find = function( tree ) {
var found,
isConfigSet = arguments.length === 3,
callback = arguments[ isConfigSet ? 2 : 1],
config = isConfigSet ? arguments[1] : {};
t.dfs(tree, config, function(node, par, ctrl) {
if (callback.call(node, node, par)) {
ctrl.stop = true;
found = this;
}
});
return found;
};
// _dfsPostOrder()
// -----------------
//
// this is a module-private function used by `dfs()`
_dfsPostOrder = function(nodes, config, callback) {
var cur, par, ctrl, node,
last = function(l) { return l[l.length-1]; },
ret = [],
stack = [{
node: nodes.pop(),
index: 0,
ret: []
}],
childrenName = getChildrenName(config);
while (stack.length) {
cur = last(stack);
node = cur.node;
if (node[childrenName] && node[childrenName].length) {
if (cur.index < node[childrenName].length) {
stack.push({
node: node[childrenName][cur.index++],
index: 0,
ret: []
});
continue;
}
}
ctrl = {};
par = stack[stack.length-2];
if (par) {
par.ret.push(callback.call(node, node, par.node, ctrl, cur.ret));
stack.pop();
} else {
ret.push(callback.call(node, node, undefined, ctrl, cur.ret));
stack.pop();
if (nodes.length)
stack.push({
node: nodes.pop(),
index: 0,
ret: []
});
}
}
return ret;
};
}());