Skip to content

Commit dce60a1

Browse files
committed
Refactor to ignore iterator return values
1 parent 36b2a11 commit dce60a1

File tree

2 files changed

+5
-313
lines changed

2 files changed

+5
-313
lines changed

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

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-propert
2424
var isFunction = require( '@stdlib/assert/is-function' );
2525
var isIteratorLike = require( '@stdlib/assert/is-iterator-like' );
2626
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
27-
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
2827
var iteratorSymbol = require( '@stdlib/symbol/iterator' );
2928

3029

@@ -106,9 +105,8 @@ function iterReplicateBy( iterator, fcn, thisArg ) {
106105
* @returns {Object} iterator protocol-compliant object
107106
*/
108107
function next() {
109-
var out;
110108
var v;
111-
if ( FLG === 2 ) {
109+
if ( FLG ) {
112110
return {
113111
'done': true
114112
};
@@ -117,40 +115,12 @@ function iterReplicateBy( iterator, fcn, thisArg ) {
117115
t += 1;
118116
i += 1;
119117
if ( i >= n ) {
120-
if ( FLG === 1 ) {
121-
// If we are here, we have finished the last of the replicates...
122-
FLG = 2;
123-
return {
124-
'done': true
125-
};
126-
}
127118
v = iterator.next();
128119
if ( v.done ) {
129-
out = {};
130-
if ( hasOwnProp( v, 'value' ) ) {
131-
// We *may* still need to replicate one more value...
132-
value = v.value; // cached value
133-
j += 1;
134-
n = fcn.call( thisArg, value, j, t ); // number of replicates
135-
if ( !isInteger( n ) ) {
136-
throw new TypeError( 'invalid return value. Callback function must return an integer. Value: `' + n + '`.' );
137-
}
138-
if ( n > 0 ) {
139-
out.value = value;
140-
out.done = false;
141-
FLG = 1;
142-
i = 0; // reset the counter
143-
} else {
144-
out.done = true;
145-
FLG = 2;
146-
}
147-
} else {
148-
out.done = true;
149-
FLG = 2;
150-
}
151-
return out;
120+
FLG = true;
121+
return v;
152122
}
153-
value = v.value; // cached value
123+
value = v.value; // cache value
154124
j += 1;
155125
n = fcn.call( thisArg, value, j, t );
156126
if ( !isInteger( n ) ) {
@@ -180,7 +150,7 @@ function iterReplicateBy( iterator, fcn, thisArg ) {
180150
* @returns {Object} iterator protocol-compliant object
181151
*/
182152
function end( value ) {
183-
FLG = 2;
153+
FLG = true;
184154
if ( arguments.length ) {
185155
return {
186156
'value': value,

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

Lines changed: 0 additions & 278 deletions
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,6 @@ var noop = require( '@stdlib/utils/noop' );
2929
var iterReplicateBy = 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 ) {
@@ -157,38 +126,6 @@ tape( 'the function returns an iterator protocol-compliant object having a `next
157126
}
158127
});
159128

160-
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 an integer value (value+done)', function test( t ) {
161-
var values;
162-
var i;
163-
164-
values = [
165-
'5',
166-
3.14,
167-
NaN,
168-
true,
169-
false,
170-
null,
171-
void 0,
172-
[],
173-
{},
174-
function noop() {}
175-
];
176-
for ( i = 0; i < values.length; i++ ) {
177-
t.throws( badValue( values[i] ), TypeError, 'throws an error when a callback returns '+values[i] );
178-
}
179-
t.end();
180-
181-
function badValue( value ) {
182-
function clbk() {
183-
return value;
184-
}
185-
return function badValue() {
186-
var it = iterReplicateBy( createIterator( [ 1 ] ), clbk );
187-
it.next();
188-
};
189-
}
190-
});
191-
192129
tape( 'the function returns an iterator protocol-compliant object (infinite iterator)', function test( t ) {
193130
var actual;
194131
var it;
@@ -623,170 +560,6 @@ tape( 'the function returns an iterator protocol-compliant object which replicat
623560
}
624561
});
625562

626-
tape( 'the function returns an iterator protocol-compliant object which replicates each iterated value according to a provided function (value+done; >1)', function test( t ) {
627-
var expected;
628-
var values;
629-
var actual;
630-
var it;
631-
var i;
632-
633-
values = [ 1, 2, 3, 4 ];
634-
expected = [
635-
{
636-
'value': 1,
637-
'done': false
638-
},
639-
{
640-
'value': 1,
641-
'done': false
642-
},
643-
{
644-
'value': 2,
645-
'done': false
646-
},
647-
{
648-
'value': 2,
649-
'done': false
650-
},
651-
{
652-
'value': 3,
653-
'done': false
654-
},
655-
{
656-
'value': 3,
657-
'done': false
658-
},
659-
{
660-
'value': 4,
661-
'done': false
662-
},
663-
{
664-
'value': 4,
665-
'done': false
666-
},
667-
{
668-
'done': true
669-
}
670-
];
671-
672-
it = iterReplicateBy( createIterator( values ), fcn );
673-
t.equal( it.next.length, 0, 'has zero arity' );
674-
675-
actual = [];
676-
for ( i = 0; i < expected.length; i++ ) {
677-
actual.push( it.next() );
678-
}
679-
t.deepEqual( actual, expected, 'returns expected values' );
680-
t.end();
681-
682-
function fcn() {
683-
return 2;
684-
}
685-
});
686-
687-
tape( 'the function returns an iterator protocol-compliant object which replicates each iterated value according to a provided function (value+done; once)', function test( t ) {
688-
var expected;
689-
var values;
690-
var actual;
691-
var it;
692-
var i;
693-
694-
values = [ 1, 2, 3, 4 ];
695-
expected = [
696-
{
697-
'value': 1,
698-
'done': false
699-
},
700-
{
701-
'value': 2,
702-
'done': false
703-
},
704-
{
705-
'value': 3,
706-
'done': false
707-
},
708-
{
709-
'value': 4,
710-
'done': false
711-
},
712-
{
713-
'done': true
714-
}
715-
];
716-
717-
it = iterReplicateBy( createIterator( values ), fcn );
718-
t.equal( it.next.length, 0, 'has zero arity' );
719-
720-
actual = [];
721-
for ( i = 0; i < expected.length; i++ ) {
722-
actual.push( it.next() );
723-
}
724-
t.deepEqual( actual, expected, 'returns expected values' );
725-
t.end();
726-
727-
function fcn() {
728-
return 1;
729-
}
730-
});
731-
732-
tape( 'the function returns an iterator protocol-compliant object which replicates each iterated value according to a provided function (value+done; none)', function test( t ) {
733-
var expected;
734-
var values;
735-
var actual;
736-
var it;
737-
var i;
738-
739-
values = [ 1, 2, 3, 4 ];
740-
expected = [
741-
{
742-
'done': true
743-
}
744-
];
745-
746-
it = iterReplicateBy( createIterator( values ), fcn );
747-
t.equal( it.next.length, 0, 'has zero arity' );
748-
749-
actual = [];
750-
for ( i = 0; i < expected.length; i++ ) {
751-
actual.push( it.next() );
752-
}
753-
t.deepEqual( actual, expected, 'returns expected values' );
754-
t.end();
755-
756-
function fcn() {
757-
return 0;
758-
}
759-
});
760-
761-
tape( 'the function returns an iterator protocol-compliant object which replicates each iterated value according to a provided function (value+done; none)', function test( t ) {
762-
var expected;
763-
var values;
764-
var actual;
765-
var it;
766-
var i;
767-
768-
values = [ 1, 2, 3, 4 ];
769-
expected = [
770-
{
771-
'done': true
772-
}
773-
];
774-
775-
it = iterReplicateBy( createIterator( values ), fcn );
776-
t.equal( it.next.length, 0, 'has zero arity' );
777-
778-
actual = [];
779-
for ( i = 0; i < expected.length; i++ ) {
780-
actual.push( it.next() );
781-
}
782-
t.deepEqual( actual, expected, 'returns expected values' );
783-
t.end();
784-
785-
function fcn( v, i ) {
786-
return -i;
787-
}
788-
});
789-
790563
tape( 'the function supports specifying the callback execution context', function test( t ) {
791564
var expected;
792565
var values;
@@ -838,57 +611,6 @@ tape( 'the function supports specifying the callback execution context', functio
838611
}
839612
});
840613

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': false
869-
},
870-
{
871-
'done': true
872-
}
873-
];
874-
875-
it = iterReplicateBy( createIterator( values ), fcn, ctx );
876-
t.equal( it.next.length, 0, 'has zero arity' );
877-
878-
actual = [];
879-
for ( i = 0; i < expected.length; i++ ) {
880-
actual.push( it.next() );
881-
}
882-
t.deepEqual( actual, expected, 'returns expected values' );
883-
t.equal( ctx.count, 4, 'returns expected value' );
884-
t.end();
885-
886-
function fcn() {
887-
this.count += 1; // eslint-disable-line no-invalid-this
888-
return 1;
889-
}
890-
});
891-
892614
tape( 'the returned iterator has a `return` method for closing an iterator (no argument)', function test( t ) {
893615
var it;
894616
var r;

0 commit comments

Comments
 (0)