Skip to content

Commit

Permalink
refactor to use getIteratorMethod + getIterator in some cases
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Aug 28, 2021
1 parent 5bb48b3 commit 0f1bc28
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 6 deletions.
3 changes: 2 additions & 1 deletion packages/core-js/internals/array-from.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var callWithSafeIterationClosing = require('../internals/call-with-safe-iteratio
var isArrayIteratorMethod = require('../internals/is-array-iterator-method');
var toLength = require('../internals/to-length');
var createProperty = require('../internals/create-property');
var getIterator = require('../internals/get-iterator');
var getIteratorMethod = require('../internals/get-iterator-method');

// `Array.from` method implementation
Expand All @@ -21,7 +22,7 @@ module.exports = function from(arrayLike /* , mapfn = undefined, thisArg = undef
if (mapping) mapfn = bind(mapfn, argumentsLength > 2 ? arguments[2] : undefined, 2);
// if the target is not iterable or it's an array with the default iterator - use a simple case
if (iteratorMethod != undefined && !(C == Array && isArrayIteratorMethod(iteratorMethod))) {
iterator = iteratorMethod.call(O);
iterator = getIterator(O, iteratorMethod);
next = iterator.next;
result = new C();
for (;!(step = next.call(iterator)).done; index++) {
Expand Down
3 changes: 2 additions & 1 deletion packages/core-js/internals/iterate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var anObject = require('../internals/an-object');
var isArrayIteratorMethod = require('../internals/is-array-iterator-method');
var toLength = require('../internals/to-length');
var bind = require('../internals/function-bind-context');
var getIterator = require('../internals/get-iterator');
var getIteratorMethod = require('../internals/get-iterator-method');
var iteratorClose = require('../internals/iterator-close');

Expand Down Expand Up @@ -42,7 +43,7 @@ module.exports = function (iterable, unboundFunction, options) {
if (result && result instanceof Result) return result;
} return new Result(false);
}
iterator = iterFn.call(iterable);
iterator = getIterator(iterable, iterFn);
}

next = iterator.next;
Expand Down
3 changes: 2 additions & 1 deletion packages/core-js/internals/typed-array-from.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var toObject = require('../internals/to-object');
var toLength = require('../internals/to-length');
var getIterator = require('../internals/get-iterator');
var getIteratorMethod = require('../internals/get-iterator-method');
var isArrayIteratorMethod = require('../internals/is-array-iterator-method');
var bind = require('../internals/function-bind-context');
Expand All @@ -13,7 +14,7 @@ module.exports = function from(source /* , mapfn, thisArg */) {
var iteratorMethod = getIteratorMethod(O);
var i, length, result, step, iterator, next;
if (iteratorMethod != undefined && !isArrayIteratorMethod(iteratorMethod)) {
iterator = iteratorMethod.call(O);
iterator = getIterator(O, iteratorMethod);
next = iterator.next;
O = [];
while (!(step = next.call(iterator)).done) {
Expand Down
4 changes: 2 additions & 2 deletions packages/core-js/modules/esnext.iterator.from.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// https://github.com/tc39/proposal-iterator-helpers
var $ = require('../internals/export');
var path = require('../internals/path');
var aFunction = require('../internals/a-function');
var anObject = require('../internals/an-object');
var toObject = require('../internals/to-object');
var createIteratorProxy = require('../internals/iterator-create-proxy');
var getIterator = require('../internals/get-iterator');
var getIteratorMethod = require('../internals/get-iterator-method');

var Iterator = path.Iterator;
Expand All @@ -21,7 +21,7 @@ $({ target: 'Iterator', stat: true }, {
var usingIterator = getIteratorMethod(object);
var iterator;
if (usingIterator != null) {
iterator = aFunction(usingIterator).call(object);
iterator = getIterator(object, usingIterator);
if (iterator instanceof Iterator) return iterator;
} else {
iterator = object;
Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/modules/web.url-search-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ var URLSearchParamsConstructor = function URLSearchParams(/* init */) {
if (isObject(init)) {
iteratorMethod = getIteratorMethod(init);
if (typeof iteratorMethod === 'function') {
iterator = iteratorMethod.call(init);
iterator = getIterator(init, iteratorMethod);
next = iterator.next;
while (!(step = next.call(iterator)).done) {
entryIterator = getIterator(anObject(step.value));
Expand Down

0 comments on commit 0f1bc28

Please sign in to comment.