1 | 'use strict';
|
---|
2 | var isArray = require('../internals/is-array');
|
---|
3 | var lengthOfArrayLike = require('../internals/length-of-array-like');
|
---|
4 | var doesNotExceedSafeInteger = require('../internals/does-not-exceed-safe-integer');
|
---|
5 | var bind = require('../internals/function-bind-context');
|
---|
6 |
|
---|
7 | // `FlattenIntoArray` abstract operation
|
---|
8 | // https://tc39.github.io/proposal-flatMap/#sec-FlattenIntoArray
|
---|
9 | var flattenIntoArray = function (target, original, source, sourceLen, start, depth, mapper, thisArg) {
|
---|
10 | var targetIndex = start;
|
---|
11 | var sourceIndex = 0;
|
---|
12 | var mapFn = mapper ? bind(mapper, thisArg) : false;
|
---|
13 | var element, elementLen;
|
---|
14 |
|
---|
15 | while (sourceIndex < sourceLen) {
|
---|
16 | if (sourceIndex in source) {
|
---|
17 | element = mapFn ? mapFn(source[sourceIndex], sourceIndex, original) : source[sourceIndex];
|
---|
18 |
|
---|
19 | if (depth > 0 && isArray(element)) {
|
---|
20 | elementLen = lengthOfArrayLike(element);
|
---|
21 | targetIndex = flattenIntoArray(target, original, element, elementLen, targetIndex, depth - 1) - 1;
|
---|
22 | } else {
|
---|
23 | doesNotExceedSafeInteger(targetIndex + 1);
|
---|
24 | target[targetIndex] = element;
|
---|
25 | }
|
---|
26 |
|
---|
27 | targetIndex++;
|
---|
28 | }
|
---|
29 | sourceIndex++;
|
---|
30 | }
|
---|
31 | return targetIndex;
|
---|
32 | };
|
---|
33 |
|
---|
34 | module.exports = flattenIntoArray;
|
---|