|
Last change
on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago |
|
Added visualizations
|
-
Property mode
set to
100644
|
|
File size:
1.1 KB
|
| Rev | Line | |
|---|
| [a762898] | 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|---|
| 4 |
|
|---|
| 5 | const isArrayLike = require('../predicate/isArrayLike.js');
|
|---|
| 6 |
|
|---|
| 7 | function flatten(value, depth = 1) {
|
|---|
| 8 | const result = [];
|
|---|
| 9 | const flooredDepth = Math.floor(depth);
|
|---|
| 10 | if (!isArrayLike.isArrayLike(value)) {
|
|---|
| 11 | return result;
|
|---|
| 12 | }
|
|---|
| 13 | const recursive = (arr, currentDepth) => {
|
|---|
| 14 | for (let i = 0; i < arr.length; i++) {
|
|---|
| 15 | const item = arr[i];
|
|---|
| 16 | if (currentDepth < flooredDepth &&
|
|---|
| 17 | (Array.isArray(item) ||
|
|---|
| 18 | Boolean(item?.[Symbol.isConcatSpreadable]) ||
|
|---|
| 19 | (item !== null && typeof item === 'object' && Object.prototype.toString.call(item) === '[object Arguments]'))) {
|
|---|
| 20 | if (Array.isArray(item)) {
|
|---|
| 21 | recursive(item, currentDepth + 1);
|
|---|
| 22 | }
|
|---|
| 23 | else {
|
|---|
| 24 | recursive(Array.from(item), currentDepth + 1);
|
|---|
| 25 | }
|
|---|
| 26 | }
|
|---|
| 27 | else {
|
|---|
| 28 | result.push(item);
|
|---|
| 29 | }
|
|---|
| 30 | }
|
|---|
| 31 | };
|
|---|
| 32 | recursive(Array.from(value), 0);
|
|---|
| 33 | return result;
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | exports.flatten = flatten;
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.