es-toolkit 1.13.1-dev.358 → 1.13.1-dev.360
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 +2 -1
- package/dist/compat/index.d.ts +2 -1
- package/dist/compat/index.js +30 -1
- package/dist/compat/index.mjs +2 -1
- package/dist/index.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.mjs +1 -0
- package/dist/string/index.d.mts +1 -0
- package/dist/string/index.d.ts +1 -0
- package/dist/string/index.js +8 -0
- package/dist/string/index.mjs +1 -0
- package/dist/string/padStart.d.mts +20 -0
- package/dist/string/padStart.d.ts +20 -0
- package/dist/string/padStart.mjs +8 -0
- 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';
|
|
@@ -93,10 +92,12 @@ export { kebabCase } from '../string/kebabCase.mjs';
|
|
|
93
92
|
export { lowerCase } from '../string/lowerCase.mjs';
|
|
94
93
|
export { startCase } from '../string/startCase.mjs';
|
|
95
94
|
export { capitalize } from '../string/capitalize.mjs';
|
|
95
|
+
export { padStart } from '../string/padStart.mjs';
|
|
96
96
|
export { chunk } from './array/chunk.mjs';
|
|
97
97
|
export { concat } from './array/concat.mjs';
|
|
98
98
|
export { difference } from './array/difference.mjs';
|
|
99
99
|
export { fill } from './array/fill.mjs';
|
|
100
|
+
export { flatten } from './array/flatten.mjs';
|
|
100
101
|
export { zipObjectDeep } from './array/zipObjectDeep.mjs';
|
|
101
102
|
export { get } from './object/get.mjs';
|
|
102
103
|
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';
|
|
@@ -93,10 +92,12 @@ export { kebabCase } from '../string/kebabCase.js';
|
|
|
93
92
|
export { lowerCase } from '../string/lowerCase.js';
|
|
94
93
|
export { startCase } from '../string/startCase.js';
|
|
95
94
|
export { capitalize } from '../string/capitalize.js';
|
|
95
|
+
export { padStart } from '../string/padStart.js';
|
|
96
96
|
export { chunk } from './array/chunk.js';
|
|
97
97
|
export { concat } from './array/concat.js';
|
|
98
98
|
export { difference } from './array/difference.js';
|
|
99
99
|
export { fill } from './array/fill.js';
|
|
100
|
+
export { flatten } from './array/flatten.js';
|
|
100
101
|
export { zipObjectDeep } from './array/zipObjectDeep.js';
|
|
101
102
|
export { get } from './object/get.js';
|
|
102
103
|
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;
|
|
@@ -464,6 +491,7 @@ exports.camelCase = string_index.camelCase;
|
|
|
464
491
|
exports.capitalize = string_index.capitalize;
|
|
465
492
|
exports.kebabCase = string_index.kebabCase;
|
|
466
493
|
exports.lowerCase = string_index.lowerCase;
|
|
494
|
+
exports.padStart = string_index.padStart;
|
|
467
495
|
exports.snakeCase = string_index.snakeCase;
|
|
468
496
|
exports.startCase = string_index.startCase;
|
|
469
497
|
exports.chunk = chunk;
|
|
@@ -471,6 +499,7 @@ exports.concat = concat;
|
|
|
471
499
|
exports.difference = difference;
|
|
472
500
|
exports.endsWith = endsWith;
|
|
473
501
|
exports.fill = fill;
|
|
502
|
+
exports.flatten = flatten;
|
|
474
503
|
exports.get = get;
|
|
475
504
|
exports.isArray = isArray;
|
|
476
505
|
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';
|
|
@@ -93,10 +92,12 @@ export { kebabCase } from '../string/kebabCase.mjs';
|
|
|
93
92
|
export { lowerCase } from '../string/lowerCase.mjs';
|
|
94
93
|
export { startCase } from '../string/startCase.mjs';
|
|
95
94
|
export { capitalize } from '../string/capitalize.mjs';
|
|
95
|
+
export { padStart } from '../string/padStart.mjs';
|
|
96
96
|
export { chunk } from './array/chunk.mjs';
|
|
97
97
|
export { concat } from './array/concat.mjs';
|
|
98
98
|
export { difference } from './array/difference.mjs';
|
|
99
99
|
export { fill } from './array/fill.mjs';
|
|
100
|
+
export { flatten } from './array/flatten.mjs';
|
|
100
101
|
export { zipObjectDeep } from './array/zipObjectDeep.mjs';
|
|
101
102
|
export { get } from './object/get.mjs';
|
|
102
103
|
export { set } from './object/set.mjs';
|
package/dist/index.d.mts
CHANGED
|
@@ -99,3 +99,4 @@ export { kebabCase } from './string/kebabCase.mjs';
|
|
|
99
99
|
export { lowerCase } from './string/lowerCase.mjs';
|
|
100
100
|
export { startCase } from './string/startCase.mjs';
|
|
101
101
|
export { capitalize } from './string/capitalize.mjs';
|
|
102
|
+
export { padStart } from './string/padStart.mjs';
|
package/dist/index.d.ts
CHANGED
|
@@ -99,3 +99,4 @@ export { kebabCase } from './string/kebabCase.js';
|
|
|
99
99
|
export { lowerCase } from './string/lowerCase.js';
|
|
100
100
|
export { startCase } from './string/startCase.js';
|
|
101
101
|
export { capitalize } from './string/capitalize.js';
|
|
102
|
+
export { padStart } from './string/padStart.js';
|
package/dist/index.js
CHANGED
|
@@ -114,5 +114,6 @@ exports.camelCase = string_index.camelCase;
|
|
|
114
114
|
exports.capitalize = string_index.capitalize;
|
|
115
115
|
exports.kebabCase = string_index.kebabCase;
|
|
116
116
|
exports.lowerCase = string_index.lowerCase;
|
|
117
|
+
exports.padStart = string_index.padStart;
|
|
117
118
|
exports.snakeCase = string_index.snakeCase;
|
|
118
119
|
exports.startCase = string_index.startCase;
|
package/dist/index.mjs
CHANGED
|
@@ -99,3 +99,4 @@ export { kebabCase } from './string/kebabCase.mjs';
|
|
|
99
99
|
export { lowerCase } from './string/lowerCase.mjs';
|
|
100
100
|
export { startCase } from './string/startCase.mjs';
|
|
101
101
|
export { capitalize } from './string/capitalize.mjs';
|
|
102
|
+
export { padStart } from './string/padStart.mjs';
|
package/dist/string/index.d.mts
CHANGED
package/dist/string/index.d.ts
CHANGED
package/dist/string/index.js
CHANGED
|
@@ -52,9 +52,17 @@ function startCase(str) {
|
|
|
52
52
|
return result;
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
+
const padStart = (str, length = 0, chars = ' ') => {
|
|
56
|
+
if (Number.isSafeInteger(length) && length > str.length && chars.length > 0) {
|
|
57
|
+
return str.padStart(length, chars);
|
|
58
|
+
}
|
|
59
|
+
return str;
|
|
60
|
+
};
|
|
61
|
+
|
|
55
62
|
exports.camelCase = camelCase;
|
|
56
63
|
exports.capitalize = capitalize;
|
|
57
64
|
exports.kebabCase = kebabCase;
|
|
58
65
|
exports.lowerCase = lowerCase;
|
|
66
|
+
exports.padStart = padStart;
|
|
59
67
|
exports.snakeCase = snakeCase;
|
|
60
68
|
exports.startCase = startCase;
|
package/dist/string/index.mjs
CHANGED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pads the start of a string with a given character until it reaches the specified length.
|
|
3
|
+
* If the length is less than or equal to the original string's length, or if the padding character is an empty string,
|
|
4
|
+
* the original string is returned unchanged.
|
|
5
|
+
*
|
|
6
|
+
* @template T - The type of the input string.
|
|
7
|
+
* @param {T} str - The string to pad.
|
|
8
|
+
* @param {number} [length=0] - The length of the resulting string once padded. Default is 0.
|
|
9
|
+
* @param {string} [chars=' '] - The character(s) to use for padding. Default is a single space.
|
|
10
|
+
* @returns {string} - The padded string, or the original string if padding is not required.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const result1 = padStart('abc', 6); // result will be ' abc'
|
|
14
|
+
* const result2 = padStart('abc', 6, '_-'); // result will be '_-_abc'
|
|
15
|
+
* const result3 = padStart('abc', 3); // result will be 'abc'
|
|
16
|
+
* const result4 = padStart('abc', 2); // result will be 'abc'
|
|
17
|
+
*/
|
|
18
|
+
declare const padStart: <T extends string>(str: T, length?: number, chars?: string) => string;
|
|
19
|
+
|
|
20
|
+
export { padStart };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pads the start of a string with a given character until it reaches the specified length.
|
|
3
|
+
* If the length is less than or equal to the original string's length, or if the padding character is an empty string,
|
|
4
|
+
* the original string is returned unchanged.
|
|
5
|
+
*
|
|
6
|
+
* @template T - The type of the input string.
|
|
7
|
+
* @param {T} str - The string to pad.
|
|
8
|
+
* @param {number} [length=0] - The length of the resulting string once padded. Default is 0.
|
|
9
|
+
* @param {string} [chars=' '] - The character(s) to use for padding. Default is a single space.
|
|
10
|
+
* @returns {string} - The padded string, or the original string if padding is not required.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const result1 = padStart('abc', 6); // result will be ' abc'
|
|
14
|
+
* const result2 = padStart('abc', 6, '_-'); // result will be '_-_abc'
|
|
15
|
+
* const result3 = padStart('abc', 3); // result will be 'abc'
|
|
16
|
+
* const result4 = padStart('abc', 2); // result will be 'abc'
|
|
17
|
+
*/
|
|
18
|
+
declare const padStart: <T extends string>(str: T, length?: number, chars?: string) => string;
|
|
19
|
+
|
|
20
|
+
export { padStart };
|
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.360+8249ed25",
|
|
5
5
|
"homepage": "https://es-toolkit.slash.page",
|
|
6
6
|
"bugs": "https://github.com/toss/es-toolkit/issues",
|
|
7
7
|
"repository": {
|