Skip to content

Commit 83ff16d

Browse files
Iterate over the previous and next siblings
1 parent 2004889 commit 83ff16d

File tree

2 files changed

+139
-1
lines changed

2 files changed

+139
-1
lines changed

lib/SymbolTree.js

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ class SymbolTree {
351351
}
352352

353353
/**
354-
* Iterate over all children of the given object to an array.
354+
* Iterate over all children of the given object
355355
*
356356
* `O(1)` for a single iteration
357357
*
@@ -390,6 +390,86 @@ class SymbolTree {
390390
return iterator;
391391
}
392392

393+
/**
394+
* Iterate over all the previous siblings of the given object. (in reverse tree order)
395+
*
396+
* `O(1)` for a single iteration
397+
*
398+
* @method prevSiblingsIterator
399+
* @memberOf module:symbol-tree#
400+
* @param {Object} object
401+
* @return {Object} An iterable iterator (ES6)
402+
*/
403+
prevSiblingsIterator(object) {
404+
const _node = this._node.bind(this);
405+
406+
let nextObject = _node(object).prev;
407+
const iterator = {};
408+
409+
iterator.next = function() {
410+
if (!nextObject) {
411+
return {
412+
done : true,
413+
value : object
414+
};
415+
}
416+
417+
const value = nextObject;
418+
nextObject = _node(nextObject).prev;
419+
420+
return {
421+
done : false,
422+
value : value
423+
};
424+
};
425+
426+
iterator[Symbol.iterator] = function() {
427+
return iterator;
428+
};
429+
430+
return iterator;
431+
}
432+
433+
/**
434+
* Iterate over all the next siblings of the given object. (in tree order)
435+
*
436+
* `O(1)` for a single iteration
437+
*
438+
* @method nextSiblingsIterator
439+
* @memberOf module:symbol-tree#
440+
* @param {Object} object
441+
* @return {Object} An iterable iterator (ES6)
442+
*/
443+
nextSiblingsIterator(object) {
444+
const _node = this._node.bind(this);
445+
446+
let nextObject = _node(object).next;
447+
const iterator = {};
448+
449+
iterator.next = function() {
450+
if (!nextObject) {
451+
return {
452+
done : true,
453+
value : object
454+
};
455+
}
456+
457+
const value = nextObject;
458+
nextObject = _node(nextObject).next;
459+
460+
return {
461+
done : false,
462+
value : value
463+
};
464+
};
465+
466+
iterator[Symbol.iterator] = function() {
467+
return iterator;
468+
};
469+
470+
return iterator;
471+
}
472+
393473
/**
394474
* Iterate over all inclusive ancestors of the given object
395475
*

test/SymbolTree.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,64 @@ test('children iterator return value using a generator', function(t) {
724724
t.end();
725725
});
726726

727+
test('prev sibling iterator', function(t) {
728+
const tree = new SymbolTree();
729+
const a = {};
730+
const aa = {};
731+
const ab = {};
732+
const aba = {};
733+
const ac = {};
734+
const ad = {};
735+
const ae = {};
736+
const b = {};
737+
738+
tree.insertLast(aa, a);
739+
tree.insertLast(ab, a);
740+
tree.insertLast(aba, ab);
741+
tree.insertLast(ac, a);
742+
tree.insertLast(ad, a);
743+
tree.insertLast(ae, a);
744+
tree.insertAfter(b, a);
745+
746+
const results = [];
747+
748+
for (const object of tree.prevSiblingsIterator(ad)) {
749+
results.push(object);
750+
}
751+
t.deepEqual([ac, ab, aa], results);
752+
753+
t.end();
754+
});
755+
756+
test('next sibling iterator', function(t) {
757+
const tree = new SymbolTree();
758+
const a = {};
759+
const aa = {};
760+
const ab = {};
761+
const aba = {};
762+
const ac = {};
763+
const ad = {};
764+
const ae = {};
765+
const b = {};
766+
767+
tree.insertLast(aa, a);
768+
tree.insertLast(ab, a);
769+
tree.insertLast(aba, ab);
770+
tree.insertLast(ac, a);
771+
tree.insertLast(ad, a);
772+
tree.insertLast(ae, a);
773+
tree.insertAfter(b, a);
774+
775+
const results = [];
776+
777+
for (const object of tree.nextSiblingsIterator(ab)) {
778+
results.push(object);
779+
}
780+
t.deepEqual([ac, ad, ae], results);
781+
782+
t.end();
783+
});
784+
727785
test('ancestorsToArray', function(t) {
728786
const tree = new SymbolTree();
729787
const a = {};

0 commit comments

Comments
 (0)