40 lines
1.3 KiB
JavaScript
40 lines
1.3 KiB
JavaScript
|
'use strict';
|
||
|
// https://tc39.github.io/proposal-flatMap/#sec-FlattenIntoArray
|
||
|
var isArray = require('./_is-array');
|
||
|
var isObject = require('./_is-object');
|
||
|
var toLength = require('./_to-length');
|
||
|
var ctx = require('./_ctx');
|
||
|
var IS_CONCAT_SPREADABLE = require('./_wks')('isConcatSpreadable');
|
||
|
|
||
|
function flattenIntoArray(target, original, source, sourceLen, start, depth, mapper, thisArg) {
|
||
|
var targetIndex = start;
|
||
|
var sourceIndex = 0;
|
||
|
var mapFn = mapper ? ctx(mapper, thisArg, 3) : false;
|
||
|
var element, spreadable;
|
||
|
|
||
|
while (sourceIndex < sourceLen) {
|
||
|
if (sourceIndex in source) {
|
||
|
element = mapFn ? mapFn(source[sourceIndex], sourceIndex, original) : source[sourceIndex];
|
||
|
|
||
|
spreadable = false;
|
||
|
if (isObject(element)) {
|
||
|
spreadable = element[IS_CONCAT_SPREADABLE];
|
||
|
spreadable = spreadable !== undefined ? !!spreadable : isArray(element);
|
||
|
}
|
||
|
|
||
|
if (spreadable && depth > 0) {
|
||
|
targetIndex = flattenIntoArray(target, original, element, toLength(element.length), targetIndex, depth - 1) - 1;
|
||
|
} else {
|
||
|
if (targetIndex >= 0x1fffffffffffff) throw TypeError();
|
||
|
target[targetIndex] = element;
|
||
|
}
|
||
|
|
||
|
targetIndex++;
|
||
|
}
|
||
|
sourceIndex++;
|
||
|
}
|
||
|
return targetIndex;
|
||
|
}
|
||
|
|
||
|
module.exports = flattenIntoArray;
|