File tree Expand file tree Collapse file tree 2 files changed +31
-3
lines changed Expand file tree Collapse file tree 2 files changed +31
-3
lines changed Original file line number Diff line number Diff line change @@ -358,12 +358,14 @@ class SymbolTree {
358
358
* @method childrenIterator
359
359
* @memberOf module:symbol-tree#
360
360
* @param {Object } parent
361
+ * @param {Boolean } [reverse=false]
361
362
* @return {Object } An iterable iterator (ES6)
362
363
*/
363
- childrenIterator ( parent ) {
364
+ childrenIterator ( parent , reverse ) {
364
365
const _node = this . _node . bind ( this ) ;
365
366
366
- let nextObject = this . _node ( parent ) . first ;
367
+ let nextObject = this . _node ( parent ) [ reverse ? 'last' : 'first' ] ;
368
+
367
369
const iterator = { } ;
368
370
369
371
iterator . next = function ( ) {
@@ -375,7 +377,7 @@ class SymbolTree {
375
377
}
376
378
377
379
const value = nextObject ;
378
- nextObject = _node ( nextObject ) . next ;
380
+ nextObject = _node ( nextObject ) [ reverse ? 'prev' : ' next' ] ;
379
381
380
382
return {
381
383
done : false ,
Original file line number Diff line number Diff line change @@ -698,6 +698,32 @@ test('children iterator', function(t) {
698
698
t . end ( ) ;
699
699
} ) ;
700
700
701
+ test ( 'children iterator reverse' , function ( t ) {
702
+ const tree = new SymbolTree ( ) ;
703
+ const a = { } ;
704
+ const aa = { } ;
705
+ const ab = { } ;
706
+ const aba = { } ;
707
+ const ac = { } ;
708
+ const b = { } ;
709
+
710
+ tree . insertLast ( aa , a ) ;
711
+ tree . insertLast ( ab , a ) ;
712
+ tree . insertLast ( aba , ab ) ;
713
+ tree . insertLast ( ac , a ) ;
714
+ tree . insertAfter ( b , a ) ;
715
+
716
+ const results = [ ] ;
717
+
718
+ for ( const object of tree . childrenIterator ( a , true ) ) {
719
+ results . push ( object ) ;
720
+ }
721
+ t . deepEqual ( [ ac , ab , aa ] , results ) ;
722
+
723
+ t . end ( ) ;
724
+ } ) ;
725
+
726
+
701
727
test ( 'children iterator return value using a generator' , function ( t ) {
702
728
const tree = new SymbolTree ( ) ;
703
729
const a = { } ;
You can’t perform that action at this time.
0 commit comments