Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Array.fromAsync #1122

Merged
merged 2 commits into from Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,8 @@
## Changelog
##### Unreleased
- [`Array.fromAsync` proposal](https://github.com/tc39/proposal-array-from-async):
- Moved to Stage 3, [September TC39 meeting](https://github.com/babel/proposals/issues/83#issuecomment-1246218703)
- Avoid observable side effects of `%Array.prototype.values%` usage in array-like branch, [proposal-array-from-async/30](https://github.com/tc39/proposal-array-from-async/pull/30)
- Added `inverse` option to `core-js-compat`, [#1119](https://github.com/zloirock/core-js/issues/1119)
- Added `format` option to `core-js-builder`, [#1120](https://github.com/zloirock/core-js/issues/1120)

Expand Down
34 changes: 17 additions & 17 deletions README.md
Expand Up @@ -126,13 +126,13 @@ queueMicrotask(() => console.log('called as microtask'));
- [`Symbol.prototype.description`](#symbolprototypedescription)
- [Well-formed `JSON.stringify`](#well-formed-jsonstringify)
- [Stage 3 proposals](#stage-3-proposals)
- [`Array.fromAsync`](#arrayfromasync)
- [`Array` grouping](#array-grouping)
- [Change `Array` by copy](#change-array-by-copy)
- [Stage 2 proposals](#stage-2-proposals)
- [`Iterator` helpers](#iterator-helpers)
- [New `Set` methods](#new-set-methods)
- [`Map.prototype.emplace`](#mapprototypeemplace)
- [`Array.fromAsync`](#arrayfromasync)
- [`Array.isTemplateObject`](#arrayistemplateobject)
- [`Symbol.{ asyncDispose, dispose }` for `using` statement](#symbol-asyncdispose-dispose--for-using-statement)
- [`Symbol.metadataKey` for decorators metadata proposal](#symbolmetadatakey-for-decorators-metadata-proposal)
Expand Down Expand Up @@ -2103,6 +2103,22 @@ core-js/proposals/well-formed-stringify
```js
core-js(-pure)/stage/3
```
##### [`Array.fromAsync`](https://github.com/tc39/proposal-array-from-async)[⬆](#index)
Modules [`esnext.array.from-async`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.array.from-async.js).
```js
class Array {
static fromAsync(asyncItems: AsyncIterable | Iterable | ArrayLike, mapfn?: (value: any, index: number) => any, thisArg?: any): Array;
}
```
[*CommonJS entry points:*](#commonjs-api)
```js
core-js/proposals/array-from-async-stage-2
core-js(-pure)/full/array/from-async
```
[*Example*](https://goo.gl/Jt7SsD):
```js
await Array.fromAsync((async function * (){ yield * [1, 2, 3] })(), i => i * i); // => [1, 4, 9]
```
##### [`Array` grouping](https://github.com/tc39/proposal-array-grouping)[⬆](#index)
Modules [`esnext.array.group`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.array.group.js), [`esnext.array.group-to-map`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.array.group-to-map.js).
```js
Expand Down Expand Up @@ -2343,22 +2359,6 @@ map.emplace('b', { update: it => it ** 2, insert: () => 3}); // => 3

console.log(map); // => Map { 'a': 4, 'b': 3 }
```
##### [`Array.fromAsync`](https://github.com/tc39/proposal-array-from-async)[⬆](#index)
Modules [`esnext.array.from-async`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.array.from-async.js).
```js
class Array {
static fromAsync(asyncItems: AsyncIterable | Iterable | ArrayLike, mapfn?: (value: any, index: number) => any, thisArg?: any): Array;
}
```
[*CommonJS entry points:*](#commonjs-api)
```js
core-js/proposals/array-from-async-stage-2
core-js(-pure)/full/array/from-async
```
[*Example*](https://goo.gl/Jt7SsD):
```js
await Array.fromAsync((async function * (){ yield * [1, 2, 3] })(), i => i * i); // => [1, 4, 9]
```
##### [`Array.isTemplateObject`](https://github.com/tc39/proposal-array-is-template-object)[⬆](#index)
Module [`esnext.array.is-template-object`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.array.is-template-object.js)
```js
Expand Down
8 changes: 8 additions & 0 deletions packages/core-js/actual/array/from-async.js
@@ -0,0 +1,8 @@
require('../../modules/es.array.iterator');
require('../../modules/es.object.to-string');
require('../../modules/es.promise');
require('../../modules/es.string.iterator');
require('../../modules/esnext.array.from-async');
var path = require('../../internals/path');

module.exports = path.Array.fromAsync;
1 change: 1 addition & 0 deletions packages/core-js/actual/array/index.js
Expand Up @@ -3,6 +3,7 @@ require('../../modules/es.map');
require('../../modules/es.object.to-string');
require('../../modules/esnext.array.find-last');
require('../../modules/esnext.array.find-last-index');
require('../../modules/esnext.array.from-async');
require('../../modules/esnext.array.group');
require('../../modules/esnext.array.group-by');
require('../../modules/esnext.array.group-by-to-map');
Expand Down
9 changes: 2 additions & 7 deletions packages/core-js/full/array/from-async.js
@@ -1,8 +1,3 @@
require('../../modules/es.array.iterator');
require('../../modules/es.object.to-string');
require('../../modules/es.promise');
require('../../modules/es.string.iterator');
require('../../modules/esnext.array.from-async');
var path = require('../../internals/path');
var parent = require('../../actual/array/from-async');

module.exports = path.Array.fromAsync;
module.exports = parent;
1 change: 0 additions & 1 deletion packages/core-js/full/array/index.js
@@ -1,6 +1,5 @@
var parent = require('../../actual/array');
require('../../modules/es.promise');
require('../../modules/esnext.array.from-async');
// TODO: Remove from `core-js@4`
require('../../modules/esnext.array.at');
// TODO: Remove from `core-js@4`
Expand Down
18 changes: 16 additions & 2 deletions packages/core-js/internals/array-from-async.js
@@ -1,5 +1,6 @@
'use strict';
var bind = require('../internals/function-bind-context');
var uncurryThis = require('../internals/function-uncurry-this');
var toObject = require('../internals/to-object');
var isConstructor = require('../internals/is-constructor');
var getAsyncIterator = require('../internals/get-async-iterator');
Expand All @@ -13,7 +14,20 @@ var AsyncFromSyncIterator = require('../internals/async-from-sync-iterator');
var toArray = require('../internals/async-iterator-iteration').toArray;

var ASYNC_ITERATOR = wellKnownSymbol('asyncIterator');
var arrayIterator = getVirtual('Array').values;
var arrayIterator = uncurryThis(getVirtual('Array').values);
var arrayIteratorNext = uncurryThis(arrayIterator([]).next);

var safeArrayIterator = function () {
return new SafeArrayIterator(this);
};

var SafeArrayIterator = function (O) {
this.iterator = arrayIterator(O);
};

SafeArrayIterator.prototype.next = function () {
return arrayIteratorNext(this.iterator);
};

// `Array.fromAsync` method implementation
// https://github.com/tc39/proposal-array-from-async
Expand All @@ -26,7 +40,7 @@ module.exports = function fromAsync(asyncItems /* , mapfn = undefined, thisArg =
var O = toObject(asyncItems);
if (mapfn !== undefined) mapfn = bind(mapfn, thisArg);
var usingAsyncIterator = getMethod(O, ASYNC_ITERATOR);
var usingSyncIterator = usingAsyncIterator ? undefined : getIteratorMethod(O) || arrayIterator;
var usingSyncIterator = usingAsyncIterator ? undefined : getIteratorMethod(O) || safeArrayIterator;
var A = isConstructor(C) ? new C() : [];
var iterator = usingAsyncIterator
? getAsyncIterator(O, usingAsyncIterator)
Expand Down
1 change: 0 additions & 1 deletion packages/core-js/stage/2.js
@@ -1,4 +1,3 @@
require('../proposals/array-from-async-stage-2');
require('../proposals/array-is-template-object');
require('../proposals/decorator-metadata');
require('../proposals/iterator-helpers');
Expand Down
1 change: 1 addition & 0 deletions packages/core-js/stage/3.js
@@ -1,3 +1,4 @@
require('../proposals/array-from-async-stage-2');
require('../proposals/array-grouping-stage-3-2');
require('../proposals/change-array-by-copy');
// TODO: Obsolete versions, remove from `core-js@4`
Expand Down
2 changes: 1 addition & 1 deletion tests/commonjs.mjs
Expand Up @@ -596,6 +596,7 @@ for (PATH of ['core-js-pure', 'core-js']) {
}

for (const NS of ['actual', 'full', 'features']) {
ok(typeof load(NS, 'array/from-async') == 'function');
ok(typeof load(NS, 'array/group') == 'function');
ok(typeof load(NS, 'array/group-to-map') == 'function');
ok(typeof load(NS, 'array/group-by') == 'function');
Expand Down Expand Up @@ -667,7 +668,6 @@ for (PATH of ['core-js-pure', 'core-js']) {
const Set = load(NS, 'set');
const WeakMap = load(NS, 'weak-map');
const WeakSet = load(NS, 'weak-set');
ok(typeof load(NS, 'array/from-async') == 'function');
ok(typeof load(NS, 'array/filter-out') == 'function');
ok(typeof load(NS, 'array/filter-reject') == 'function');
ok(typeof load(NS, 'array/is-template-object') == 'function');
Expand Down