Skip to content

Commit 92e1bfc

Browse files
committed
Fix variable initialization, fix docs, and update tests
1 parent d566346 commit 92e1bfc

File tree

3 files changed

+72
-42
lines changed

3 files changed

+72
-42
lines changed

lib/node_modules/@stdlib/iter/slice/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ var bool = it.next().done;
103103
// returns true
104104
```
105105

106-
If `end` is greater than or equal to `begin`, the returned [iterator][mdn-iterator-protocol] does not return any iterated values.
106+
If `begin` is greater than or equal to `end`, the returned [iterator][mdn-iterator-protocol] does not return any iterated values.
107107

108108
```javascript
109109
var array2iterator = require( '@stdlib/array/to-iterator' );

lib/node_modules/@stdlib/iter/slice/lib/main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ function iterSlice( iterator, begin, end ) {
9191
M = END;
9292
}
9393
n = M - N;
94-
i = 0;
94+
i = -1;
9595

9696
// Create an iterator protocol-compliant object:
9797
iter = {};

lib/node_modules/@stdlib/iter/slice/test/test.js

+70-40
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,37 @@ var iteratorSymbol = require( '@stdlib/symbol/iterator' );
2929
var iterSlice = require( './../lib' );
3030

3131

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+
3263
// TESTS //
3364

3465
tape( 'main export is a function', function test( t ) {
@@ -265,11 +296,9 @@ tape( 'the function returns an iterator protocol-compliant object (finite iterat
265296
t.equal( it.next.length, 0, 'has zero arity' );
266297

267298
actual = [];
268-
for ( i = 0; i < values.length; i++ ) {
299+
for ( i = 0; i < expected.length; i++ ) {
269300
actual.push( it.next() );
270301
}
271-
actual.push( it.next() );
272-
273302
t.deepEqual( actual, expected, 'returns expected values' );
274303
t.end();
275304
});
@@ -308,11 +337,9 @@ tape( 'the function returns an iterator protocol-compliant object (begin+<last>)
308337
t.equal( it.next.length, 0, 'has zero arity' );
309338

310339
actual = [];
311-
for ( i = 0; i < 4; i++ ) {
340+
for ( i = 0; i < expected.length; i++ ) {
312341
actual.push( it.next() );
313342
}
314-
actual.push( it.next() );
315-
316343
t.deepEqual( actual, expected, 'returns expected values' );
317344
t.end();
318345
});
@@ -351,11 +378,42 @@ tape( 'the function returns an iterator protocol-compliant object (begin+end)',
351378
t.equal( it.next.length, 0, 'has zero arity' );
352379

353380
actual = [];
354-
for ( i = 0; i < 4; i++ ) {
381+
for ( i = 0; i < expected.length; i++ ) {
355382
actual.push( it.next() );
356383
}
357-
actual.push( it.next() );
384+
t.deepEqual( actual, expected, 'returns expected values' );
385+
t.end();
386+
});
358387

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+
}
359417
t.deepEqual( actual, expected, 'returns expected values' );
360418
t.end();
361419
});
@@ -390,7 +448,7 @@ tape( 'the function returns an iterator protocol-compliant object which returns
390448
t.equal( it.next.length, 0, 'has zero arity' );
391449

392450
actual = [];
393-
for ( i = 0; i < 4; i++ ) {
451+
for ( i = 0; i < expected.length; i++ ) {
394452
actual.push( it.next() );
395453
}
396454
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
415473
t.equal( it.next.length, 0, 'has zero arity' );
416474

417475
actual = [];
418-
for ( i = 0; i < 1; i++ ) {
476+
for ( i = 0; i < expected.length; i++ ) {
419477
actual.push( it.next() );
420478
}
421479
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
440498
t.equal( it.next.length, 0, 'has zero arity' );
441499

442500
actual = [];
443-
for ( i = 0; i < 1; i++ ) {
501+
for ( i = 0; i < expected.length; i++ ) {
444502
actual.push( it.next() );
445503
}
446504
t.deepEqual( actual, expected, 'returns expected values' );
@@ -478,39 +536,11 @@ tape( 'the function returns an iterator protocol-compliant object (value+done)',
478536
t.equal( it.next.length, 0, 'has zero arity' );
479537

480538
actual = [];
481-
for ( i = 0; i < values.length-3; i++ ) {
539+
for ( i = 0; i < expected.length; i++ ) {
482540
actual.push( it.next() );
483541
}
484542
t.deepEqual( actual, expected, 'returns expected values' );
485543
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-
}
514544
});
515545

516546
tape( 'the returned iterator has a `return` method for closing an iterator (no argument)', function test( t ) {

0 commit comments

Comments
 (0)