source: imaps-frontend/node_modules/es-abstract/2020/CreateListFromArrayLike.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 1.5 KB
Line 
1'use strict';
2
3var callBound = require('call-bound');
4
5var $TypeError = require('es-errors/type');
6var $indexOf = callBound('Array.prototype.indexOf', true) || callBound('String.prototype.indexOf');
7var $push = callBound('Array.prototype.push');
8
9var Get = require('./Get');
10var IsArray = require('./IsArray');
11var LengthOfArrayLike = require('./LengthOfArrayLike');
12var ToString = require('./ToString');
13var Type = require('./Type');
14
15var isObject = require('../helpers/isObject');
16
17var defaultElementTypes = ['Undefined', 'Null', 'Boolean', 'String', 'Symbol', 'Number', 'BigInt', 'Object'];
18
19// https://262.ecma-international.org/11.0/#sec-createlistfromarraylike
20
21/** @type {(obj: object, elementTypes?: typeof defaultElementTypes) => unknown[]} */
22module.exports = function CreateListFromArrayLike(obj) {
23 var elementTypes = arguments.length > 1
24 ? arguments[1]
25 : defaultElementTypes;
26
27 if (!isObject(obj)) {
28 throw new $TypeError('Assertion failed: `obj` must be an Object');
29 }
30 if (!IsArray(elementTypes)) {
31 throw new $TypeError('Assertion failed: `elementTypes`, if provided, must be an array');
32 }
33 var len = LengthOfArrayLike(obj);
34 /** @type {(typeof elementTypes)[]} */
35 var list = [];
36 var index = 0;
37 while (index < len) {
38 var indexName = ToString(index);
39 var next = Get(obj, indexName);
40 var nextType = Type(next);
41 if ($indexOf(elementTypes, nextType) < 0) {
42 throw new $TypeError('item type ' + nextType + ' is not a valid elementType');
43 }
44 $push(list, next);
45 index += 1;
46 }
47 return list;
48};
Note: See TracBrowser for help on using the repository browser.