Skip to content

Commit c8d22f6

Browse files
committed
add counter argument to iterator helpers
tc39/proposal-iterator-helpers#211
1 parent 3e18cd0 commit c8d22f6

26 files changed

+69
-40
lines changed

packages/core-js/internals/iterator-create-proxy.js

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ module.exports = function (nextHandler, IS_ITERATOR) {
7575
} else state = record;
7676
state.type = ITERATOR_PROXY;
7777
state.nextHandler = nextHandler;
78+
state.counter = 0;
7879
state.done = false;
7980
setInternalState(this, state);
8081
};

packages/core-js/modules/esnext.iterator.every.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ var getIteratorDirect = require('../internals/get-iterator-direct');
88
$({ target: 'Iterator', proto: true, real: true, forced: true }, {
99
every: function every(fn) {
1010
var record = getIteratorDirect(this);
11+
var counter = 0;
1112
aCallable(fn);
1213
return !iterate(record, function (value, stop) {
13-
if (!fn(value)) return stop();
14+
if (!fn(value, counter++)) return stop();
1415
}, { IS_RECORD: true, INTERRUPTED: true }).stopped;
1516
}
1617
});

packages/core-js/modules/esnext.iterator.filter.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var IteratorProxy = createIteratorProxy(function () {
1818
done = this.done = !!result.done;
1919
if (done) return;
2020
value = result.value;
21-
if (callWithSafeIterationClosing(iterator, filterer, value)) return value;
21+
if (callWithSafeIterationClosing(iterator, filterer, [value, this.counter++], true)) return value;
2222
}
2323
});
2424

packages/core-js/modules/esnext.iterator.find.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ var getIteratorDirect = require('../internals/get-iterator-direct');
88
$({ target: 'Iterator', proto: true, real: true, forced: true }, {
99
find: function find(fn) {
1010
var record = getIteratorDirect(this);
11+
var counter = 0;
1112
aCallable(fn);
1213
return iterate(record, function (value, stop) {
13-
if (fn(value)) return stop(value);
14+
if (fn(value, counter++)) return stop(value);
1415
}, { IS_RECORD: true, INTERRUPTED: true }).result;
1516
}
1617
});

packages/core-js/modules/esnext.iterator.flat-map.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var IteratorProxy = createIteratorProxy(function () {
2828
if (this.done = !!result.done) return;
2929

3030
try {
31-
mapped = mapper(result.value);
31+
mapped = mapper(result.value, this.counter++);
3232
iteratorMethod = getIteratorMethod(mapped);
3333

3434
if (!iteratorMethod) {

packages/core-js/modules/esnext.iterator.for-each.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22
// https://github.com/tc39/proposal-iterator-helpers
33
var $ = require('../internals/export');
44
var iterate = require('../internals/iterate');
5+
var aCallable = require('../internals/a-callable');
56
var getIteratorDirect = require('../internals/get-iterator-direct');
67

78
$({ target: 'Iterator', proto: true, real: true, forced: true }, {
89
forEach: function forEach(fn) {
9-
iterate(getIteratorDirect(this), fn, { IS_RECORD: true });
10+
var record = getIteratorDirect(this);
11+
var counter = 0;
12+
aCallable(fn);
13+
iterate(record, function (value) {
14+
fn(value, counter++);
15+
}, { IS_RECORD: true });
1016
}
1117
});

packages/core-js/modules/esnext.iterator.indexed.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// TODO: Remove from `core-js@4`
12
// https://github.com/tc39/proposal-iterator-helpers
23
var $ = require('../internals/export');
34
var indexed = require('../internals/iterator-indexed');

packages/core-js/modules/esnext.iterator.map.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var IteratorProxy = createIteratorProxy(function () {
1212
var iterator = this.iterator;
1313
var result = anObject(call(this.next, iterator));
1414
var done = this.done = !!result.done;
15-
if (!done) return callWithSafeIterationClosing(iterator, this.mapper, result.value);
15+
if (!done) return callWithSafeIterationClosing(iterator, this.mapper, [result.value, this.counter++], true);
1616
});
1717

1818
$({ target: 'Iterator', proto: true, real: true, forced: true }, {

packages/core-js/modules/esnext.iterator.reduce.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ $({ target: 'Iterator', proto: true, real: true, forced: true }, {
1313
aCallable(reducer);
1414
var noInitial = arguments.length < 2;
1515
var accumulator = noInitial ? undefined : arguments[1];
16+
var counter = 0;
1617
iterate(record, function (value) {
1718
if (noInitial) {
1819
noInitial = false;
1920
accumulator = value;
2021
} else {
21-
accumulator = reducer(accumulator, value);
22+
accumulator = reducer(accumulator, value, counter);
2223
}
24+
counter++;
2325
}, { IS_RECORD: true });
2426
if (noInitial) throw $TypeError('Reduce of empty iterator with no initial value');
2527
return accumulator;

packages/core-js/modules/esnext.iterator.some.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ var getIteratorDirect = require('../internals/get-iterator-direct');
88
$({ target: 'Iterator', proto: true, real: true, forced: true }, {
99
some: function some(fn) {
1010
var record = getIteratorDirect(this);
11+
var counter = 0;
1112
aCallable(fn);
1213
return iterate(record, function (value, stop) {
13-
if (fn(value)) return stop();
14+
if (fn(value, counter++)) return stop();
1415
}, { IS_RECORD: true, INTERRUPTED: true }).stopped;
1516
}
1617
});

tests/pure/esnext.iterator.every.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ QUnit.test('Iterator#every', assert => {
1212

1313
assert.true(every.call(createIterator([1, 2, 3]), it => typeof it == 'number'), 'basic functionality #1');
1414
assert.false(every.call(createIterator([1, 2, 3]), it => it % 2), 'basic functionality #2');
15-
every.call(createIterator([1]), function (arg) {
15+
every.call(createIterator([1]), function (arg, counter) {
1616
assert.same(this, STRICT_THIS, 'this');
17-
assert.same(arguments.length, 1, 'arguments length');
17+
assert.same(arguments.length, 2, 'arguments length');
1818
assert.same(arg, 1, 'argument');
19+
assert.same(counter, 0, 'counter');
1920
});
2021

2122
assert.throws(() => every.call(undefined, () => { /* empty */ }), TypeError);

tests/pure/esnext.iterator.filter.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ QUnit.test('Iterator#filter', assert => {
1111
assert.nonEnumerable(Iterator.prototype, 'filter');
1212

1313
assert.arrayEqual(filter.call(createIterator([1, 2, 3]), it => it % 2).toArray(), [1, 3], 'basic functionality');
14-
filter.call(createIterator([1]), function (arg) {
14+
filter.call(createIterator([1]), function (arg, counter) {
1515
assert.same(this, STRICT_THIS, 'this');
16-
assert.same(arguments.length, 1, 'arguments length');
16+
assert.same(arguments.length, 2, 'arguments length');
1717
assert.same(arg, 1, 'argument');
18+
assert.same(counter, 0, 'counter');
1819
}).toArray();
1920

2021
assert.throws(() => filter.call(undefined, () => { /* empty */ }), TypeError);

tests/pure/esnext.iterator.find.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ QUnit.test('Iterator#find', assert => {
1111
assert.nonEnumerable(Iterator.prototype, 'find');
1212

1313
assert.same(find.call(createIterator([1, 2, 3]), it => !(it % 2)), 2, 'basic functionality');
14-
find.call(createIterator([1]), function (arg) {
14+
find.call(createIterator([1]), function (arg, counter) {
1515
assert.same(this, STRICT_THIS, 'this');
16-
assert.same(arguments.length, 1, 'arguments length');
16+
assert.same(arguments.length, 2, 'arguments length');
1717
assert.same(arg, 1, 'argument');
18+
assert.same(counter, 0, 'counter');
1819
});
1920

2021
assert.throws(() => find.call(undefined, () => { /* empty */ }), TypeError);

tests/pure/esnext.iterator.flat-map.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ QUnit.test('Iterator#flatMap', assert => {
1515
[-1, -2, 3, 4, 5, 6, 'a', 'b'],
1616
'basic functionality',
1717
);
18-
flatMap.call(createIterator([1]), function (arg) {
18+
flatMap.call(createIterator([1]), function (arg, counter) {
1919
assert.same(this, STRICT_THIS, 'this');
20-
assert.same(arguments.length, 1, 'arguments length');
20+
assert.same(arguments.length, 2, 'arguments length');
2121
assert.same(arg, 1, 'argument');
22+
assert.same(counter, 0, 'counter');
2223
return [arg];
2324
}).toArray();
2425

tests/pure/esnext.iterator.for-each.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ QUnit.test('Iterator#forEach', assert => {
1616

1717
assert.arrayEqual(array, [1, 2, 3], 'basic functionality');
1818

19-
forEach.call(createIterator([1]), function (arg) {
19+
forEach.call(createIterator([1]), function (arg, counter) {
2020
assert.same(this, STRICT_THIS, 'this');
21-
assert.same(arguments.length, 1, 'arguments length');
21+
assert.same(arguments.length, 2, 'arguments length');
2222
assert.same(arg, 1, 'argument');
23+
assert.same(counter, 0, 'counter');
2324
});
2425

2526
assert.throws(() => forEach.call(undefined, () => { /* empty */ }), TypeError);

tests/pure/esnext.iterator.map.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ QUnit.test('Iterator#map', assert => {
1111
assert.nonEnumerable(Iterator.prototype, 'map');
1212

1313
assert.arrayEqual(map.call(createIterator([1, 2, 3]), it => it ** 2).toArray(), [1, 4, 9], 'basic functionality');
14-
map.call(createIterator([1]), function (arg) {
14+
map.call(createIterator([1]), function (arg, counter) {
1515
assert.same(this, STRICT_THIS, 'this');
16-
assert.same(arguments.length, 1, 'arguments length');
16+
assert.same(arguments.length, 2, 'arguments length');
1717
assert.same(arg, 1, 'argument');
18+
assert.same(counter, 0, 'counter');
1819
}).toArray();
1920

2021
assert.throws(() => map.call(undefined, () => { /* empty */ }), TypeError);

tests/pure/esnext.iterator.reduce.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ QUnit.test('Iterator#reduce', assert => {
1212

1313
assert.same(reduce.call(createIterator([1, 2, 3]), (a, b) => a + b, 1), 7, 'basic functionality');
1414
assert.same(reduce.call(createIterator([1, 2, 3]), (a, b) => a + b), 6, 'basic functionality, no init');
15-
reduce.call(createIterator([2]), function (a, b) {
15+
reduce.call(createIterator([2]), function (a, b, counter) {
1616
assert.same(this, STRICT_THIS, 'this');
17-
assert.same(arguments.length, 2, 'arguments length');
17+
assert.same(arguments.length, 3, 'arguments length');
1818
assert.same(a, 1, 'argument 1');
1919
assert.same(b, 2, 'argument 2');
20+
assert.same(counter, 0, 'counter');
2021
}, 1);
2122

2223
assert.throws(() => reduce.call(undefined, (a, b) => a + b, 0), TypeError);

tests/pure/esnext.iterator.some.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ QUnit.test('Iterator#some', assert => {
1212

1313
assert.true(some.call(createIterator([1, 2, 3]), it => it % 2), 'basic functionality #1');
1414
assert.false(some.call(createIterator([1, 2, 3]), it => typeof it == 'string'), 'basic functionality #2');
15-
some.call(createIterator([1]), function (arg) {
15+
some.call(createIterator([1]), function (arg, counter) {
1616
assert.same(this, STRICT_THIS, 'this');
17-
assert.same(arguments.length, 1, 'arguments length');
17+
assert.same(arguments.length, 2, 'arguments length');
1818
assert.same(arg, 1, 'argument');
19+
assert.same(counter, 0, 'counter');
1920
});
2021

2122
assert.throws(() => some.call(undefined, () => { /* empty */ }), TypeError);

tests/tests/esnext.iterator.every.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ QUnit.test('Iterator#every', assert => {
1212

1313
assert.true(every.call(createIterator([1, 2, 3]), it => typeof it == 'number'), 'basic functionality #1');
1414
assert.false(every.call(createIterator([1, 2, 3]), it => it % 2), 'basic functionality #2');
15-
every.call(createIterator([1]), function (arg) {
15+
every.call(createIterator([1]), function (arg, counter) {
1616
assert.same(this, STRICT_THIS, 'this');
17-
assert.same(arguments.length, 1, 'arguments length');
17+
assert.same(arguments.length, 2, 'arguments length');
1818
assert.same(arg, 1, 'argument');
19+
assert.same(counter, 0, 'counter');
1920
});
2021

2122
assert.throws(() => every.call(undefined, () => { /* empty */ }), TypeError);

tests/tests/esnext.iterator.filter.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ QUnit.test('Iterator#filter', assert => {
1111
assert.nonEnumerable(Iterator.prototype, 'filter');
1212

1313
assert.arrayEqual(filter.call(createIterator([1, 2, 3]), it => it % 2).toArray(), [1, 3], 'basic functionality');
14-
filter.call(createIterator([1]), function (arg) {
14+
filter.call(createIterator([1]), function (arg, counter) {
1515
assert.same(this, STRICT_THIS, 'this');
16-
assert.same(arguments.length, 1, 'arguments length');
16+
assert.same(arguments.length, 2, 'arguments length');
1717
assert.same(arg, 1, 'argument');
18+
assert.same(counter, 0, 'counter');
1819
});
1920

2021
assert.throws(() => filter.call(undefined, () => { /* empty */ }), TypeError);

tests/tests/esnext.iterator.find.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ QUnit.test('Iterator#find', assert => {
1111
assert.nonEnumerable(Iterator.prototype, 'find');
1212

1313
assert.same(find.call(createIterator([1, 2, 3]), it => !(it % 2)), 2, 'basic functionality');
14-
find.call(createIterator([1]), function (arg) {
14+
find.call(createIterator([1]), function (arg, counter) {
1515
assert.same(this, STRICT_THIS, 'this');
16-
assert.same(arguments.length, 1, 'arguments length');
16+
assert.same(arguments.length, 2, 'arguments length');
1717
assert.same(arg, 1, 'argument');
18+
assert.same(counter, 0, 'counter');
1819
});
1920

2021
assert.throws(() => find.call(undefined, () => { /* empty */ }), TypeError);

tests/tests/esnext.iterator.flat-map.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ QUnit.test('Iterator#flatMap', assert => {
1515
[-1, -2, 3, 4, 5, 6, 'a', 'b'],
1616
'basic functionality',
1717
);
18-
flatMap.call(createIterator([1]), function (arg) {
18+
flatMap.call(createIterator([1]), function (arg, counter) {
1919
assert.same(this, STRICT_THIS, 'this');
20-
assert.same(arguments.length, 1, 'arguments length');
20+
assert.same(arguments.length, 2, 'arguments length');
2121
assert.same(arg, 1, 'argument');
22+
assert.same(counter, 0, 'counter');
2223
return [arg];
2324
}).toArray();
2425

tests/tests/esnext.iterator.for-each.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ QUnit.test('Iterator#forEach', assert => {
1616

1717
assert.arrayEqual(array, [1, 2, 3], 'basic functionality');
1818

19-
forEach.call(createIterator([1]), function (arg) {
19+
forEach.call(createIterator([1]), function (arg, counter) {
2020
assert.same(this, STRICT_THIS, 'this');
21-
assert.same(arguments.length, 1, 'arguments length');
21+
assert.same(arguments.length, 2, 'arguments length');
2222
assert.same(arg, 1, 'argument');
23+
assert.same(counter, 0, 'counter');
2324
});
2425

2526
assert.throws(() => forEach.call(undefined, () => { /* empty */ }), TypeError);

tests/tests/esnext.iterator.map.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ QUnit.test('Iterator#map', assert => {
1111
assert.nonEnumerable(Iterator.prototype, 'map');
1212

1313
assert.arrayEqual(map.call(createIterator([1, 2, 3]), it => it ** 2).toArray(), [1, 4, 9], 'basic functionality');
14-
map.call(createIterator([1]), function (arg) {
14+
map.call(createIterator([1]), function (arg, counter) {
1515
assert.same(this, STRICT_THIS, 'this');
16-
assert.same(arguments.length, 1, 'arguments length');
16+
assert.same(arguments.length, 2, 'arguments length');
1717
assert.same(arg, 1, 'argument');
18+
assert.same(counter, 0, 'counter');
1819
}).toArray();
1920

2021
assert.throws(() => map.call(undefined, () => { /* empty */ }), TypeError);

tests/tests/esnext.iterator.reduce.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ QUnit.test('Iterator#reduce', assert => {
1212

1313
assert.same(reduce.call(createIterator([1, 2, 3]), (a, b) => a + b, 1), 7, 'basic functionality');
1414
assert.same(reduce.call(createIterator([1, 2, 3]), (a, b) => a + b), 6, 'basic functionality, no init');
15-
reduce.call(createIterator([2]), function (a, b) {
15+
reduce.call(createIterator([2]), function (a, b, counter) {
1616
assert.same(this, STRICT_THIS, 'this');
17-
assert.same(arguments.length, 2, 'arguments length');
17+
assert.same(arguments.length, 3, 'arguments length');
1818
assert.same(a, 1, 'argument 1');
1919
assert.same(b, 2, 'argument 2');
20+
assert.same(counter, 0, 'counter');
2021
}, 1);
2122

2223
assert.throws(() => reduce.call(undefined, (a, b) => a + b, 0), TypeError);

tests/tests/esnext.iterator.some.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ QUnit.test('Iterator#some', assert => {
1212

1313
assert.true(some.call(createIterator([1, 2, 3]), it => it % 2), 'basic functionality #1');
1414
assert.false(some.call(createIterator([1, 2, 3]), it => typeof it == 'string'), 'basic functionality #2');
15-
some.call(createIterator([1]), function (arg) {
15+
some.call(createIterator([1]), function (arg, counter) {
1616
assert.same(this, STRICT_THIS, 'this');
17-
assert.same(arguments.length, 1, 'arguments length');
17+
assert.same(arguments.length, 2, 'arguments length');
1818
assert.same(arg, 1, 'argument');
19+
assert.same(counter, 0, 'counter');
1920
});
2021

2122
assert.throws(() => some.call(undefined, () => { /* empty */ }), TypeError);

0 commit comments

Comments
 (0)