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 |
|
---|
19 | // https://262.ecma-international.org/14.0/#sec-getiterator
|
---|
20 |
|
---|
21 | module.exports = function GetIterator(obj, kind) {
|
---|
22 | if (kind !== 'sync' && kind !== 'async') {
|
---|
23 | throw new $TypeError("Assertion failed: `kind` must be one of 'sync' or 'async', got " + inspect(kind));
|
---|
24 | }
|
---|
25 |
|
---|
26 | var method;
|
---|
27 | if (kind === 'async') { // step 1
|
---|
28 | if (hasSymbols && $asyncIterator) {
|
---|
29 | method = GetMethod(obj, $asyncIterator); // step 1.a
|
---|
30 | }
|
---|
31 | }
|
---|
32 | if (typeof method === 'undefined') { // step 1.b
|
---|
33 | // var syncMethod = GetMethod(obj, $iterator); // step 1.b.i
|
---|
34 | var syncMethod = getIteratorMethod(
|
---|
35 | {
|
---|
36 | AdvanceStringIndex: AdvanceStringIndex,
|
---|
37 | GetMethod: GetMethod,
|
---|
38 | IsArray: IsArray
|
---|
39 | },
|
---|
40 | obj
|
---|
41 | );
|
---|
42 | if (kind === 'async') {
|
---|
43 | if (typeof syncMethod === 'undefined') {
|
---|
44 | throw new $TypeError('iterator method is `undefined`'); // step 1.b.ii
|
---|
45 | }
|
---|
46 | var syncIteratorRecord = GetIteratorFromMethod(obj, syncMethod); // step 1.b.iii
|
---|
47 | return CreateAsyncFromSyncIterator(syncIteratorRecord); // step 1.b.iv
|
---|
48 | }
|
---|
49 | method = syncMethod; // step 2, kind of
|
---|
50 | }
|
---|
51 |
|
---|
52 | if (typeof method === 'undefined') {
|
---|
53 | throw new $TypeError('iterator method is `undefined`'); // step 3
|
---|
54 | }
|
---|
55 | return GetIteratorFromMethod(obj, method); // step 4
|
---|
56 | };
|
---|