2020-08-26 01:57:08 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var hasSymbols = require('has-symbols')();
|
2021-02-26 04:58:33 +01:00
|
|
|
var GetIntrinsic = require('get-intrinsic');
|
|
|
|
var callBound = require('call-bind/callBound');
|
2022-11-10 11:43:16 +01:00
|
|
|
var isString = require('is-string');
|
2020-08-26 01:57:08 +02:00
|
|
|
|
|
|
|
var $iterator = GetIntrinsic('%Symbol.iterator%', true);
|
|
|
|
var $stringSlice = callBound('String.prototype.slice');
|
2022-11-10 11:43:16 +01:00
|
|
|
var $String = GetIntrinsic('%String%', true);
|
2020-08-26 01:57:08 +02:00
|
|
|
|
|
|
|
module.exports = function getIteratorMethod(ES, iterable) {
|
|
|
|
var usingIterator;
|
|
|
|
if (hasSymbols) {
|
|
|
|
usingIterator = ES.GetMethod(iterable, $iterator);
|
|
|
|
} else if (ES.IsArray(iterable)) {
|
|
|
|
usingIterator = function () {
|
|
|
|
var i = -1;
|
|
|
|
var arr = this; // eslint-disable-line no-invalid-this
|
|
|
|
return {
|
|
|
|
next: function () {
|
|
|
|
i += 1;
|
|
|
|
return {
|
|
|
|
done: i >= arr.length,
|
|
|
|
value: arr[i]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
2022-11-10 11:43:16 +01:00
|
|
|
} else if (isString(iterable)) {
|
2020-08-26 01:57:08 +02:00
|
|
|
usingIterator = function () {
|
|
|
|
var i = 0;
|
|
|
|
return {
|
|
|
|
next: function () {
|
2022-11-10 11:43:16 +01:00
|
|
|
var nextIndex = ES.AdvanceStringIndex($String(iterable), i, true);
|
2020-08-26 01:57:08 +02:00
|
|
|
var value = $stringSlice(iterable, i, nextIndex);
|
|
|
|
i = nextIndex;
|
|
|
|
return {
|
|
|
|
done: nextIndex > iterable.length,
|
|
|
|
value: value
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return usingIterator;
|
|
|
|
};
|