[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var GetIntrinsic = require('get-intrinsic');
|
---|
| 4 |
|
---|
| 5 | var $TypeError = require('es-errors/type');
|
---|
| 6 | var $asyncIterator = GetIntrinsic('%Symbol.asyncIterator%', true);
|
---|
| 7 |
|
---|
| 8 | var inspect = require('object-inspect');
|
---|
| 9 | var hasSymbols = require('has-symbols')();
|
---|
| 10 |
|
---|
| 11 | var AdvanceStringIndex = require('./AdvanceStringIndex');
|
---|
| 12 | var CreateAsyncFromSyncIterator = require('./CreateAsyncFromSyncIterator');
|
---|
| 13 | var GetIteratorFromMethod = require('./GetIteratorFromMethod');
|
---|
| 14 | var GetMethod = require('./GetMethod');
|
---|
| 15 | var IsArray = require('./IsArray');
|
---|
| 16 |
|
---|
| 17 | var getIteratorMethod = require('../helpers/getIteratorMethod');
|
---|
| 18 |
|
---|
[79a0317] | 19 | var ES = {
|
---|
| 20 | AdvanceStringIndex: AdvanceStringIndex,
|
---|
| 21 | GetMethod: GetMethod,
|
---|
| 22 | IsArray: IsArray
|
---|
| 23 | };
|
---|
| 24 |
|
---|
[d565449] | 25 | // https://262.ecma-international.org/14.0/#sec-getiterator
|
---|
| 26 |
|
---|
| 27 | module.exports = function GetIterator(obj, kind) {
|
---|
| 28 | if (kind !== 'sync' && kind !== 'async') {
|
---|
| 29 | throw new $TypeError("Assertion failed: `kind` must be one of 'sync' or 'async', got " + inspect(kind));
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | var method;
|
---|
| 33 | if (kind === 'async') { // step 1
|
---|
| 34 | if (hasSymbols && $asyncIterator) {
|
---|
| 35 | method = GetMethod(obj, $asyncIterator); // step 1.a
|
---|
| 36 | }
|
---|
| 37 | }
|
---|
| 38 | if (typeof method === 'undefined') { // step 1.b
|
---|
| 39 | // var syncMethod = GetMethod(obj, $iterator); // step 1.b.i
|
---|
[79a0317] | 40 | var syncMethod = getIteratorMethod(ES, obj);
|
---|
[d565449] | 41 | if (kind === 'async') {
|
---|
| 42 | if (typeof syncMethod === 'undefined') {
|
---|
| 43 | throw new $TypeError('iterator method is `undefined`'); // step 1.b.ii
|
---|
| 44 | }
|
---|
| 45 | var syncIteratorRecord = GetIteratorFromMethod(obj, syncMethod); // step 1.b.iii
|
---|
| 46 | return CreateAsyncFromSyncIterator(syncIteratorRecord); // step 1.b.iv
|
---|
| 47 | }
|
---|
| 48 | method = syncMethod; // step 2, kind of
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | if (typeof method === 'undefined') {
|
---|
| 52 | throw new $TypeError('iterator method is `undefined`'); // step 3
|
---|
| 53 | }
|
---|
| 54 | return GetIteratorFromMethod(obj, method); // step 4
|
---|
| 55 | };
|
---|