es-toolkit 1.8.0 → 1.9.0-dev.170

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.
Files changed (64) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/dist/array/flatten.d.mts +19 -0
  3. package/dist/array/flatten.d.ts +19 -0
  4. package/dist/array/flatten.js +45 -0
  5. package/dist/array/flatten.js.map +1 -0
  6. package/dist/array/flatten.mjs +8 -0
  7. package/dist/array/head.d.mts +22 -0
  8. package/dist/array/head.d.ts +22 -0
  9. package/dist/array/head.js +33 -0
  10. package/dist/array/head.js.map +1 -0
  11. package/dist/array/head.mjs +8 -0
  12. package/dist/array/head.mjs.map +1 -0
  13. package/dist/array/index.d.mts +4 -0
  14. package/dist/array/index.d.ts +4 -0
  15. package/dist/array/index.js +61 -0
  16. package/dist/array/index.js.map +1 -1
  17. package/dist/array/index.mjs +59 -43
  18. package/dist/array/tail.d.mts +38 -0
  19. package/dist/array/tail.d.ts +38 -0
  20. package/dist/array/tail.js +41 -0
  21. package/dist/array/tail.js.map +1 -0
  22. package/dist/array/tail.mjs +8 -0
  23. package/dist/array/tail.mjs.map +1 -0
  24. package/dist/array/unzip.d.mts +18 -0
  25. package/dist/array/unzip.d.ts +18 -0
  26. package/dist/array/unzip.js +46 -0
  27. package/dist/array/unzip.js.map +1 -0
  28. package/dist/array/unzip.mjs +8 -0
  29. package/dist/array/unzip.mjs.map +1 -0
  30. package/dist/chunk-33MSBDC6.mjs +21 -0
  31. package/dist/chunk-33MSBDC6.mjs.map +1 -0
  32. package/dist/{chunk-FLTT2MGF.mjs → chunk-74WWMNZS.mjs} +1 -1
  33. package/dist/{chunk-FLTT2MGF.mjs.map → chunk-74WWMNZS.mjs.map} +1 -1
  34. package/dist/{chunk-UNLX2F2Y.mjs → chunk-DPH2KE6O.mjs} +1 -1
  35. package/dist/chunk-DPH2KE6O.mjs.map +1 -0
  36. package/dist/chunk-HM2JT2RX.mjs +9 -0
  37. package/dist/chunk-HM2JT2RX.mjs.map +1 -0
  38. package/dist/chunk-LOY6PATI.mjs +1 -0
  39. package/dist/chunk-LOY6PATI.mjs.map +1 -0
  40. package/dist/chunk-OF6NM4G7.mjs +17 -0
  41. package/dist/chunk-OF6NM4G7.mjs.map +1 -0
  42. package/dist/chunk-UYZKMBDO.mjs +22 -0
  43. package/dist/chunk-UYZKMBDO.mjs.map +1 -0
  44. package/dist/index.d.mts +4 -0
  45. package/dist/index.d.ts +4 -0
  46. package/dist/index.js +61 -0
  47. package/dist/index.js.map +1 -1
  48. package/dist/index.mjs +101 -85
  49. package/dist/math/index.js.map +1 -1
  50. package/dist/math/index.mjs +3 -3
  51. package/dist/math/range.d.mts +5 -6
  52. package/dist/math/range.d.ts +5 -6
  53. package/dist/math/range.js.map +1 -1
  54. package/dist/math/range.mjs +1 -1
  55. package/dist/object/index.js.map +1 -1
  56. package/dist/object/index.mjs +6 -6
  57. package/dist/object/invert.d.mts +0 -1
  58. package/dist/object/invert.d.ts +0 -1
  59. package/dist/object/invert.js.map +1 -1
  60. package/dist/object/invert.mjs +1 -1
  61. package/package.json +1 -1
  62. package/dist/chunk-OC7ZNKWX.mjs +0 -1
  63. package/dist/chunk-UNLX2F2Y.mjs.map +0 -1
  64. /package/dist/{chunk-OC7ZNKWX.mjs.map → array/flatten.mjs.map} +0 -0
package/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # es-toolkit Changelog
2
2
 
3
+ ## Version v1.9.0
4
+
5
+ Released on July 10th, 2024.
6
+
7
+ - Add support for [head](https://es-toolkit.slash.page/reference/array/head.html) and [tail](https://es-toolkit.slash.page/reference/array/tail.html). (https://github.com/toss/es-toolkit/pull/131, https://github.com/toss/es-toolkit/pull/143).
8
+ - Add support for [unzip](https://es-toolkit.slash.page/reference/array/unzip.html). (https://github.com/toss/es-toolkit/pull/130)
9
+ - Add support for [flatten](https://es-toolkit.slash.page/reference/array/flatten.html), which is six times faster than `Array#flat`. (https://github.com/toss/es-toolkit/pull/147)
10
+
11
+
3
12
  ## Version v1.8.0
4
13
 
5
14
  Released on July 5th, 2024.
@@ -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[]} arr - The array 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>(arr: readonly T[], 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[]} arr - The array 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>(arr: readonly T[], depth?: D): Array<FlatArray<T[], D>>;
18
+
19
+ export { flatten };
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/array/flatten.ts
21
+ var flatten_exports = {};
22
+ __export(flatten_exports, {
23
+ flatten: () => flatten
24
+ });
25
+ module.exports = __toCommonJS(flatten_exports);
26
+ function flatten(arr, depth = 1) {
27
+ const result = [];
28
+ const flooredDepth = Math.floor(depth);
29
+ const recursive = (arr2, currentDepth) => {
30
+ for (const item of arr2) {
31
+ if (Array.isArray(item) && currentDepth < flooredDepth) {
32
+ recursive(item, currentDepth + 1);
33
+ } else {
34
+ result.push(item);
35
+ }
36
+ }
37
+ };
38
+ recursive(arr, 0);
39
+ return result;
40
+ }
41
+ // Annotate the CommonJS export names for ESM import in node:
42
+ 0 && (module.exports = {
43
+ flatten
44
+ });
45
+ //# sourceMappingURL=flatten.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/array/flatten.ts"],"sourcesContent":["/**\n * Flattens an array up to the specified depth.\n *\n * @template T - The type of elements within the array.\n * @template D - The depth to which the array should be flattened.\n * @param {T[]} arr - The array to flatten.\n * @param {D} depth - The depth level specifying how deep a nested array structure should be flattened. Defaults to 1.\n * @returns {Array<FlatArray<T[], D>>} A new array that has been flattened.\n *\n * @example\n * const arr = flatten([1, [2, 3], [4, [5, 6]]], 1);\n * // Returns: [1, 2, 3, 4, [5, 6]]\n *\n * const arr = flatten([1, [2, 3], [4, [5, 6]]], 2);\n * // Returns: [1, 2, 3, 4, 5, 6]\n */\nexport function flatten<T, D extends number = 1>(arr: readonly T[], depth = 1 as D): Array<FlatArray<T[], D>> {\n const result: Array<FlatArray<T[], D>> = [];\n const flooredDepth = Math.floor(depth);\n\n const recursive = (arr: readonly T[], currentDepth: number) => {\n for (const item of arr) {\n if (Array.isArray(item) && currentDepth < flooredDepth) {\n recursive(item, currentDepth + 1);\n } else {\n result.push(item as FlatArray<T[], D>);\n }\n }\n };\n\n recursive(arr, 0);\n return result;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAgBO,SAAS,QAAiC,KAAmB,QAAQ,GAAkC;AAC5G,QAAM,SAAmC,CAAC;AAC1C,QAAM,eAAe,KAAK,MAAM,KAAK;AAErC,QAAM,YAAY,CAACA,MAAmB,iBAAyB;AAC7D,eAAW,QAAQA,MAAK;AACtB,UAAI,MAAM,QAAQ,IAAI,KAAK,eAAe,cAAc;AACtD,kBAAU,MAAM,eAAe,CAAC;AAAA,MAClC,OAAO;AACL,eAAO,KAAK,IAAyB;AAAA,MACvC;AAAA,IACF;AAAA,EACF;AAEA,YAAU,KAAK,CAAC;AAChB,SAAO;AACT;","names":["arr"]}
@@ -0,0 +1,8 @@
1
+ import {
2
+ flatten
3
+ } from "../chunk-33MSBDC6.mjs";
4
+ import "../chunk-2MM5EJJX.mjs";
5
+ export {
6
+ flatten
7
+ };
8
+ //# sourceMappingURL=flatten.mjs.map
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Returns the first element of an array.
3
+ *
4
+ * This function takes an array and returns the first element of the array.
5
+ * If the array is empty, the function returns `undefined`.
6
+ *
7
+ * @param {T[]} arr - The array from which to get the first element.
8
+ * @returns {T | undefined} The first element of the array, or `undefined` if the array is empty.
9
+ *
10
+ * @example
11
+ * const arr = [1, 2, 3];
12
+ * const firstElement = head(arr);
13
+ * // firstElement will be 1
14
+ *
15
+ * const emptyArr: number[] = [];
16
+ * const noElement = head(emptyArr);
17
+ * // noElement will be undefined
18
+ */
19
+ declare function head<T>(arr: readonly [T, ...T[]]): T;
20
+ declare function head<T>(arr: readonly T[]): T | undefined;
21
+
22
+ export { head };
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Returns the first element of an array.
3
+ *
4
+ * This function takes an array and returns the first element of the array.
5
+ * If the array is empty, the function returns `undefined`.
6
+ *
7
+ * @param {T[]} arr - The array from which to get the first element.
8
+ * @returns {T | undefined} The first element of the array, or `undefined` if the array is empty.
9
+ *
10
+ * @example
11
+ * const arr = [1, 2, 3];
12
+ * const firstElement = head(arr);
13
+ * // firstElement will be 1
14
+ *
15
+ * const emptyArr: number[] = [];
16
+ * const noElement = head(emptyArr);
17
+ * // noElement will be undefined
18
+ */
19
+ declare function head<T>(arr: readonly [T, ...T[]]): T;
20
+ declare function head<T>(arr: readonly T[]): T | undefined;
21
+
22
+ export { head };
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/array/head.ts
21
+ var head_exports = {};
22
+ __export(head_exports, {
23
+ head: () => head
24
+ });
25
+ module.exports = __toCommonJS(head_exports);
26
+ function head(arr) {
27
+ return arr[0];
28
+ }
29
+ // Annotate the CommonJS export names for ESM import in node:
30
+ 0 && (module.exports = {
31
+ head
32
+ });
33
+ //# sourceMappingURL=head.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/array/head.ts"],"sourcesContent":["/**\n * Returns the first element of an array.\n *\n * This function takes an array and returns the first element of the array.\n * If the array is empty, the function returns `undefined`.\n *\n * @param {T[]} arr - The array from which to get the first element.\n * @returns {T | undefined} The first element of the array, or `undefined` if the array is empty.\n *\n * @example\n * const arr = [1, 2, 3];\n * const firstElement = head(arr);\n * // firstElement will be 1\n *\n * const emptyArr: number[] = [];\n * const noElement = head(emptyArr);\n * // noElement will be undefined\n */\nexport function head<T>(arr: readonly [T, ...T[]]): T;\nexport function head<T>(arr: readonly T[]): T | undefined;\nexport function head<T>(arr: readonly T[]): T | undefined {\n return arr[0];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAoBO,SAAS,KAAQ,KAAkC;AACxD,SAAO,IAAI,CAAC;AACd;","names":[]}
@@ -0,0 +1,8 @@
1
+ import {
2
+ head
3
+ } from "../chunk-HM2JT2RX.mjs";
4
+ import "../chunk-2MM5EJJX.mjs";
5
+ export {
6
+ head
7
+ };
8
+ //# sourceMappingURL=head.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -9,6 +9,7 @@ export { dropRight } from './dropRight.mjs';
9
9
  export { dropRightWhile } from './dropRightWhile.mjs';
10
10
  export { dropWhile } from './dropWhile.mjs';
11
11
  export { fill } from './fill.mjs';
12
+ export { flatten } from './flatten.mjs';
12
13
  export { forEachRight } from './forEachRight.mjs';
13
14
  export { groupBy } from './groupBy.mjs';
14
15
  export { intersection } from './intersection.mjs';
@@ -32,6 +33,7 @@ export { unionWith } from './unionWith.mjs';
32
33
  export { uniq } from './uniq.mjs';
33
34
  export { uniqBy } from './uniqBy.mjs';
34
35
  export { uniqWith } from './uniqWith.mjs';
36
+ export { unzip } from './unzip.mjs';
35
37
  export { unzipWith } from './unzipWith.mjs';
36
38
  export { xor } from './xor.mjs';
37
39
  export { xorBy } from './xorBy.mjs';
@@ -40,3 +42,5 @@ export { zip } from './zip.mjs';
40
42
  export { zipObject } from './zipObject.mjs';
41
43
  export { zipWith } from './zipWith.mjs';
42
44
  export { without } from './without.mjs';
45
+ export { head } from './head.mjs';
46
+ export { tail } from './tail.mjs';
@@ -9,6 +9,7 @@ export { dropRight } from './dropRight.js';
9
9
  export { dropRightWhile } from './dropRightWhile.js';
10
10
  export { dropWhile } from './dropWhile.js';
11
11
  export { fill } from './fill.js';
12
+ export { flatten } from './flatten.js';
12
13
  export { forEachRight } from './forEachRight.js';
13
14
  export { groupBy } from './groupBy.js';
14
15
  export { intersection } from './intersection.js';
@@ -32,6 +33,7 @@ export { unionWith } from './unionWith.js';
32
33
  export { uniq } from './uniq.js';
33
34
  export { uniqBy } from './uniqBy.js';
34
35
  export { uniqWith } from './uniqWith.js';
36
+ export { unzip } from './unzip.js';
35
37
  export { unzipWith } from './unzipWith.js';
36
38
  export { xor } from './xor.js';
37
39
  export { xorBy } from './xorBy.js';
@@ -40,3 +42,5 @@ export { zip } from './zip.js';
40
42
  export { zipObject } from './zipObject.js';
41
43
  export { zipWith } from './zipWith.js';
42
44
  export { without } from './without.js';
45
+ export { head } from './head.js';
46
+ export { tail } from './tail.js';
@@ -31,8 +31,10 @@ __export(array_exports, {
31
31
  dropRightWhile: () => dropRightWhile,
32
32
  dropWhile: () => dropWhile,
33
33
  fill: () => fill,
34
+ flatten: () => flatten,
34
35
  forEachRight: () => forEachRight,
35
36
  groupBy: () => groupBy,
37
+ head: () => head,
36
38
  intersection: () => intersection,
37
39
  intersectionBy: () => intersectionBy,
38
40
  intersectionWith: () => intersectionWith,
@@ -44,6 +46,7 @@ __export(array_exports, {
44
46
  sample: () => sample,
45
47
  sampleSize: () => sampleSize,
46
48
  shuffle: () => shuffle,
49
+ tail: () => tail,
47
50
  take: () => take,
48
51
  takeRight: () => takeRight,
49
52
  takeRightWhile: () => takeRightWhile,
@@ -54,6 +57,7 @@ __export(array_exports, {
54
57
  uniq: () => uniq,
55
58
  uniqBy: () => uniqBy,
56
59
  uniqWith: () => uniqWith,
60
+ unzip: () => unzip,
57
61
  unzipWith: () => unzipWith,
58
62
  without: () => without,
59
63
  xor: () => xor,
@@ -161,6 +165,23 @@ function fill(arr, value, start = 0, end = arr.length) {
161
165
  return arr;
162
166
  }
163
167
 
168
+ // src/array/flatten.ts
169
+ function flatten(arr, depth = 1) {
170
+ const result = [];
171
+ const flooredDepth = Math.floor(depth);
172
+ const recursive = (arr2, currentDepth) => {
173
+ for (const item of arr2) {
174
+ if (Array.isArray(item) && currentDepth < flooredDepth) {
175
+ recursive(item, currentDepth + 1);
176
+ } else {
177
+ result.push(item);
178
+ }
179
+ }
180
+ };
181
+ recursive(arr, 0);
182
+ return result;
183
+ }
184
+
164
185
  // src/array/forEachRight.ts
165
186
  function forEachRight(arr, callback) {
166
187
  for (let i = arr.length - 1; i >= 0; i--) {
@@ -419,6 +440,24 @@ function uniqBy(arr, mapper) {
419
440
  return Array.from(map.values());
420
441
  }
421
442
 
443
+ // src/array/unzip.ts
444
+ function unzip(zipped) {
445
+ let maxLen = 0;
446
+ for (let i = 0; i < zipped.length; i++) {
447
+ if (zipped[i].length > maxLen) {
448
+ maxLen = zipped[i].length;
449
+ }
450
+ }
451
+ const result = new Array(maxLen);
452
+ for (let i = 0; i < maxLen; i++) {
453
+ result[i] = new Array(zipped.length);
454
+ for (let j = 0; j < zipped.length; j++) {
455
+ result[i][j] = zipped[j][i];
456
+ }
457
+ }
458
+ return result;
459
+ }
460
+
422
461
  // src/array/unzipWith.ts
423
462
  function unzipWith(target, iteratee) {
424
463
  const maxLength = Math.max(...target.map((innerArray) => innerArray.length));
@@ -493,6 +532,24 @@ function without(array, ...values) {
493
532
  const valuesSet = new Set(values);
494
533
  return array.filter((item) => !valuesSet.has(item));
495
534
  }
535
+
536
+ // src/array/head.ts
537
+ function head(arr) {
538
+ return arr[0];
539
+ }
540
+
541
+ // src/array/tail.ts
542
+ function tail(arr) {
543
+ const len = arr.length;
544
+ if (len <= 1) {
545
+ return [];
546
+ }
547
+ const result = new Array(len - 1);
548
+ for (let i = 1; i < len; i++) {
549
+ result[i - 1] = arr[i];
550
+ }
551
+ return result;
552
+ }
496
553
  // Annotate the CommonJS export names for ESM import in node:
497
554
  0 && (module.exports = {
498
555
  chunk,
@@ -506,8 +563,10 @@ function without(array, ...values) {
506
563
  dropRightWhile,
507
564
  dropWhile,
508
565
  fill,
566
+ flatten,
509
567
  forEachRight,
510
568
  groupBy,
569
+ head,
511
570
  intersection,
512
571
  intersectionBy,
513
572
  intersectionWith,
@@ -519,6 +578,7 @@ function without(array, ...values) {
519
578
  sample,
520
579
  sampleSize,
521
580
  shuffle,
581
+ tail,
522
582
  take,
523
583
  takeRight,
524
584
  takeRightWhile,
@@ -529,6 +589,7 @@ function without(array, ...values) {
529
589
  uniq,
530
590
  uniqBy,
531
591
  uniqWith,
592
+ unzip,
532
593
  unzipWith,
533
594
  without,
534
595
  xor,