Skip to content

Commit

Permalink
Merge pull request #1101 from zloirock/iterator-helpers
Browse files Browse the repository at this point in the history
Iterator helpers proposal changes
  • Loading branch information
zloirock committed Jul 24, 2022
2 parents 1f78646 + 5a2d421 commit c2620d2
Show file tree
Hide file tree
Showing 53 changed files with 595 additions and 288 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,17 @@
## Changelog
##### Unreleased
- Recent updates of the [iterator helpers proposal](https://github.com/tc39/proposal-iterator-helpers), [#1101](https://github.com/zloirock/core-js/issues/1101):
- `.asIndexedPairs` renamed to `.indexed`, [proposal-iterator-helpers/183](https://github.com/tc39/proposal-iterator-helpers/pull/183):
- `Iterator.prototype.asIndexedPairs` -> `Iterator.prototype.indexed`
- `AsyncIterator.prototype.asIndexedPairs` -> `AsyncIterator.prototype.indexed`
- Avoid exposing spec fiction `%AsyncFromSyncIteratorPrototype%` in `AsyncIterator.from` and `Iterator.prototype.toAsync`, [proposal-iterator-helpers/182](https://github.com/tc39/proposal-iterator-helpers/pull/182), [proposal-iterator-helpers/202](https://github.com/tc39/proposal-iterator-helpers/pull/202)
- Avoid unnecessary promise creation in `%WrapForValidAsyncIteratorPrototype%.next`, [proposal-iterator-helpers/197](https://github.com/tc39/proposal-iterator-helpers/pull/197)
- Do not validate value in `%WrapForValid(Async)IteratorPrototype%.next`, [proposal-iterator-helpers/197](https://github.com/tc39/proposal-iterator-helpers/pull/197) and [proposal-iterator-helpers/205](https://github.com/tc39/proposal-iterator-helpers/pull/205)
- Do not forward the parameter of `.next` / `.return` to an underlying iterator by the extended iterator protocol, a part of [proposal-iterator-helpers/194](https://github.com/tc39/proposal-iterator-helpers/pull/194)
- `.throw` methods removed from all wrappers / helpers prototypes, a part of [proposal-iterator-helpers/194](https://github.com/tc39/proposal-iterator-helpers/pull/194)
- Close inner iterators in `{ Iterator, AsyncIterator }.prototype.flatMap` on `.return`, [proposal-iterator-helpers/195](https://github.com/tc39/proposal-iterator-helpers/pull/195)
- Throw `RangeError` on `NaN` in `{ Iterator, AsyncIterator }.prototype.{ drop, take }`, [proposal-iterator-helpers/181](https://github.com/tc39/proposal-iterator-helpers/pull/181)
- Many other updates and fixes of this proposal
- Polyfill `Promise` with `unhandledrejection` event support (browser style) in Deno < [1.24](https://github.com/denoland/deno/releases/tag/v1.24.0)
- Added compat data for Bun, [#1103](https://github.com/zloirock/core-js/issues/1103)
- Added compat data for Hermes, [#1099](https://github.com/zloirock/core-js/issues/1099)
Expand Down
10 changes: 5 additions & 5 deletions README.md
Expand Up @@ -2180,17 +2180,17 @@ correctionNeeded; // => [1, 1, 3]
core-js(-pure)/stage/2
```
##### [Iterator helpers](https://github.com/tc39/proposal-iterator-helpers)[⬆](#index)
Modules [`esnext.async-iterator.constructor`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.constructor.js), [`esnext.async-iterator.as-indexed-pairs`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.as-indexed-pairs.js), [`esnext.async-iterator.drop`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.drop.js), [`esnext.async-iterator.every`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.every.js), [`esnext.async-iterator.filter`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.filter.js), [`esnext.async-iterator.find`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.find.js), [`esnext.async-iterator.flat-map`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.flat-map.js), [`esnext.async-iterator.for-each`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.for-each.js), [`esnext.async-iterator.from`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.from.js), [`esnext.async-iterator.map`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.map.js), [`esnext.async-iterator.reduce`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.reduce.js), [`esnext.async-iterator.some`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.some.js), [`esnext.async-iterator.take`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.take.js), [`esnext.async-iterator.to-array`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.to-array.js), [`esnext.iterator.constructor`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.constructor.js), [`esnext.iterator.as-indexed-pairs`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.as-indexed-pairs.js), [`esnext.iterator.drop`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.drop.js), [`esnext.iterator.every`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.every.js), [`esnext.iterator.filter`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.filter.js), [`esnext.iterator.find`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.find.js), [`esnext.iterator.flat-map`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.flat-map.js), [`esnext.iterator.for-each`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.for-each.js), [`esnext.iterator.from`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.from.js), [`esnext.iterator.map`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.map.js), [`esnext.iterator.reduce`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.reduce.js), [`esnext.iterator.some`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.some.js), [`esnext.iterator.take`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.take.js), [`esnext.iterator.to-array`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.to-array.js) and [`esnext.iterator.to-async`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.to-async.js)
Modules [`esnext.async-iterator.constructor`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.constructor.js), [`esnext.async-iterator.drop`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.drop.js), [`esnext.async-iterator.every`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.every.js), [`esnext.async-iterator.filter`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.filter.js), [`esnext.async-iterator.find`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.find.js), [`esnext.async-iterator.flat-map`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.flat-map.js), [`esnext.async-iterator.for-each`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.for-each.js), [`esnext.async-iterator.from`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.from.js), [`esnext.async-iterator.indexed`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.indexed.js), [`esnext.async-iterator.map`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.map.js), [`esnext.async-iterator.reduce`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.reduce.js), [`esnext.async-iterator.some`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.some.js), [`esnext.async-iterator.take`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.take.js), [`esnext.async-iterator.to-array`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.to-array.js), [`esnext.iterator.constructor`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.constructor.js), [`esnext.iterator.drop`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.drop.js), [`esnext.iterator.every`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.every.js), [`esnext.iterator.filter`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.filter.js), [`esnext.iterator.find`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.find.js), [`esnext.iterator.flat-map`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.flat-map.js), [`esnext.iterator.for-each`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.for-each.js), [`esnext.iterator.from`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.from.js), [`esnext.iterator.indexed`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.indexed.js), [`esnext.iterator.map`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.map.js), [`esnext.iterator.reduce`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.reduce.js), [`esnext.iterator.some`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.some.js), [`esnext.iterator.take`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.take.js), [`esnext.iterator.to-array`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.to-array.js) and [`esnext.iterator.to-async`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.to-async.js)
```js
class Iterator {
static from(iterable: Iterable<mixed>): Iterator<any>;
asIndexedPairs(): Iterator<[index, any]>;
drop(limit: uint): Iterator<any>;
every(callbackfn: value: any => boolean): boolean;
filter(callbackfn: value: any => boolean): Iterator<any>;
find(callbackfn: value: any => boolean)): any;
flatMap(callbackfn: value => any: Iterable): Iterator<any>;
forEach(callbackfn: value => void): void;
indexed(): Iterator<[index, any]>;
map(callbackfn: value => any): Iterator<any>;
reduce(callbackfn: (memo: any, value: any) => any, initialValue: any): any;
some(callbackfn: value: any => boolean): boolean;
Expand All @@ -2202,13 +2202,13 @@ class Iterator {
class AsyncIterator {
static from(iterable: Iterable<mixed>): AsyncIterator<any>;
asIndexedPairs(): AsyncIterator<[index, any]>;
drop(limit: uint): AsyncIterator<any>;
every(async callbackfn: value: any => boolean): Promise<boolean>;
filter(async callbackfn: value: any => boolean): AsyncIterator<any>;
find(async callbackfn: value: any => boolean)): Promise<any>;
flatMap(async callbackfn: value => any: Iterable): AsyncIterator<any>;
forEach(async callbackfn: value => void): Promise<void>;
indexed(): AsyncIterator<[index, any]>;
map(async callbackfn: value => any): AsyncIterator<any>;
reduce(async callbackfn: (memo: any, value: any) => any, initialValue: any): Promise<any>;
some(async callbackfn: value: any => boolean): Promise<boolean>;
Expand All @@ -2221,28 +2221,28 @@ class AsyncIterator {
```js
core-js/proposals/iterator-helpers
core-js(-pure)/full/async-iterator
core-js(-pure)/full/async-iterator/as-indexed-pairs
core-js(-pure)/full/async-iterator/drop
core-js(-pure)/full/async-iterator/every
core-js(-pure)/full/async-iterator/filter
core-js(-pure)/full/async-iterator/find
core-js(-pure)/full/async-iterator/flat-map
core-js(-pure)/full/async-iterator/for-each
core-js(-pure)/full/async-iterator/from
core-js(-pure)/full/async-iterator/indexed
core-js(-pure)/full/async-iterator/map
core-js(-pure)/full/async-iterator/reduce
core-js(-pure)/full/async-iterator/some
core-js(-pure)/full/async-iterator/take
core-js(-pure)/full/async-iterator/to-array
core-js(-pure)/full/iterator
core-js(-pure)/full/iterator/as-indexed-pairs
core-js(-pure)/full/iterator/drop
core-js(-pure)/full/iterator/every
core-js(-pure)/full/iterator/filter
core-js(-pure)/full/iterator/find
core-js(-pure)/full/iterator/flat-map
core-js(-pure)/full/iterator/for-each
core-js(-pure)/full/iterator/from
core-js(-pure)/full/iterator/indexed
core-js(-pure)/full/iterator/map
core-js(-pure)/full/iterator/reduce
core-js(-pure)/full/iterator/some
Expand Down
6 changes: 6 additions & 0 deletions packages/core-js-compat/src/data.mjs
Expand Up @@ -2080,6 +2080,7 @@ export const data = {
},
'esnext.async-iterator.constructor': {
},
// TODO: Remove from `core-js@4`
'esnext.async-iterator.as-indexed-pairs': {
},
'esnext.async-iterator.drop': {
Expand All @@ -2096,6 +2097,8 @@ export const data = {
},
'esnext.async-iterator.from': {
},
'esnext.async-iterator.indexed': {
},
'esnext.async-iterator.map': {
},
'esnext.async-iterator.reduce': {
Expand All @@ -2122,6 +2125,7 @@ export const data = {
'esnext.global-this': null,
'esnext.iterator.constructor': {
},
// TODO: Remove from `core-js@4`
'esnext.iterator.as-indexed-pairs': {
},
'esnext.iterator.drop': {
Expand All @@ -2138,6 +2142,8 @@ export const data = {
},
'esnext.iterator.from': {
},
'esnext.iterator.indexed': {
},
'esnext.iterator.map': {
},
'esnext.iterator.reduce': {
Expand Down
4 changes: 4 additions & 0 deletions packages/core-js-compat/src/modules-by-versions.mjs
Expand Up @@ -150,4 +150,8 @@ export default {
'esnext.array.group-to-map',
'esnext.symbol.metadata-key',
],
3.24: [
'esnext.async-iterator.indexed',
'esnext.iterator.indexed',
],
};
2 changes: 2 additions & 0 deletions packages/core-js/full/async-iterator/index.js
Expand Up @@ -3,6 +3,7 @@ require('../../modules/es.object.to-string');
require('../../modules/es.promise');
require('../../modules/es.string.iterator');
require('../../modules/esnext.async-iterator.constructor');
// TODO: Remove from `core-js@4`
require('../../modules/esnext.async-iterator.as-indexed-pairs');
require('../../modules/esnext.async-iterator.drop');
require('../../modules/esnext.async-iterator.every');
Expand All @@ -11,6 +12,7 @@ require('../../modules/esnext.async-iterator.find');
require('../../modules/esnext.async-iterator.flat-map');
require('../../modules/esnext.async-iterator.for-each');
require('../../modules/esnext.async-iterator.from');
require('../../modules/esnext.async-iterator.indexed');
require('../../modules/esnext.async-iterator.map');
require('../../modules/esnext.async-iterator.reduce');
require('../../modules/esnext.async-iterator.some');
Expand Down
8 changes: 8 additions & 0 deletions packages/core-js/full/async-iterator/indexed.js
@@ -0,0 +1,8 @@
require('../../modules/es.object.to-string');
require('../../modules/es.promise');
require('../../modules/esnext.async-iterator.constructor');
require('../../modules/esnext.async-iterator.indexed');

var entryUnbind = require('../../internals/entry-unbind');

module.exports = entryUnbind('AsyncIterator', 'indexed');
2 changes: 2 additions & 0 deletions packages/core-js/full/iterator/index.js
Expand Up @@ -3,6 +3,7 @@ require('../../modules/es.object.to-string');
require('../../modules/es.promise');
require('../../modules/es.string.iterator');
require('../../modules/esnext.iterator.constructor');
// TODO: Remove from `core-js@4`
require('../../modules/esnext.iterator.as-indexed-pairs');
require('../../modules/esnext.iterator.drop');
require('../../modules/esnext.iterator.every');
Expand All @@ -11,6 +12,7 @@ require('../../modules/esnext.iterator.find');
require('../../modules/esnext.iterator.flat-map');
require('../../modules/esnext.iterator.for-each');
require('../../modules/esnext.iterator.from');
require('../../modules/esnext.iterator.indexed');
require('../../modules/esnext.iterator.map');
require('../../modules/esnext.iterator.reduce');
require('../../modules/esnext.iterator.some');
Expand Down
8 changes: 8 additions & 0 deletions packages/core-js/full/iterator/indexed.js
@@ -0,0 +1,8 @@
require('../../modules/es.object.to-string');
require('../../modules/esnext.iterator.constructor');
require('../../modules/esnext.iterator.indexed');

var entryUnbind = require('../../internals/entry-unbind');

module.exports = entryUnbind('Iterator', 'indexed');

24 changes: 6 additions & 18 deletions packages/core-js/internals/async-from-sync-iterator.js
@@ -1,5 +1,5 @@
'use strict';
var apply = require('../internals/function-apply');
var call = require('../internals/function-call');
var anObject = require('../internals/an-object');
var create = require('../internals/object-create');
var getMethod = require('../internals/get-method');
Expand Down Expand Up @@ -30,31 +30,19 @@ var AsyncFromSyncIterator = function AsyncIterator(iterator) {
};

AsyncFromSyncIterator.prototype = defineBuiltIns(create(AsyncIteratorPrototype), {
next: function next(arg) {
next: function next() {
var state = getInternalState(this);
var hasArg = !!arguments.length;
return new Promise(function (resolve, reject) {
var result = anObject(apply(state.next, state.iterator, hasArg ? [arg] : []));
var result = anObject(call(state.next, state.iterator));
asyncFromSyncIteratorContinuation(result, resolve, reject);
});
},
'return': function (arg) {
'return': function () {
var iterator = getInternalState(this).iterator;
var hasArg = !!arguments.length;
return new Promise(function (resolve, reject) {
var $return = getMethod(iterator, 'return');
if ($return === undefined) return resolve({ done: true, value: arg });
var result = anObject(apply($return, iterator, hasArg ? [arg] : []));
asyncFromSyncIteratorContinuation(result, resolve, reject);
});
},
'throw': function (arg) {
var iterator = getInternalState(this).iterator;
var hasArg = !!arguments.length;
return new Promise(function (resolve, reject) {
var $throw = getMethod(iterator, 'throw');
if ($throw === undefined) return reject(arg);
var result = anObject(apply($throw, iterator, hasArg ? [arg] : []));
if ($return === undefined) return resolve({ done: true, value: undefined });
var result = anObject(call($return, iterator));
asyncFromSyncIteratorContinuation(result, resolve, reject);
});
}
Expand Down
18 changes: 18 additions & 0 deletions packages/core-js/internals/async-iterator-close.js
@@ -0,0 +1,18 @@
var call = require('../internals/function-call');
var getBuiltIn = require('../internals/get-built-in');
var getMethod = require('../internals/get-method');

module.exports = function (iterator, method, argument, reject) {
try {
var returnMethod = getMethod(iterator, 'return');
if (returnMethod) {
return getBuiltIn('Promise').resolve(call(returnMethod, iterator)).then(function () {
method(argument);
}, function (error) {
reject(error);
});
}
} catch (error2) {
return reject(error2);
} method(argument);
};

0 comments on commit c2620d2

Please sign in to comment.