File tree Expand file tree Collapse file tree 3 files changed +9
-48
lines changed
lib/node_modules/@stdlib/iter/length Expand file tree Collapse file tree 3 files changed +9
-48
lines changed Original file line number Diff line number Diff line change @@ -38,23 +38,27 @@ function createIterator( arr ) {
3838
3939 it = { } ;
4040 it . next = next ;
41+ it . reset = reset ;
4142
4243 return it ;
4344
4445 function next ( ) {
4546 i += 1 ;
46- if ( i < len - 1 ) {
47+ if ( i < len ) {
4748 return {
4849 'value' : arr [ i ] ,
4950 'done' : false
5051 } ;
5152 }
5253 i = - 1 ; // reset index
5354 return {
54- 'value' : arr [ len - 1 ] ,
5555 'done' : true
5656 } ;
5757 }
58+
59+ function reset ( ) {
60+ i = - 1 ;
61+ }
5862}
5963
6064
@@ -73,6 +77,7 @@ bench( pkg, function benchmark( b ) {
7377 if ( isnan ( v ) ) {
7478 b . fail ( 'should not be NaN' ) ;
7579 }
80+ arr . reset ( ) ;
7681 }
7782 b . toc ( ) ;
7883 if ( isnan ( v ) ) {
@@ -103,6 +108,7 @@ bench( pkg+'::loop', function benchmark( b ) {
103108 if ( count !== values . length ) {
104109 b . fail ( 'unexpected result' ) ;
105110 }
111+ arr . reset ( ) ;
106112 }
107113 b . toc ( ) ;
108114 if ( count !== values . length ) {
Original file line number Diff line number Diff line change 2121// MODULES //
2222
2323var isIteratorLike = require ( '@stdlib/assert/is-iterator-like' ) ;
24- var hasOwnProp = require ( '@stdlib/assert/has-own-property' ) ;
2524
2625
2726// MAIN //
@@ -51,15 +50,10 @@ function iterLength( iterator ) {
5150 while ( true ) {
5251 v = iterator . next ( ) ;
5352 if ( v . done ) {
54- if ( hasOwnProp ( v , 'value' ) ) {
55- count += 1 ;
56- return count ;
57- }
58- break ;
53+ return count ;
5954 }
6055 count += 1 ;
6156 }
62- return count ;
6357}
6458
6559
Original file line number Diff line number Diff line change @@ -83,42 +83,3 @@ tape( 'the function returns the iterator length', function test( t ) {
8383 t . strictEqual ( v , 5 , 'returns expected value' ) ;
8484 t . end ( ) ;
8585} ) ;
86-
87- tape ( 'the function returns the iterator length (value+done)' , function test ( t ) {
88- var arr ;
89- var v ;
90-
91- arr = createIterator ( [ 0 , 0 , 1 ] ) ;
92- v = iterLength ( arr ) ;
93-
94- t . strictEqual ( v , 3 , 'returns expected value' ) ;
95- t . end ( ) ;
96-
97- function createIterator ( arr ) {
98- var len ;
99- var it ;
100- var i ;
101-
102- len = arr . length ;
103- i = - 1 ;
104-
105- it = { } ;
106- it . next = next ;
107-
108- return it ;
109-
110- function next ( ) {
111- var out ;
112- i += 1 ;
113- if ( i < len ) {
114- out = { } ;
115- out . value = arr [ i ] ;
116- out . done = ( i === len - 1 ) ;
117- return out ;
118- }
119- return {
120- 'done' : true
121- } ;
122- }
123- }
124- } ) ;
You can’t perform that action at this time.
0 commit comments