source: imaps-frontend/node_modules/es-abstract/2024/GetIterator.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.8 KB
RevLine 
[d565449]1'use strict';
2
3var GetIntrinsic = require('get-intrinsic');
4
5var $TypeError = require('es-errors/type');
6var $asyncIterator = GetIntrinsic('%Symbol.asyncIterator%', true);
7
8var inspect = require('object-inspect');
9var hasSymbols = require('has-symbols')();
10
11var AdvanceStringIndex = require('./AdvanceStringIndex');
12var CreateAsyncFromSyncIterator = require('./CreateAsyncFromSyncIterator');
13var GetIteratorFromMethod = require('./GetIteratorFromMethod');
14var GetMethod = require('./GetMethod');
15var IsArray = require('./IsArray');
16
17var getIteratorMethod = require('../helpers/getIteratorMethod');
18
19// https://262.ecma-international.org/14.0/#sec-getiterator
20
21module.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};
Note: See TracBrowser for help on using the repository browser.