Skip to content

Commit

Permalink
[Refactor] ES2015+: GetIterator: hoist an object to module scope
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Dec 1, 2024
1 parent 2a37ecb commit f9ca142
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 80 deletions.
15 changes: 7 additions & 8 deletions 2015/GetIterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@ var IsArray = require('./IsArray');

var isObject = require('../helpers/isObject');

var ES = {
AdvanceStringIndex: AdvanceStringIndex,
GetMethod: GetMethod,
IsArray: IsArray
};

// https://262.ecma-international.org/6.0/#sec-getiterator

module.exports = function GetIterator(obj, method) {
var actualMethod = method;
if (arguments.length < 2) {
actualMethod = getIteratorMethod(
{
AdvanceStringIndex: AdvanceStringIndex,
GetMethod: GetMethod,
IsArray: IsArray
},
obj
);
actualMethod = getIteratorMethod(ES, obj);
}
var iterator = Call(actualMethod, obj);
if (!isObject(iterator)) {
Expand Down
15 changes: 7 additions & 8 deletions 2016/GetIterator.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 7 additions & 8 deletions 2017/GetIterator.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 7 additions & 8 deletions 2018/GetIterator.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 7 additions & 8 deletions 2019/GetIterator.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 7 additions & 8 deletions 2020/GetIterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ var IsArray = require('./IsArray');

var isObject = require('../helpers/isObject');

var ES = {
AdvanceStringIndex: AdvanceStringIndex,
GetMethod: GetMethod,
IsArray: IsArray
};

// https://262.ecma-international.org/11.0/#sec-getiterator

module.exports = function GetIterator(obj, hint, method) {
Expand All @@ -38,14 +44,7 @@ module.exports = function GetIterator(obj, hint, method) {
throw new $SyntaxError("async from sync iterators aren't currently supported");
}
} else {
actualMethod = getIteratorMethod(
{
AdvanceStringIndex: AdvanceStringIndex,
GetMethod: GetMethod,
IsArray: IsArray
},
obj
);
actualMethod = getIteratorMethod(ES, obj);
}
}
var iterator = Call(actualMethod, obj);
Expand Down
15 changes: 7 additions & 8 deletions 2021/GetIterator.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 7 additions & 8 deletions 2022/GetIterator.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 7 additions & 8 deletions 2023/GetIterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ var IsArray = require('./IsArray');

var getIteratorMethod = require('../helpers/getIteratorMethod');

var ES = {
AdvanceStringIndex: AdvanceStringIndex,
GetMethod: GetMethod,
IsArray: IsArray
};

// https://262.ecma-international.org/14.0/#sec-getiterator

module.exports = function GetIterator(obj, kind) {
Expand All @@ -31,14 +37,7 @@ module.exports = function GetIterator(obj, kind) {
}
if (typeof method === 'undefined') { // step 1.b
// var syncMethod = GetMethod(obj, $iterator); // step 1.b.i
var syncMethod = getIteratorMethod(
{
AdvanceStringIndex: AdvanceStringIndex,
GetMethod: GetMethod,
IsArray: IsArray
},
obj
);
var syncMethod = getIteratorMethod(ES, obj);
if (kind === 'async') {
if (typeof syncMethod === 'undefined') {
throw new $TypeError('iterator method is `undefined`'); // step 1.b.ii
Expand Down
15 changes: 7 additions & 8 deletions 2024/GetIterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ var GetIteratorFromMethod = require('./GetIteratorFromMethod');
var GetMethod = require('./GetMethod');
var IsArray = require('./IsArray');

var ES = {
AdvanceStringIndex: AdvanceStringIndex,
GetMethod: GetMethod,
IsArray: IsArray
};

var getIteratorMethod = require('../helpers/getIteratorMethod');

// https://262.ecma-international.org/14.0/#sec-getiterator
Expand All @@ -31,14 +37,7 @@ module.exports = function GetIterator(obj, kind) {
}
if (typeof method === 'undefined') { // step 1.b
// var syncMethod = GetMethod(obj, $iterator); // step 1.b.i
var syncMethod = getIteratorMethod(
{
AdvanceStringIndex: AdvanceStringIndex,
GetMethod: GetMethod,
IsArray: IsArray
},
obj
);
var syncMethod = getIteratorMethod(ES, obj);
if (kind === 'ASYNC') {
if (typeof syncMethod === 'undefined') {
throw new $TypeError('iterator method is `undefined`'); // step 1.b.ii
Expand Down

0 comments on commit f9ca142

Please sign in to comment.