@@ -29,32 +29,30 @@ DeepIterator.prototype.next = function () {
29
29
// eslint-disable-next-line default-case
30
30
switch ( this . _direction ) {
31
31
case DOWNWARD :
32
- this . _tree . _middle = this . _tree . _middle . _force ( ) ;
33
- if ( this . _tree . _middle instanceof Deep ) {
34
- this . _treeStack . push ( this . _tree ) ;
35
- this . _tree = this . _tree . _middle ;
36
- this . _downwardStep ( ) ;
32
+ // eslint-disable-next-line no-case-declarations
33
+ const tree = this . _treeStack . pop ( ) . _force ( ) ;
34
+ if ( tree instanceof Deep ) {
35
+ this . _downwardStep ( tree ) ;
37
36
++ this . _currentLevel ;
38
- this . _level = this . _stack . map ( ( ) => this . _currentLevel ) ;
37
+ this . _treeStack . push ( tree , tree . _middle ) ;
39
38
break ;
40
39
} else {
40
+ assert ( this . _treeStack . length >= 1 ) ;
41
41
this . _direction = UPWARD ;
42
- if ( this . _tree . _middle instanceof Single ) {
43
- this . _stack = [ this . _tree . _middle . a ] ;
44
- this . _level = [ this . _currentLevel + 1 ] ;
42
+ if ( tree instanceof Single ) {
43
+ this . _stack . push ( tree . a ) ;
44
+ this . _level . push ( this . _currentLevel ) ;
45
45
break ;
46
46
}
47
47
48
- assert ( this . _tree . _middle instanceof Empty ) ;
48
+ assert ( tree instanceof Empty ) ;
49
49
}
50
50
51
51
case UPWARD :
52
- if ( this . _currentLevel === - 1 ) return { done : true } ;
53
- assert ( this . _tree instanceof Deep ) ;
52
+ if ( this . _currentLevel === 0 ) return { done : true } ;
53
+ assert ( this . _treeStack [ this . _treeStack . length - 1 ] instanceof Deep ) ;
54
+ -- this . _currentLevel ;
54
55
this . _upwardStep ( ) ;
55
- this . _level = this . _stack . map ( ( ) => this . _currentLevel ) ;
56
-
57
- this . _tree = -- this . _currentLevel === - 1 ? null : this . _treeStack . pop ( ) ;
58
56
}
59
57
/* eslint-enable no-fallthrough */
60
58
}
0 commit comments