[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var callBound = require('call-bind/callBound');
|
---|
| 4 |
|
---|
| 5 | var $TypeError = require('es-errors/type');
|
---|
| 6 | var $indexOf = callBound('Array.prototype.indexOf', true) || callBound('String.prototype.indexOf');
|
---|
| 7 | var $push = callBound('Array.prototype.push');
|
---|
| 8 |
|
---|
| 9 | var Get = require('./Get');
|
---|
| 10 | var IsArray = require('./IsArray');
|
---|
| 11 | var LengthOfArrayLike = require('./LengthOfArrayLike');
|
---|
| 12 | var ToString = require('./ToString');
|
---|
| 13 | var Type = require('./Type');
|
---|
| 14 |
|
---|
| 15 | var defaultElementTypes = ['Undefined', 'Null', 'Boolean', 'String', 'Symbol', 'Number', 'BigInt', 'Object'];
|
---|
| 16 |
|
---|
| 17 | // https://262.ecma-international.org/11.0/#sec-createlistfromarraylike
|
---|
| 18 |
|
---|
| 19 | module.exports = function CreateListFromArrayLike(obj) {
|
---|
| 20 | var elementTypes = arguments.length > 1
|
---|
| 21 | ? arguments[1]
|
---|
| 22 | : defaultElementTypes;
|
---|
| 23 |
|
---|
| 24 | if (Type(obj) !== 'Object') {
|
---|
| 25 | throw new $TypeError('Assertion failed: `obj` must be an Object');
|
---|
| 26 | }
|
---|
| 27 | if (!IsArray(elementTypes)) {
|
---|
| 28 | throw new $TypeError('Assertion failed: `elementTypes`, if provided, must be an array');
|
---|
| 29 | }
|
---|
| 30 | var len = LengthOfArrayLike(obj);
|
---|
| 31 | var list = [];
|
---|
| 32 | var index = 0;
|
---|
| 33 | while (index < len) {
|
---|
| 34 | var indexName = ToString(index);
|
---|
| 35 | var next = Get(obj, indexName);
|
---|
| 36 | var nextType = Type(next);
|
---|
| 37 | if ($indexOf(elementTypes, nextType) < 0) {
|
---|
| 38 | throw new $TypeError('item type ' + nextType + ' is not a valid elementType');
|
---|
| 39 | }
|
---|
| 40 | $push(list, next);
|
---|
| 41 | index += 1;
|
---|
| 42 | }
|
---|
| 43 | return list;
|
---|
| 44 | };
|
---|