es-toolkit 1.13.1-dev.358 → 1.13.1-dev.359
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser.global.js +1 -1
- package/dist/browser.global.js.map +1 -1
- package/dist/compat/array/flatten.d.mts +19 -0
- package/dist/compat/array/flatten.d.ts +19 -0
- package/dist/compat/array/flatten.mjs +29 -0
- package/dist/compat/index.d.mts +1 -1
- package/dist/compat/index.d.ts +1 -1
- package/dist/compat/index.js +29 -1
- package/dist/compat/index.mjs +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flattens an array up to the specified depth.
|
|
3
|
+
*
|
|
4
|
+
* @template T - The type of elements within the array.
|
|
5
|
+
* @template D - The depth to which the array should be flattened.
|
|
6
|
+
* @param {T[] | object} value - The object to flatten.
|
|
7
|
+
* @param {D} depth - The depth level specifying how deep a nested array structure should be flattened. Defaults to 1.
|
|
8
|
+
* @returns {Array<FlatArray<T[], D>> | []} A new array that has been flattened.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const arr = flatten([1, [2, 3], [4, [5, 6]]], 1);
|
|
12
|
+
* // Returns: [1, 2, 3, 4, [5, 6]]
|
|
13
|
+
*
|
|
14
|
+
* const arr = flatten([1, [2, 3], [4, [5, 6]]], 2);
|
|
15
|
+
* // Returns: [1, 2, 3, 4, 5, 6]
|
|
16
|
+
*/
|
|
17
|
+
declare function flatten<T, D extends number = 1>(value: T[] | object, depth?: D): Array<FlatArray<T[], D>> | [];
|
|
18
|
+
|
|
19
|
+
export { flatten };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flattens an array up to the specified depth.
|
|
3
|
+
*
|
|
4
|
+
* @template T - The type of elements within the array.
|
|
5
|
+
* @template D - The depth to which the array should be flattened.
|
|
6
|
+
* @param {T[] | object} value - The object to flatten.
|
|
7
|
+
* @param {D} depth - The depth level specifying how deep a nested array structure should be flattened. Defaults to 1.
|
|
8
|
+
* @returns {Array<FlatArray<T[], D>> | []} A new array that has been flattened.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const arr = flatten([1, [2, 3], [4, [5, 6]]], 1);
|
|
12
|
+
* // Returns: [1, 2, 3, 4, [5, 6]]
|
|
13
|
+
*
|
|
14
|
+
* const arr = flatten([1, [2, 3], [4, [5, 6]]], 2);
|
|
15
|
+
* // Returns: [1, 2, 3, 4, 5, 6]
|
|
16
|
+
*/
|
|
17
|
+
declare function flatten<T, D extends number = 1>(value: T[] | object, depth?: D): Array<FlatArray<T[], D>> | [];
|
|
18
|
+
|
|
19
|
+
export { flatten };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
function flatten(value, depth = 1) {
|
|
2
|
+
const result = [];
|
|
3
|
+
const flooredDepth = Math.floor(depth);
|
|
4
|
+
if (!Array.isArray(value)) {
|
|
5
|
+
return result;
|
|
6
|
+
}
|
|
7
|
+
const recursive = (arr, currentDepth) => {
|
|
8
|
+
for (const item of arr) {
|
|
9
|
+
if (currentDepth < flooredDepth &&
|
|
10
|
+
(Array.isArray(item) ||
|
|
11
|
+
Boolean(item?.[Symbol.isConcatSpreadable]) ||
|
|
12
|
+
(item !== null && typeof item === 'object' && Object.prototype.toString.call(item) === '[object Arguments]'))) {
|
|
13
|
+
if (Array.isArray(item)) {
|
|
14
|
+
recursive(item, currentDepth + 1);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
recursive(Array.from(item), currentDepth + 1);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
result.push(item);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
recursive(value, 0);
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export { flatten };
|
package/dist/compat/index.d.mts
CHANGED
|
@@ -7,7 +7,6 @@ export { dropRight } from '../array/dropRight.mjs';
|
|
|
7
7
|
export { dropRightWhile } from '../array/dropRightWhile.mjs';
|
|
8
8
|
export { dropWhile } from '../array/dropWhile.mjs';
|
|
9
9
|
export { flatMap } from '../array/flatMap.mjs';
|
|
10
|
-
export { flatten } from '../array/flatten.mjs';
|
|
11
10
|
export { flattenDeep } from '../array/flattenDeep.mjs';
|
|
12
11
|
export { forEachRight } from '../array/forEachRight.mjs';
|
|
13
12
|
export { groupBy } from '../array/groupBy.mjs';
|
|
@@ -97,6 +96,7 @@ export { chunk } from './array/chunk.mjs';
|
|
|
97
96
|
export { concat } from './array/concat.mjs';
|
|
98
97
|
export { difference } from './array/difference.mjs';
|
|
99
98
|
export { fill } from './array/fill.mjs';
|
|
99
|
+
export { flatten } from './array/flatten.mjs';
|
|
100
100
|
export { zipObjectDeep } from './array/zipObjectDeep.mjs';
|
|
101
101
|
export { get } from './object/get.mjs';
|
|
102
102
|
export { set } from './object/set.mjs';
|
package/dist/compat/index.d.ts
CHANGED
|
@@ -7,7 +7,6 @@ export { dropRight } from '../array/dropRight.js';
|
|
|
7
7
|
export { dropRightWhile } from '../array/dropRightWhile.js';
|
|
8
8
|
export { dropWhile } from '../array/dropWhile.js';
|
|
9
9
|
export { flatMap } from '../array/flatMap.js';
|
|
10
|
-
export { flatten } from '../array/flatten.js';
|
|
11
10
|
export { flattenDeep } from '../array/flattenDeep.js';
|
|
12
11
|
export { forEachRight } from '../array/forEachRight.js';
|
|
13
12
|
export { groupBy } from '../array/groupBy.js';
|
|
@@ -97,6 +96,7 @@ export { chunk } from './array/chunk.js';
|
|
|
97
96
|
export { concat } from './array/concat.js';
|
|
98
97
|
export { difference } from './array/difference.js';
|
|
99
98
|
export { fill } from './array/fill.js';
|
|
99
|
+
export { flatten } from './array/flatten.js';
|
|
100
100
|
export { zipObjectDeep } from './array/zipObjectDeep.js';
|
|
101
101
|
export { get } from './object/get.js';
|
|
102
102
|
export { set } from './object/set.js';
|
package/dist/compat/index.js
CHANGED
|
@@ -43,6 +43,34 @@ function fill(array, value, start = 0, end = array.length) {
|
|
|
43
43
|
return array_index.fill(array, value, start, end);
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
+
function flatten(value, depth = 1) {
|
|
47
|
+
const result = [];
|
|
48
|
+
const flooredDepth = Math.floor(depth);
|
|
49
|
+
if (!Array.isArray(value)) {
|
|
50
|
+
return result;
|
|
51
|
+
}
|
|
52
|
+
const recursive = (arr, currentDepth) => {
|
|
53
|
+
for (const item of arr) {
|
|
54
|
+
if (currentDepth < flooredDepth &&
|
|
55
|
+
(Array.isArray(item) ||
|
|
56
|
+
Boolean(item?.[Symbol.isConcatSpreadable]) ||
|
|
57
|
+
(item !== null && typeof item === 'object' && Object.prototype.toString.call(item) === '[object Arguments]'))) {
|
|
58
|
+
if (Array.isArray(item)) {
|
|
59
|
+
recursive(item, currentDepth + 1);
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
recursive(Array.from(item), currentDepth + 1);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
result.push(item);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
recursive(value, 0);
|
|
71
|
+
return result;
|
|
72
|
+
}
|
|
73
|
+
|
|
46
74
|
const IS_UNSIGNED_INTEGER = /^(?:0|[1-9]\d*)$/;
|
|
47
75
|
function isIndex(value) {
|
|
48
76
|
switch (typeof value) {
|
|
@@ -380,7 +408,6 @@ exports.dropRightWhile = array_index.dropRightWhile;
|
|
|
380
408
|
exports.dropWhile = array_index.dropWhile;
|
|
381
409
|
exports.first = array_index.head;
|
|
382
410
|
exports.flatMap = array_index.flatMap;
|
|
383
|
-
exports.flatten = array_index.flatten;
|
|
384
411
|
exports.flattenDeep = array_index.flattenDeep;
|
|
385
412
|
exports.forEachRight = array_index.forEachRight;
|
|
386
413
|
exports.groupBy = array_index.groupBy;
|
|
@@ -471,6 +498,7 @@ exports.concat = concat;
|
|
|
471
498
|
exports.difference = difference;
|
|
472
499
|
exports.endsWith = endsWith;
|
|
473
500
|
exports.fill = fill;
|
|
501
|
+
exports.flatten = flatten;
|
|
474
502
|
exports.get = get;
|
|
475
503
|
exports.isArray = isArray;
|
|
476
504
|
exports.isMatch = isMatch;
|
package/dist/compat/index.mjs
CHANGED
|
@@ -7,7 +7,6 @@ export { dropRight } from '../array/dropRight.mjs';
|
|
|
7
7
|
export { dropRightWhile } from '../array/dropRightWhile.mjs';
|
|
8
8
|
export { dropWhile } from '../array/dropWhile.mjs';
|
|
9
9
|
export { flatMap } from '../array/flatMap.mjs';
|
|
10
|
-
export { flatten } from '../array/flatten.mjs';
|
|
11
10
|
export { flattenDeep } from '../array/flattenDeep.mjs';
|
|
12
11
|
export { forEachRight } from '../array/forEachRight.mjs';
|
|
13
12
|
export { groupBy } from '../array/groupBy.mjs';
|
|
@@ -97,6 +96,7 @@ export { chunk } from './array/chunk.mjs';
|
|
|
97
96
|
export { concat } from './array/concat.mjs';
|
|
98
97
|
export { difference } from './array/difference.mjs';
|
|
99
98
|
export { fill } from './array/fill.mjs';
|
|
99
|
+
export { flatten } from './array/flatten.mjs';
|
|
100
100
|
export { zipObjectDeep } from './array/zipObjectDeep.mjs';
|
|
101
101
|
export { get } from './object/get.mjs';
|
|
102
102
|
export { set } from './object/set.mjs';
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "es-toolkit",
|
|
3
3
|
"description": "A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.",
|
|
4
|
-
"version": "1.13.1-dev.
|
|
4
|
+
"version": "1.13.1-dev.359+2d1c4063",
|
|
5
5
|
"homepage": "https://es-toolkit.slash.page",
|
|
6
6
|
"bugs": "https://github.com/toss/es-toolkit/issues",
|
|
7
7
|
"repository": {
|