Skip to content

Commit

Permalink
Merge pull request #1123 from zloirock/iterator-helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock authored Oct 7, 2022
2 parents 48cc8f5 + b16bb78 commit d81aeb1
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- [`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)
- Early exit on broken `.next` in missed cases of `{ Iterator, AsyncIterator }.from`, [proposal-iterator-helpers/232](https://github.com/tc39/proposal-iterator-helpers/pull/232)
- 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
3 changes: 2 additions & 1 deletion packages/core-js/internals/array-from-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var toObject = require('../internals/to-object');
var isConstructor = require('../internals/is-constructor');
var getAsyncIterator = require('../internals/get-async-iterator');
var getIterator = require('../internals/get-iterator');
var getIteratorDirect = require('../internals/get-iterator-direct');
var getIteratorMethod = require('../internals/get-iterator-method');
var getMethod = require('../internals/get-method');
var getVirtual = require('../internals/entry-virtual');
Expand Down Expand Up @@ -44,7 +45,7 @@ module.exports = function fromAsync(asyncItems /* , mapfn = undefined, thisArg =
var A = isConstructor(C) ? new C() : [];
var iterator = usingAsyncIterator
? getAsyncIterator(O, usingAsyncIterator)
: new AsyncFromSyncIterator(getIterator(O, usingSyncIterator));
: new AsyncFromSyncIterator(getIteratorDirect(getIterator(O, usingSyncIterator)));
resolve(toArray(iterator, mapfn, A));
});
};
9 changes: 3 additions & 6 deletions packages/core-js/internals/async-from-sync-iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,9 @@ var asyncFromSyncIteratorContinuation = function (result, resolve, reject) {
}, reject);
};

var AsyncFromSyncIterator = function AsyncIterator(iterator) {
setInternalState(this, {
type: ASYNC_FROM_SYNC_ITERATOR,
iterator: anObject(iterator),
next: iterator.next
});
var AsyncFromSyncIterator = function AsyncIterator(iteratorRecord) {
iteratorRecord.type = ASYNC_FROM_SYNC_ITERATOR;
setInternalState(this, iteratorRecord);
};

AsyncFromSyncIterator.prototype = defineBuiltIns(create(AsyncIteratorPrototype), {
Expand Down
3 changes: 2 additions & 1 deletion packages/core-js/internals/get-async-iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ var call = require('../internals/function-call');
var AsyncFromSyncIterator = require('../internals/async-from-sync-iterator');
var anObject = require('../internals/an-object');
var getIterator = require('../internals/get-iterator');
var getIteratorDirect = require('../internals/get-iterator-direct');
var getMethod = require('../internals/get-method');
var wellKnownSymbol = require('../internals/well-known-symbol');

var ASYNC_ITERATOR = wellKnownSymbol('asyncIterator');

module.exports = function (it, usingIterator) {
var method = arguments.length < 2 ? getMethod(it, ASYNC_ITERATOR) : usingIterator;
return method ? anObject(call(method, it)) : new AsyncFromSyncIterator(getIterator(it));
return method ? anObject(call(method, it)) : new AsyncFromSyncIterator(getIteratorDirect(getIterator(it)));
};
14 changes: 8 additions & 6 deletions packages/core-js/modules/esnext.async-iterator.from.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ $({ target: 'AsyncIterator', stat: true, forced: true }, {
from: function from(O) {
var object = toObject(O);
var usingIterator = getMethod(object, ASYNC_ITERATOR);
var iterator;
var iteratorRecord;
if (usingIterator) {
iterator = getAsyncIterator(object, usingIterator);
if (isPrototypeOf(AsyncIteratorPrototype, iterator)) return iterator;
iteratorRecord = getIteratorDirect(getAsyncIterator(object, usingIterator));
if (isPrototypeOf(AsyncIteratorPrototype, iteratorRecord.iterator)) return iteratorRecord.iterator;
}
if (iterator === undefined) {
if (iteratorRecord === undefined) {
usingIterator = getIteratorMethod(object);
if (usingIterator) iterator = new AsyncFromSyncIterator(getIterator(object, usingIterator));
if (usingIterator) iteratorRecord = getIteratorDirect(new AsyncFromSyncIterator(
getIteratorDirect(getIterator(object, usingIterator))
));
}
return new WrapAsyncIterator(getIteratorDirect(iterator !== undefined ? iterator : object));
return new WrapAsyncIterator(iteratorRecord !== undefined ? iteratorRecord : getIteratorDirect(object));
}
});
10 changes: 5 additions & 5 deletions packages/core-js/modules/esnext.iterator.from.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ $({ target: 'Iterator', stat: true, forced: true }, {
from: function from(O) {
var object = toObject(O);
var usingIterator = getIteratorMethod(object);
var iterator;
var iteratorRecord;
if (usingIterator) {
iterator = getIterator(object, usingIterator);
if (isPrototypeOf(IteratorPrototype, iterator)) return iterator;
iteratorRecord = getIteratorDirect(getIterator(object, usingIterator));
if (isPrototypeOf(IteratorPrototype, iteratorRecord.iterator)) return iteratorRecord.iterator;
} else {
iterator = object;
} return new IteratorProxy(getIteratorDirect(iterator));
iteratorRecord = getIteratorDirect(object);
} return new IteratorProxy(iteratorRecord);
}
});
2 changes: 1 addition & 1 deletion packages/core-js/modules/esnext.iterator.to-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ var getIteratorDirect = require('../internals/get-iterator-direct');

$({ target: 'Iterator', proto: true, real: true, forced: true }, {
toAsync: function toAsync() {
return new WrapAsyncIterator(getIteratorDirect(new AsyncFromSyncIterator(this)));
return new WrapAsyncIterator(getIteratorDirect(new AsyncFromSyncIterator(getIteratorDirect(this))));
}
});
3 changes: 2 additions & 1 deletion tests/pure/esnext.async-iterator.from.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import values from 'core-js-pure/es/array/values';
import AsyncIterator from 'core-js-pure/full/async-iterator';

QUnit.test('AsyncIterator.from', assert => {
assert.expect(9);
assert.expect(10);
const async = assert.async();
const { from } = AsyncIterator;

Expand All @@ -29,4 +29,5 @@ QUnit.test('AsyncIterator.from', assert => {
assert.throws(() => from(undefined), TypeError);
assert.throws(() => from(null), TypeError);
assert.throws(() => from({}), TypeError);
assert.throws(() => from(assign(new AsyncIterator(), { next: 42 })), TypeError);
});
2 changes: 2 additions & 0 deletions tests/pure/esnext.iterator.from.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createIterable, createIterator } from '../helpers/helpers';

import Iterator from 'core-js-pure/full/iterator';
import assign from 'core-js-pure/es/object/assign';

QUnit.test('Iterator.from', assert => {
const { from } = Iterator;
Expand All @@ -17,4 +18,5 @@ QUnit.test('Iterator.from', assert => {
assert.throws(() => from(undefined), TypeError);
assert.throws(() => from(null), TypeError);
assert.throws(() => from({}), TypeError);
assert.throws(() => from(assign(new Iterator(), { next: 42 })), TypeError);
});
7 changes: 5 additions & 2 deletions tests/tests/esnext.async-iterator.from.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const { assign } = Object;

QUnit.test('AsyncIterator.from', assert => {
assert.expect(12);
assert.expect(13);
const async = assert.async();
const { from } = AsyncIterator;

Expand All @@ -18,7 +20,7 @@ QUnit.test('AsyncIterator.from', assert => {
async();
});

const asyncIterator = Object.assign(new AsyncIterator(), {
const asyncIterator = assign(new AsyncIterator(), {
next: () => { /* empty */ },
});

Expand All @@ -27,4 +29,5 @@ QUnit.test('AsyncIterator.from', assert => {
assert.throws(() => from(undefined), TypeError);
assert.throws(() => from(null), TypeError);
assert.throws(() => from({}), TypeError);
assert.throws(() => from(assign(new AsyncIterator(), { next: 42 })), TypeError);
});
3 changes: 3 additions & 0 deletions tests/tests/esnext.iterator.from.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { createIterable, createIterator } from '../helpers/helpers';

const { assign } = Object;

QUnit.test('Iterator.from', assert => {
const { from } = Iterator;

Expand All @@ -18,4 +20,5 @@ QUnit.test('Iterator.from', assert => {
assert.throws(() => from(undefined), TypeError);
assert.throws(() => from(null), TypeError);
assert.throws(() => from({}), TypeError);
assert.throws(() => from(assign(new Iterator(), { next: 42 })), TypeError);
});

0 comments on commit d81aeb1

Please sign in to comment.