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 {
358358 * @method childrenIterator
359359 * @memberOf module:symbol-tree#
360360 * @param {Object } parent
361+ * @param {Boolean } [reverse=false]
361362 * @return {Object } An iterable iterator (ES6)
362363 */
363- childrenIterator ( parent ) {
364+ childrenIterator ( parent , reverse ) {
364365 const _node = this . _node . bind ( this ) ;
365366
366- let nextObject = this . _node ( parent ) . first ;
367+ let nextObject = this . _node ( parent ) [ reverse ? 'last' : 'first' ] ;
368+
367369 const iterator = { } ;
368370
369371 iterator . next = function ( ) {
@@ -375,7 +377,7 @@ class SymbolTree {
375377 }
376378
377379 const value = nextObject ;
378- nextObject = _node ( nextObject ) . next ;
380+ nextObject = _node ( nextObject ) [ reverse ? 'prev' : ' next' ] ;
379381
380382 return {
381383 done : false ,
Original file line number Diff line number Diff line change @@ -698,6 +698,32 @@ test('children iterator', function(t) {
698698 t . end ( ) ;
699699} ) ;
700700
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+
701727test ( 'children iterator return value using a generator' , function ( t ) {
702728 const tree = new SymbolTree ( ) ;
703729 const a = { } ;
You can’t perform that action at this time.
0 commit comments