@@ -29,6 +29,37 @@ var iteratorSymbol = require( '@stdlib/symbol/iterator' );
29
29
var iterSlice = require ( './../lib' ) ;
30
30
31
31
32
+ // FUNCTIONS //
33
+
34
+ function createIterator ( arr ) {
35
+ var len ;
36
+ var it ;
37
+ var i ;
38
+
39
+ len = arr . length ;
40
+ i = - 1 ;
41
+
42
+ it = { } ;
43
+ it . next = next ;
44
+
45
+ return it ;
46
+
47
+ function next ( ) {
48
+ var out ;
49
+ i += 1 ;
50
+ if ( i < len ) {
51
+ out = { } ;
52
+ out . value = arr [ i ] ;
53
+ out . done = ( i === len - 1 ) ;
54
+ return out ;
55
+ }
56
+ return {
57
+ 'done' : true
58
+ } ;
59
+ }
60
+ }
61
+
62
+
32
63
// TESTS //
33
64
34
65
tape ( 'main export is a function' , function test ( t ) {
@@ -265,11 +296,9 @@ tape( 'the function returns an iterator protocol-compliant object (finite iterat
265
296
t . equal ( it . next . length , 0 , 'has zero arity' ) ;
266
297
267
298
actual = [ ] ;
268
- for ( i = 0 ; i < values . length ; i ++ ) {
299
+ for ( i = 0 ; i < expected . length ; i ++ ) {
269
300
actual . push ( it . next ( ) ) ;
270
301
}
271
- actual . push ( it . next ( ) ) ;
272
-
273
302
t . deepEqual ( actual , expected , 'returns expected values' ) ;
274
303
t . end ( ) ;
275
304
} ) ;
@@ -308,11 +337,9 @@ tape( 'the function returns an iterator protocol-compliant object (begin+<last>)
308
337
t . equal ( it . next . length , 0 , 'has zero arity' ) ;
309
338
310
339
actual = [ ] ;
311
- for ( i = 0 ; i < 4 ; i ++ ) {
340
+ for ( i = 0 ; i < expected . length ; i ++ ) {
312
341
actual . push ( it . next ( ) ) ;
313
342
}
314
- actual . push ( it . next ( ) ) ;
315
-
316
343
t . deepEqual ( actual , expected , 'returns expected values' ) ;
317
344
t . end ( ) ;
318
345
} ) ;
@@ -351,11 +378,42 @@ tape( 'the function returns an iterator protocol-compliant object (begin+end)',
351
378
t . equal ( it . next . length , 0 , 'has zero arity' ) ;
352
379
353
380
actual = [ ] ;
354
- for ( i = 0 ; i < 4 ; i ++ ) {
381
+ for ( i = 0 ; i < expected . length ; i ++ ) {
355
382
actual . push ( it . next ( ) ) ;
356
383
}
357
- actual . push ( it . next ( ) ) ;
384
+ t . deepEqual ( actual , expected , 'returns expected values' ) ;
385
+ t . end ( ) ;
386
+ } ) ;
358
387
388
+ tape ( 'the function returns an iterator protocol-compliant object (begin+end)' , function test ( t ) {
389
+ var expected ;
390
+ var values ;
391
+ var actual ;
392
+ var it ;
393
+ var i ;
394
+
395
+ values = [ 1 , 2 , 3 , 4 ] ;
396
+ expected = [
397
+ {
398
+ 'value' : 2 ,
399
+ 'done' : false
400
+ } ,
401
+ {
402
+ 'value' : 3 ,
403
+ 'done' : false
404
+ } ,
405
+ {
406
+ 'done' : true
407
+ }
408
+ ] ;
409
+
410
+ it = iterSlice ( array2iterator ( values ) , 1 , 3 ) ;
411
+ t . equal ( it . next . length , 0 , 'has zero arity' ) ;
412
+
413
+ actual = [ ] ;
414
+ for ( i = 0 ; i < expected . length ; i ++ ) {
415
+ actual . push ( it . next ( ) ) ;
416
+ }
359
417
t . deepEqual ( actual , expected , 'returns expected values' ) ;
360
418
t . end ( ) ;
361
419
} ) ;
@@ -390,7 +448,7 @@ tape( 'the function returns an iterator protocol-compliant object which returns
390
448
t . equal ( it . next . length , 0 , 'has zero arity' ) ;
391
449
392
450
actual = [ ] ;
393
- for ( i = 0 ; i < 4 ; i ++ ) {
451
+ for ( i = 0 ; i < expected . length ; i ++ ) {
394
452
actual . push ( it . next ( ) ) ;
395
453
}
396
454
t . deepEqual ( actual , expected , 'returns expected values' ) ;
@@ -415,7 +473,7 @@ tape( 'the function returns an "empty" iterator if the third argument is less th
415
473
t . equal ( it . next . length , 0 , 'has zero arity' ) ;
416
474
417
475
actual = [ ] ;
418
- for ( i = 0 ; i < 1 ; i ++ ) {
476
+ for ( i = 0 ; i < expected . length ; i ++ ) {
419
477
actual . push ( it . next ( ) ) ;
420
478
}
421
479
t . deepEqual ( actual , expected , 'returns expected values' ) ;
@@ -440,7 +498,7 @@ tape( 'the function returns an "empty" iterator if the third argument is less th
440
498
t . equal ( it . next . length , 0 , 'has zero arity' ) ;
441
499
442
500
actual = [ ] ;
443
- for ( i = 0 ; i < 1 ; i ++ ) {
501
+ for ( i = 0 ; i < expected . length ; i ++ ) {
444
502
actual . push ( it . next ( ) ) ;
445
503
}
446
504
t . deepEqual ( actual , expected , 'returns expected values' ) ;
@@ -478,39 +536,11 @@ tape( 'the function returns an iterator protocol-compliant object (value+done)',
478
536
t . equal ( it . next . length , 0 , 'has zero arity' ) ;
479
537
480
538
actual = [ ] ;
481
- for ( i = 0 ; i < values . length - 3 ; i ++ ) {
539
+ for ( i = 0 ; i < expected . length ; i ++ ) {
482
540
actual . push ( it . next ( ) ) ;
483
541
}
484
542
t . deepEqual ( actual , expected , 'returns expected values' ) ;
485
543
t . end ( ) ;
486
-
487
- function createIterator ( arr ) {
488
- var len ;
489
- var it ;
490
- var i ;
491
-
492
- len = arr . length ;
493
- i = - 1 ;
494
-
495
- it = { } ;
496
- it . next = next ;
497
-
498
- return it ;
499
-
500
- function next ( ) {
501
- var out ;
502
- i += 1 ;
503
- if ( i < len ) {
504
- out = { } ;
505
- out . value = arr [ i ] ;
506
- out . done = ( i === len - 1 ) ;
507
- return out ;
508
- }
509
- return {
510
- 'done' : true
511
- } ;
512
- }
513
- }
514
544
} ) ;
515
545
516
546
tape ( 'the returned iterator has a `return` method for closing an iterator (no argument)' , function test ( t ) {
0 commit comments