Skip to content

Commit c4c2836

Browse files
committed
Refactor to ignore iterator return values
1 parent 73f7279 commit c4c2836

2 files changed

Lines changed: 9 additions & 167 deletions

File tree

lib/node_modules/@stdlib/iter/strided-by/lib/main.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimit
2626
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
2727
var isFunction = require( '@stdlib/assert/is-function' );
2828
var isIteratorLike = require( '@stdlib/assert/is-iterator-like' );
29-
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
3029
var iteratorSymbol = require( '@stdlib/symbol/iterator' );
3130

3231

@@ -81,6 +80,7 @@ function iterStridedBy( iterator, fcn, offset, eager, thisArg ) {
8180
var FLG;
8281
var idx;
8382
var ctx;
83+
var ret;
8484
var o;
8585
var v;
8686
var i;
@@ -129,6 +129,7 @@ function iterStridedBy( iterator, fcn, offset, eager, thisArg ) {
129129
v = iterator.next();
130130
if ( v.done ) {
131131
FLG = true;
132+
ret = v;
132133
break;
133134
}
134135
}
@@ -160,9 +161,13 @@ function iterStridedBy( iterator, fcn, offset, eager, thisArg ) {
160161
* @returns {Object} iterator protocol-compliant object
161162
*/
162163
function next() {
163-
var out;
164164
var v;
165165
if ( FLG ) {
166+
if ( ret ) {
167+
v = ret;
168+
ret = null;
169+
return v;
170+
}
166171
return {
167172
'done': true
168173
};
@@ -181,23 +186,15 @@ function iterStridedBy( iterator, fcn, offset, eager, thisArg ) {
181186
v = iterator.next();
182187
if ( v.done ) {
183188
FLG = true;
184-
out = {};
185-
if ( hasOwnProp( v, 'value' ) ) {
186-
out.value = v.value;
187-
}
188-
out.done = true;
189-
return out;
189+
return v;
190190
}
191191
s += 1;
192192
stride = fcn.call( ctx, v.value, i, s, stride );
193193
if ( !isPositiveInteger( stride ) ) {
194194
throw new TypeError( 'invalid return value. Callback function must return a positive integer. Value: `' + stride + '`.' );
195195
}
196196
idx += stride;
197-
return {
198-
'value': v.value,
199-
'done': false
200-
};
197+
return v;
201198
}
202199

203200
/**

lib/node_modules/@stdlib/iter/strided-by/test/test.js

Lines changed: 0 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,6 @@ var noop = require( '@stdlib/utils/noop' );
2929
var iterStridedBy = 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-
6332
// TESTS //
6433

6534
tape( 'main export is a function', function test( t ) {
@@ -417,40 +386,6 @@ tape( 'the function returns an iterator protocol-compliant object having a `next
417386
}
418387
});
419388

420-
tape( 'the function returns an iterator protocol-compliant object having a `next` method which throws an error if a provided callback function does not return a positive integer value (value+done)', function test( t ) {
421-
var values;
422-
var i;
423-
424-
values = [
425-
'5',
426-
-5,
427-
0,
428-
3.14,
429-
NaN,
430-
true,
431-
false,
432-
null,
433-
void 0,
434-
[],
435-
{},
436-
function noop() {}
437-
];
438-
for ( i = 0; i < values.length; i++ ) {
439-
t.throws( badValue( values[i] ), TypeError, 'throws an error when a callback returns '+values[i] );
440-
}
441-
t.end();
442-
443-
function badValue( value ) {
444-
function clbk() {
445-
return value;
446-
}
447-
return function badValue() {
448-
var it = iterStridedBy( createIterator( [ 1, 2 ] ), clbk );
449-
it.next();
450-
};
451-
}
452-
});
453-
454389
tape( 'the function returns an iterator protocol-compliant object', function test( t ) {
455390
var it;
456391
var r;
@@ -471,48 +406,6 @@ tape( 'the function returns an iterator protocol-compliant object', function tes
471406
}
472407
});
473408

474-
tape( 'the function returns an iterator protocol-compliant object (value+done)', function test( t ) {
475-
var expected;
476-
var values;
477-
var actual;
478-
var it;
479-
var i;
480-
481-
values = [ 1, 2, 3, 4 ];
482-
expected = [
483-
{
484-
'value': 1,
485-
'done': false
486-
},
487-
{
488-
'value': 2,
489-
'done': false
490-
},
491-
{
492-
'value': 3,
493-
'done': false
494-
},
495-
{
496-
'value': 4,
497-
'done': true
498-
}
499-
];
500-
501-
it = iterStridedBy( createIterator( values ), stride );
502-
t.equal( it.next.length, 0, 'has zero arity' );
503-
504-
actual = [];
505-
for ( i = 0; i < values.length; i++ ) {
506-
actual.push( it.next() );
507-
}
508-
t.deepEqual( actual, expected, 'returns expected values' );
509-
t.end();
510-
511-
function stride() {
512-
return 1;
513-
}
514-
});
515-
516409
tape( 'the function returns an iterator protocol-compliant object which steps according to a callback function', function test( t ) {
517410
var expected;
518411
var values;
@@ -838,54 +731,6 @@ tape( 'the function supports specifying the callback execution context (offset+e
838731
}
839732
});
840733

841-
tape( 'the function supports specifying the callback execution context (value+done)', function test( t ) {
842-
var expected;
843-
var values;
844-
var actual;
845-
var ctx;
846-
var it;
847-
var i;
848-
849-
ctx = {
850-
'count': 0
851-
};
852-
values = [ 1, 2, 3, 4 ];
853-
expected = [
854-
{
855-
'value': 1,
856-
'done': false
857-
},
858-
{
859-
'value': 2,
860-
'done': false
861-
},
862-
{
863-
'value': 3,
864-
'done': false
865-
},
866-
{
867-
'value': 4,
868-
'done': true
869-
}
870-
];
871-
872-
it = iterStridedBy( createIterator( values ), stride, ctx );
873-
t.equal( it.next.length, 0, 'has zero arity' );
874-
875-
actual = [];
876-
for ( i = 0; i < expected.length; i++ ) {
877-
actual.push( it.next() );
878-
}
879-
t.deepEqual( actual, expected, 'returns expected values' );
880-
t.equal( ctx.count, 3, 'returns expected value' );
881-
t.end();
882-
883-
function stride() {
884-
this.count += 1; // eslint-disable-line no-invalid-this
885-
return 1;
886-
}
887-
});
888-
889734
tape( 'the returned iterator has a `return` method for closing an iterator (no argument)', function test( t ) {
890735
var it;
891736
var r;

0 commit comments

Comments
 (0)