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 ToLength = require('./ToLength');
|
---|
12 | var ToString = require('./ToString');
|
---|
13 | var Type = require('./Type');
|
---|
14 |
|
---|
15 | var defaultElementTypes = ['Undefined', 'Null', 'Boolean', 'String', 'Symbol', 'Number', 'Object'];
|
---|
16 |
|
---|
17 | // https://262.ecma-international.org/6.0/#sec-createlistfromarraylike
|
---|
18 | module.exports = function CreateListFromArrayLike(obj) {
|
---|
19 | var elementTypes = arguments.length > 1
|
---|
20 | ? arguments[1]
|
---|
21 | : defaultElementTypes;
|
---|
22 |
|
---|
23 | if (Type(obj) !== 'Object') {
|
---|
24 | throw new $TypeError('Assertion failed: `obj` must be an Object');
|
---|
25 | }
|
---|
26 | if (!IsArray(elementTypes)) {
|
---|
27 | throw new $TypeError('Assertion failed: `elementTypes`, if provided, must be an array');
|
---|
28 | }
|
---|
29 | var len = ToLength(Get(obj, 'length'));
|
---|
30 | var list = [];
|
---|
31 | var index = 0;
|
---|
32 | while (index < len) {
|
---|
33 | var indexName = ToString(index);
|
---|
34 | var next = Get(obj, indexName);
|
---|
35 | var nextType = Type(next);
|
---|
36 | if ($indexOf(elementTypes, nextType) < 0) {
|
---|
37 | throw new $TypeError('item type ' + nextType + ' is not a valid elementType');
|
---|
38 | }
|
---|
39 | $push(list, next);
|
---|
40 | index += 1;
|
---|
41 | }
|
---|
42 | return list;
|
---|
43 | };
|
---|