Skip to content

Commit

Permalink
add Iterator.prototype.toAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Dec 9, 2021
1 parent 46cd568 commit 5357af7
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Changelog
##### Unreleased
- Added `Iterator.prototype.toAsync` method from [the iterator helpers stage 2 proposal](https://github.com/tc39/proposal-iterator-helpers)
- Added a workaround of most cases breakage modern `String#at` after loading obsolete `String#at` proposal module, [#1019](https://github.com/zloirock/core-js/issues/1019)

##### 3.19.3 - 2021.12.06
Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1918,7 +1918,7 @@ core-js/features/typed-array/find-last-index
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) and [`esnext.iterator.to-array`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.to-array.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.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)
```js
class Iterator {
static from(iterable: Iterable<mixed>): Iterator<any>;
Expand All @@ -1934,6 +1934,7 @@ class Iterator {
some(callbackfn: value: any => boolean): boolean;
take(limit: uint): Iterator<any>;
toArray(): Array<any>;
toAsync(): AsyncIterator<any>;
@@toStringTag: 'Iterator'
}

Expand Down Expand Up @@ -1985,6 +1986,7 @@ core-js(-pure)/features/iterator/reduce
core-js(-pure)/features/iterator/some
core-js(-pure)/features/iterator/take
core-js(-pure)/features/iterator/to-array
core-js(-pure)/features/iterator/to-async
```
[Examples](t.ly/FEp1):
```js
Expand All @@ -1999,13 +2001,14 @@ Iterator.from({
next: () => ({ done: Math.random() > .9, value: Math.random() * 10 | 0 })
}).toArray(); // => [7, 6, 3, 0, 2, 8]

AsyncIterator.from([1, 2, 3, 4, 5, 6, 7])
await AsyncIterator.from([1, 2, 3, 4, 5, 6, 7])
.drop(1)
.take(5)
.filter(it => it % 2)
.map(it => it ** 2)
.toArray()
.then(console.log); // => [9, 25]
.toArray(); // => [9, 25]

await [1, 2, 3].values().toAsync().map(async it => it ** 2).toArray(); // => [1, 4, 9]
```
###### Caveats:[⬆](#index)
- For preventing prototypes pollution, in the `pure` version, new `%IteratorPrototype%` methods are not added to the real `%IteratorPrototype%`, they available only on wrappers - instead of `[].values().map(fn)` use `Iterator.from([]).map(fn)`.
Expand Down
2 changes: 2 additions & 0 deletions packages/core-js-compat/src/data.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,8 @@ export const data = {
},
'esnext.iterator.to-array': {
},
'esnext.iterator.to-async': {
},
'esnext.map.delete-all': {
},
'esnext.map.emplace': {
Expand Down
3 changes: 3 additions & 0 deletions packages/core-js-compat/src/modules-by-versions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,7 @@ export default {
'esnext.array.from-async',
'esnext.typed-array.from-async',
],
'3.20': [
'esnext.iterator.to-async',
],
};
2 changes: 2 additions & 0 deletions packages/core-js/features/iterator/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require('../../modules/es.array.iterator');
require('../../modules/es.object.to-string');
require('../../modules/es.promise');
require('../../modules/es.string.iterator');
require('../../modules/esnext.iterator.constructor');
require('../../modules/esnext.iterator.as-indexed-pairs');
Expand All @@ -15,6 +16,7 @@ require('../../modules/esnext.iterator.reduce');
require('../../modules/esnext.iterator.some');
require('../../modules/esnext.iterator.take');
require('../../modules/esnext.iterator.to-array');
require('../../modules/esnext.iterator.to-async');
require('../../modules/web.dom-collections.iterator');

var path = require('../../internals/path');
Expand Down
8 changes: 8 additions & 0 deletions packages/core-js/features/iterator/to-async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require('../../modules/es.object.to-string');
require('../../modules/es.promise');
require('../../modules/esnext.iterator.constructor');
require('../../modules/esnext.iterator.to-async');

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

module.exports = entryUnbind('Iterator', 'toAsync');
10 changes: 10 additions & 0 deletions packages/core-js/modules/esnext.iterator.to-async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';
// https://github.com/tc39/proposal-iterator-helpers
var $ = require('../internals/export');
var AsyncFromSyncIterator = require('../internals/async-from-sync-iterator');

$({ target: 'Iterator', proto: true, real: true }, {
toAsync: function toAsync() {
return new AsyncFromSyncIterator(this);
}
});
1 change: 1 addition & 0 deletions packages/core-js/proposals/iterator-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ require('../modules/esnext.iterator.reduce');
require('../modules/esnext.iterator.some');
require('../modules/esnext.iterator.take');
require('../modules/esnext.iterator.to-array');
require('../modules/esnext.iterator.to-async');
1 change: 1 addition & 0 deletions tests/commonjs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,7 @@ for (PATH of ['core-js-pure', 'core-js']) {
ok(typeof load(NS, 'iterator/some') == 'function');
ok(typeof load(NS, 'iterator/take') == 'function');
ok(typeof load(NS, 'iterator/to-array') == 'function');
ok(typeof load(NS, 'iterator/to-async') == 'function');
ok(load(NS, 'map/delete-all')(new Map(), 1, 2) === false);
ok(load(NS, 'map/emplace')(new Map([[1, 2]]), 1, { update: it => it ** 2 }) === 4);
ok(load(NS, 'map/every')(new Map([[1, 2], [2, 3], [3, 4]]), it => it % 2) === false);
Expand Down
3 changes: 3 additions & 0 deletions tests/compat/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1325,6 +1325,9 @@ GLOBAL.tests = {
'esnext.iterator.to-array': function () {
return Iterator.prototype.toArray;
},
'esnext.iterator.to-async': function () {
return Iterator.prototype.toAsync;
},
'esnext.map.delete-all': function () {
return Map.prototype.deleteAll;
},
Expand Down
26 changes: 26 additions & 0 deletions tests/pure/esnext.iterator.to-async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Promise from 'core-js-pure/es/promise';
import Set from 'core-js-pure/es/set';
import Iterator from 'core-js-pure/features/iterator';
import 'core-js-pure/features/async-iterator';

QUnit.test('Iterator#toAsync', assert => {
assert.expect(6);
const async = assert.async();

const { toAsync } = Iterator.prototype;

assert.isFunction(toAsync);
assert.arity(toAsync, 0);

assert.throws(() => toAsync.call(undefined), TypeError);
assert.throws(() => toAsync.call(null), TypeError);

Iterator.from([1, 2, 3]).toAsync().map(it => Promise.resolve(it)).toArray().then(it => {
assert.arrayEqual(it, [1, 2, 3]);
}).then(() => {
return Iterator.from(new Set([1, 2, 3])).toAsync().map(it => Promise.resolve(it)).toArray();
}).then(it => {
assert.arrayEqual(it, [1, 2, 3]);
async();
});
});
24 changes: 24 additions & 0 deletions tests/tests/esnext.iterator.to-async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
QUnit.test('Iterator#toAsync', assert => {
assert.expect(9);
const async = assert.async();

const { toAsync } = Iterator.prototype;

assert.isFunction(toAsync);
assert.arity(toAsync, 0);
assert.name(toAsync, 'toAsync');
assert.looksNative(toAsync);
assert.nonEnumerable(Iterator.prototype, 'toAsync');

assert.throws(() => toAsync.call(undefined), TypeError);
assert.throws(() => toAsync.call(null), TypeError);

[1, 2, 3].values().toAsync().map(it => Promise.resolve(it)).toArray().then(it => {
assert.arrayEqual(it, [1, 2, 3]);
}).then(() => {
return new Set([1, 2, 3]).values().toAsync().map(it => Promise.resolve(it)).toArray();
}).then(it => {
assert.arrayEqual(it, [1, 2, 3]);
async();
});
});

0 comments on commit 5357af7

Please sign in to comment.