source: imaps-frontend/node_modules/es-abstract/2019/FlattenIntoArray.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.6 KB
Line 
1'use strict';
2
3var $TypeError = require('es-errors/type');
4
5var MAX_SAFE_INTEGER = require('../helpers/maxSafeInteger');
6
7var Call = require('./Call');
8var CreateDataPropertyOrThrow = require('./CreateDataPropertyOrThrow');
9var Get = require('./Get');
10var HasProperty = require('./HasProperty');
11var IsArray = require('./IsArray');
12var ToLength = require('./ToLength');
13var ToString = require('./ToString');
14
15// https://262.ecma-international.org/10.0/#sec-flattenintoarray
16
17module.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};
Note: See TracBrowser for help on using the repository browser.