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