Skip to content

Commit 1abf50e

Browse files
treeToArray: Use a filter function argument instead of includeRoot
1 parent 225c7bb commit 1abf50e

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

lib/SymbolTree.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
const SymbolTreeNode = require('./SymbolTreeNode');
99

10+
function returnTrue() {
11+
return true;
12+
}
13+
1014
class SymbolTree {
1115

1216
/**
@@ -262,20 +266,27 @@ class SymbolTree {
262266
* @memberOf module:symbol-tree#
263267
* @param {Object} root
264268
* @param {Object[]} [array=[]]
265-
* @param {Boolean} [includeRoot=true] Include the given `root` object in the array?
269+
* @param {Function} [filter] Function to test each object before it is added to the array.
270+
* Invoked with arguments (object). Should return `true` if an object
271+
* is to be included.
272+
* @param {*} [thisArg] Value to use as `this` when executing `filter`.
266273
* @return {Object[]}
267274
*/
268-
treeToArray(root, array, includeRoot) {
275+
treeToArray(root, array, filter, thisArg) {
269276
if (!array) {
270277
array = [];
271278
}
272279

273-
let object = includeRoot || includeRoot === undefined
274-
? root
275-
: this.following(root, root);
280+
if (!filter) {
281+
filter = returnTrue;
282+
}
283+
284+
let object = root;
276285

277286
while (object) {
278-
array.push(object);
287+
if (filter.call(thisArg, object)) {
288+
array.push(object);
289+
}
279290
object = this.following(object, root);
280291
}
281292

test/SymbolTree.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,11 +654,45 @@ test('treeToArray', function(t) {
654654
tree.insertAfter(b, a);
655655

656656
t.deepEqual([a, aa, ab, aba, abaa], tree.treeToArray(a));
657-
t.deepEqual([aa, ab, aba, abaa], tree.treeToArray(a, null, false));
658657

659658
const arr = ['a', 5];
660659
tree.treeToArray(a, arr);
661660
t.deepEqual(['a', 5, a, aa, ab, aba, abaa], arr);
662661

663662
t.end();
664663
});
664+
665+
test('treeToArray with filter', function(t) {
666+
const tree = new SymbolTree();
667+
const a = {};
668+
const aa = {};
669+
const ab = {};
670+
const aba = {};
671+
const abaa = {};
672+
const b = {};
673+
674+
tree.insertLast(aa, a);
675+
tree.insertLast(ab, a);
676+
tree.insertLast(aba, ab);
677+
tree.insertLast(abaa, aba);
678+
tree.insertAfter(b, a);
679+
680+
const filter = function(object) {
681+
t.equal(this, undefined);
682+
683+
return object !== a && object !== aba;
684+
};
685+
686+
t.deepEqual([aa, ab, abaa], tree.treeToArray(a, null, filter));
687+
688+
const thisArg = {foo: 'bar'};
689+
const filterThis = function(object) {
690+
t.equal(this, thisArg);
691+
692+
return object !== a && object !== aba;
693+
};
694+
695+
t.deepEqual([aa, ab, abaa], tree.treeToArray(a, null, filterThis, thisArg));
696+
697+
t.end();
698+
});

0 commit comments

Comments
 (0)