1 | 'use strict';
|
---|
2 |
|
---|
3 | var $TypeError = require('es-errors/type');
|
---|
4 |
|
---|
5 | var MAX_SAFE_INTEGER = require('../helpers/maxSafeInteger');
|
---|
6 |
|
---|
7 | var Call = require('./Call');
|
---|
8 | var CreateDataPropertyOrThrow = require('./CreateDataPropertyOrThrow');
|
---|
9 | var Get = require('./Get');
|
---|
10 | var HasProperty = require('./HasProperty');
|
---|
11 | var IsArray = require('./IsArray');
|
---|
12 | var ToLength = require('./ToLength');
|
---|
13 | var ToString = require('./ToString');
|
---|
14 |
|
---|
15 | // https://262.ecma-international.org/10.0/#sec-flattenintoarray
|
---|
16 |
|
---|
17 | module.exports = function FlattenIntoArray(target, source, sourceLen, start, depth) {
|
---|
18 | var mapperFunction;
|
---|
19 | if (arguments.length > 5) {
|
---|
20 | mapperFunction = arguments[5];
|
---|
21 | }
|
---|
22 |
|
---|
23 | var targetIndex = start;
|
---|
24 | var sourceIndex = 0;
|
---|
25 | while (sourceIndex < sourceLen) {
|
---|
26 | var P = ToString(sourceIndex);
|
---|
27 | var exists = HasProperty(source, P);
|
---|
28 | if (exists === true) {
|
---|
29 | var element = Get(source, P);
|
---|
30 | if (typeof mapperFunction !== 'undefined') {
|
---|
31 | if (arguments.length <= 6) {
|
---|
32 | throw new $TypeError('Assertion failed: thisArg is required when mapperFunction is provided');
|
---|
33 | }
|
---|
34 | element = Call(mapperFunction, arguments[6], [element, sourceIndex, source]);
|
---|
35 | }
|
---|
36 | var shouldFlatten = false;
|
---|
37 | if (depth > 0) {
|
---|
38 | shouldFlatten = IsArray(element);
|
---|
39 | }
|
---|
40 | if (shouldFlatten) {
|
---|
41 | var elementLen = ToLength(Get(element, 'length'));
|
---|
42 | targetIndex = FlattenIntoArray(target, element, elementLen, targetIndex, depth - 1);
|
---|
43 | } else {
|
---|
44 | if (targetIndex >= MAX_SAFE_INTEGER) {
|
---|
45 | throw new $TypeError('index too large');
|
---|
46 | }
|
---|
47 | CreateDataPropertyOrThrow(target, ToString(targetIndex), element);
|
---|
48 | targetIndex += 1;
|
---|
49 | }
|
---|
50 | }
|
---|
51 | sourceIndex += 1;
|
---|
52 | }
|
---|
53 |
|
---|
54 | return targetIndex;
|
---|
55 | };
|
---|