es-toolkit 1.35.0 → 1.36.0-dev.1215

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 (106) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/_chunk/{isPromise-ByAybB.js → isPromise-CxqI1v.js} +122 -36
  3. package/dist/_chunk/{reverseString-D-wK2p.js → reverseString-BixeGz.js} +84 -13
  4. package/dist/_chunk/toSnakeCaseKeys-DZO2eB.js +4901 -0
  5. package/dist/_chunk/{unary-BsNWRM.js → unary-EIEhcF.js} +46 -0
  6. package/dist/_chunk/{zip-_HttSl.js → zip-BJSrRi.js} +38 -57
  7. package/dist/array/index.js +55 -6
  8. package/dist/array/pullAt.d.mts +0 -2
  9. package/dist/array/pullAt.d.ts +0 -2
  10. package/dist/array/take.d.mts +1 -1
  11. package/dist/array/take.d.ts +1 -1
  12. package/dist/array/take.mjs +4 -1
  13. package/dist/array/takeRight.d.mts +1 -1
  14. package/dist/array/takeRight.d.ts +1 -1
  15. package/dist/array/takeRight.mjs +5 -2
  16. package/dist/browser.global.js +1 -1
  17. package/dist/browser.global.js.map +1 -1
  18. package/dist/compat/_internal/copyArray.mjs +12 -0
  19. package/dist/compat/_internal/mapToEntries.mjs +11 -0
  20. package/dist/compat/_internal/setToEntries.mjs +11 -0
  21. package/dist/compat/_internal/toKey.mjs +2 -2
  22. package/dist/compat/array/find.mjs +3 -3
  23. package/dist/compat/array/findLast.mjs +2 -2
  24. package/dist/compat/array/flatMap.d.mts +20 -0
  25. package/dist/compat/array/flatMap.d.ts +20 -0
  26. package/dist/compat/array/flatMap.mjs +13 -0
  27. package/dist/compat/array/forEachRight.d.mts +106 -0
  28. package/dist/compat/array/forEachRight.d.ts +106 -0
  29. package/dist/compat/array/forEachRight.mjs +21 -0
  30. package/dist/compat/array/groupBy.d.mts +55 -0
  31. package/dist/compat/array/groupBy.d.ts +55 -0
  32. package/dist/compat/array/groupBy.mjs +15 -0
  33. package/dist/compat/array/partition.d.mts +65 -0
  34. package/dist/compat/array/partition.d.ts +65 -0
  35. package/dist/compat/array/partition.mjs +24 -0
  36. package/dist/compat/array/pullAllWith.d.mts +59 -0
  37. package/dist/compat/array/pullAllWith.d.ts +59 -0
  38. package/dist/compat/array/pullAllWith.mjs +33 -0
  39. package/dist/compat/array/pullAt.d.mts +32 -0
  40. package/dist/compat/array/pullAt.d.ts +32 -0
  41. package/dist/compat/array/pullAt.mjs +34 -0
  42. package/dist/compat/compat.d.mts +310 -0
  43. package/dist/compat/compat.d.ts +310 -0
  44. package/dist/compat/compat.mjs +312 -0
  45. package/dist/compat/function/partial.d.mts +575 -0
  46. package/dist/compat/function/partial.d.ts +575 -0
  47. package/dist/compat/function/partial.mjs +8 -0
  48. package/dist/compat/function/partialRight.d.mts +651 -0
  49. package/dist/compat/function/partialRight.d.ts +651 -0
  50. package/dist/compat/function/partialRight.mjs +8 -0
  51. package/dist/compat/index.d.mts +16 -7
  52. package/dist/compat/index.d.ts +16 -7
  53. package/dist/compat/index.js +290 -3771
  54. package/dist/compat/index.mjs +19 -8
  55. package/dist/compat/object/defaults.mjs +7 -1
  56. package/dist/compat/object/forIn.d.mts +58 -0
  57. package/dist/compat/object/forIn.d.ts +58 -0
  58. package/dist/compat/object/forIn.mjs +16 -0
  59. package/dist/compat/object/forInRight.d.mts +58 -0
  60. package/dist/compat/object/forInRight.d.ts +58 -0
  61. package/dist/compat/object/forInRight.mjs +21 -0
  62. package/dist/compat/object/forOwn.d.mts +54 -0
  63. package/dist/compat/object/forOwn.d.ts +54 -0
  64. package/dist/compat/object/forOwn.mjs +19 -0
  65. package/dist/compat/object/forOwnRight.d.mts +54 -0
  66. package/dist/compat/object/forOwnRight.d.ts +54 -0
  67. package/dist/compat/object/forOwnRight.mjs +19 -0
  68. package/dist/compat/object/hasIn.d.mts +40 -0
  69. package/dist/compat/object/hasIn.d.ts +40 -0
  70. package/dist/compat/object/hasIn.mjs +34 -0
  71. package/dist/compat/object/toPairs.mjs +7 -2
  72. package/dist/compat/object/toPairsIn.mjs +7 -2
  73. package/dist/compat/string/repeat.d.mts +1 -1
  74. package/dist/compat/string/repeat.d.ts +1 -1
  75. package/dist/compat/string/repeat.mjs +12 -2
  76. package/dist/compat/string/words.d.mts +1 -1
  77. package/dist/compat/string/words.d.ts +1 -1
  78. package/dist/compat/string/words.mjs +2 -1
  79. package/dist/compat/toolkit.d.mts +9 -0
  80. package/dist/compat/toolkit.d.ts +9 -0
  81. package/dist/compat/toolkit.mjs +10 -0
  82. package/dist/compat/util/overEvery.d.mts +67 -0
  83. package/dist/compat/util/overEvery.d.ts +67 -0
  84. package/dist/compat/util/overEvery.mjs +23 -0
  85. package/dist/compat/util/overSome.d.mts +69 -0
  86. package/dist/compat/util/overSome.d.ts +69 -0
  87. package/dist/compat/util/overSome.mjs +23 -0
  88. package/dist/function/index.js +3 -5
  89. package/dist/function/partial.d.mts +3 -3
  90. package/dist/function/partial.d.ts +3 -3
  91. package/dist/function/partial.mjs +17 -19
  92. package/dist/function/partialRight.d.mts +3 -3
  93. package/dist/function/partialRight.d.ts +3 -3
  94. package/dist/function/partialRight.mjs +18 -20
  95. package/dist/function/throttle.mjs +0 -1
  96. package/dist/index.js +39 -42
  97. package/dist/object/index.js +15 -15
  98. package/dist/object/toCamelCaseKeys.mjs +3 -0
  99. package/dist/object/toSnakeCaseKeys.mjs +3 -0
  100. package/dist/predicate/index.js +4 -5
  101. package/dist/string/index.js +7 -8
  102. package/package.json +1 -1
  103. package/dist/_chunk/isPlainObject-Xaozpc.js +0 -93
  104. package/dist/_chunk/partialRight-B0_CSB.js +0 -50
  105. package/dist/_chunk/snakeCase-BtVEeB.js +0 -75
  106. package/dist/_chunk/toSnakeCaseKeys-BNpS71.js +0 -569
@@ -0,0 +1,12 @@
1
+ function copyArray(source, array) {
2
+ const length = source.length;
3
+ if (array == null) {
4
+ array = Array(length);
5
+ }
6
+ for (let i = 0; i < length; i++) {
7
+ array[i] = source[i];
8
+ }
9
+ return array;
10
+ }
11
+
12
+ export { copyArray as default };
@@ -0,0 +1,11 @@
1
+ function mapToEntries(map) {
2
+ const arr = new Array(map.size);
3
+ const keys = map.keys();
4
+ const values = map.values();
5
+ for (let i = 0; i < arr.length; i++) {
6
+ arr[i] = [keys.next().value, values.next().value];
7
+ }
8
+ return arr;
9
+ }
10
+
11
+ export { mapToEntries };
@@ -0,0 +1,11 @@
1
+ function setToEntries(set) {
2
+ const arr = new Array(set.size);
3
+ const values = set.values();
4
+ for (let i = 0; i < arr.length; i++) {
5
+ const value = values.next().value;
6
+ arr[i] = [value, value];
7
+ }
8
+ return arr;
9
+ }
10
+
11
+ export { setToEntries };
@@ -2,10 +2,10 @@ function toKey(value) {
2
2
  if (typeof value === 'string' || typeof value === 'symbol') {
3
3
  return value;
4
4
  }
5
- if (Object.is(value.valueOf(), -0)) {
5
+ if (Object.is(value?.valueOf?.(), -0)) {
6
6
  return '-0';
7
7
  }
8
- return value.toString();
8
+ return String(value);
9
9
  }
10
10
 
11
11
  export { toKey };
@@ -8,10 +8,9 @@ function find(source, _doesMatch, fromIndex = 0) {
8
8
  fromIndex = Math.max(source.length + fromIndex, 0);
9
9
  }
10
10
  const doesMatch = iteratee(_doesMatch);
11
- const values = Array.isArray(source) ? source.slice(fromIndex) : Object.values(source).slice(fromIndex);
12
11
  if (typeof doesMatch === 'function' && !Array.isArray(source)) {
13
- const keys = Object.keys(source).slice(fromIndex);
14
- for (let i = 0; i < keys.length; i++) {
12
+ const keys = Object.keys(source);
13
+ for (let i = fromIndex; i < keys.length; i++) {
15
14
  const key = keys[i];
16
15
  const value = source[key];
17
16
  if (doesMatch(value, key, source)) {
@@ -20,6 +19,7 @@ function find(source, _doesMatch, fromIndex = 0) {
20
19
  }
21
20
  return undefined;
22
21
  }
22
+ const values = Array.isArray(source) ? source.slice(fromIndex) : Object.values(source).slice(fromIndex);
23
23
  return values.find(doesMatch);
24
24
  }
25
25
 
@@ -14,9 +14,8 @@ function findLast(source, _doesMatch, fromIndex) {
14
14
  fromIndex = Math.min(fromIndex, length - 1);
15
15
  }
16
16
  const doesMatch = iteratee(_doesMatch);
17
- const values = Array.isArray(source) ? source.slice(0, fromIndex + 1) : Object.values(source).slice(0, fromIndex + 1);
18
17
  if (typeof doesMatch === 'function' && !Array.isArray(source)) {
19
- const keys = Object.keys(source).slice(0, fromIndex + 1);
18
+ const keys = Object.keys(source);
20
19
  for (let i = fromIndex; i >= 0; i--) {
21
20
  const key = keys[i];
22
21
  const value = source[key];
@@ -26,6 +25,7 @@ function findLast(source, _doesMatch, fromIndex) {
26
25
  }
27
26
  return undefined;
28
27
  }
28
+ const values = Array.isArray(source) ? source.slice(0, fromIndex + 1) : Object.values(source).slice(0, fromIndex + 1);
29
29
  return values.findLast(doesMatch);
30
30
  }
31
31
 
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Maps each element in the collection using the iteratee function and flattens the result by one level.
3
+ * The iteratee is invoked with three arguments: (value, index, array).
4
+ *
5
+ * @category Array
6
+ * @param {Array|Object} collection The collection to iterate over.
7
+ * @param {Function} [iteratee=identity] The function invoked per iteration.
8
+ * @returns {Array} Returns the new flattened array.
9
+ * @example
10
+ *
11
+ * function duplicate(n) {
12
+ * return [n, n];
13
+ * }
14
+ *
15
+ * flatMap([1, 2], duplicate);
16
+ * // => [1, 1, 2, 2]
17
+ */
18
+ declare function flatMap<T, R>(collection: T[] | Record<string, T> | null | undefined, iteratee?: ((value: T, index: number | string, collection: unknown) => R | R[] | null | undefined) | keyof T | object): R[];
19
+
20
+ export { flatMap };
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Maps each element in the collection using the iteratee function and flattens the result by one level.
3
+ * The iteratee is invoked with three arguments: (value, index, array).
4
+ *
5
+ * @category Array
6
+ * @param {Array|Object} collection The collection to iterate over.
7
+ * @param {Function} [iteratee=identity] The function invoked per iteration.
8
+ * @returns {Array} Returns the new flattened array.
9
+ * @example
10
+ *
11
+ * function duplicate(n) {
12
+ * return [n, n];
13
+ * }
14
+ *
15
+ * flatMap([1, 2], duplicate);
16
+ * // => [1, 1, 2, 2]
17
+ */
18
+ declare function flatMap<T, R>(collection: T[] | Record<string, T> | null | undefined, iteratee?: ((value: T, index: number | string, collection: unknown) => R | R[] | null | undefined) | keyof T | object): R[];
19
+
20
+ export { flatMap };
@@ -0,0 +1,13 @@
1
+ import { flatten } from './flatten.mjs';
2
+ import { map } from './map.mjs';
3
+ import { isNil } from '../../predicate/isNil.mjs';
4
+
5
+ function flatMap(collection, iteratee) {
6
+ if (isNil(collection)) {
7
+ return [];
8
+ }
9
+ const mapped = isNil(iteratee) ? map(collection) : map(collection, iteratee);
10
+ return flatten(mapped, 1);
11
+ }
12
+
13
+ export { flatMap };
@@ -0,0 +1,106 @@
1
+ /**
2
+ * Iterates over elements of 'array' from right to left and invokes 'callback' for each element.
3
+ *
4
+ * @template K - The type of elements in the array.
5
+ * @template T - The type of the array.
6
+ * @param {T | null | undefined} array - The array to iterate over.
7
+ * @param {(value: K, index: number, array: T) => unknown} [callback] - The function invoked for each element.
8
+ * The callback function receives three arguments:
9
+ * - 'value': The current element being processed in the array.
10
+ * - 'index': The index of the current element being processed in the array.
11
+ * - 'array': The array 'forEachRight' was called upon.
12
+ * @returns {T} Returns the original array.
13
+ *
14
+ * @example
15
+ * forEachRight([1, 2, 3], (value, index, array) => console.log(value, index));
16
+ * // Output:
17
+ * // 3 2
18
+ * // 2 1
19
+ * // 1 0
20
+ *
21
+ */
22
+ declare function forEachRight<T>(array: T[], callback?: (value: T, index: number, array: T[]) => unknown): T[];
23
+ /**
24
+ * Iterates over elements of 'array' from right to left and invokes 'callback' for each element.
25
+ *
26
+ * @template K - The type of elements in the array.
27
+ * @template T - The type of the array.
28
+ * @param {T | null | undefined} array - The array to iterate over.
29
+ * @param {(value: K, index: number, array: T) => unknown} [callback] - The function invoked for each element.
30
+ * The callback function receives three arguments:
31
+ * - 'value': The current element being processed in the array.
32
+ * - 'index': The index of the current element being processed in the array.
33
+ * - 'array': The array 'forEachRight' was called upon.
34
+ * @returns {T} Returns the original array.
35
+ *
36
+ * @example
37
+ * forEachRight([1, 2, 3], (value, index, array) => console.log(value, index));
38
+ * // Output:
39
+ * // 3 2
40
+ * // 2 1
41
+ * // 1 0
42
+ *
43
+ */
44
+ declare function forEachRight<T>(array: readonly T[], callback?: (value: T, index: number, array: T[]) => unknown): readonly T[];
45
+ /**
46
+ * Iterates over elements of 'array' from right to left and invokes 'callback' for each element.
47
+ *
48
+ * @template T - The type of string.
49
+ * @param {T | null | undefined} string - The string to iterate over
50
+ * @param {(value: T, index: number, string: T) => unknown} [callback] - The function invoked for each char.
51
+ * The callback function receives three arguments:
52
+ * - 'char': The current char being processed in the string.
53
+ * - 'index': The index of the current char being processed in the string.
54
+ * - 'string': The string 'forEachRight' was called upon.
55
+ * @returns {T} Returns the original string.
56
+ *
57
+ * @example
58
+ * forEachRight('abc', (char, index, string) => console.log(char, index));
59
+ * // Output:
60
+ * // 'c' 2
61
+ * // 'b' 1
62
+ * // 'a' 0
63
+ */
64
+ declare function forEachRight<T extends string | null | undefined>(string: T, callback?: (char: string, index: number, string: string) => unknown): T;
65
+ /**
66
+ * Iterates over elements of 'array' from right to left and invokes 'callback' for each element.
67
+ *
68
+ * @template T - The type of elements in the array.
69
+ * @param { ArrayLike<T> } array - The array to iterate over.
70
+ * @param {(value: T, index: number, array: ArrayLike<T>) => unknown} [callback] - The function invoked for each element.
71
+ * The callback function receives three arguments:
72
+ * - 'value': The current element being processed in the array.
73
+ * - 'index': The index of the current element being processed in the array.
74
+ * - 'array': The array 'forEachRight' was called upon.
75
+ * @returns {T} Returns the original array.
76
+ *
77
+ * @example
78
+ * forEachRight([1, 2, 3], (value, index, array) => console.log(value, index));
79
+ * // Output:
80
+ * // 3 2
81
+ * // 2 1
82
+ * // 1 0
83
+ *
84
+ */
85
+ declare function forEachRight<T>(array: ArrayLike<T>, callback?: (value: T, index: number, array: ArrayLike<T>) => unknown): ArrayLike<T>;
86
+ /**
87
+ * Iterates over elements of 'array' from right to left and invokes 'callback' for each element.
88
+ *
89
+ * @template T - The type of object.
90
+ * @param {T} object - The object to iterate over.
91
+ * @param {(value: T[keyof T], key: keyof T, object: T) => unknown} [callback] - The function invoked for each property.
92
+ * The callback function receives three arguments:
93
+ * - 'value': The current property being processed in the object.
94
+ * - 'key': The key of the current property being processed in the object.
95
+ * - 'object': The object 'forEachRight' was called upon.
96
+ * @returns {T} Returns the original object.
97
+ *
98
+ * @example
99
+ * forEachRight({'a': 1, 'b': 2 }, (value, key, object) => console.log(value, key));
100
+ * // Output:
101
+ * // 2 'b'
102
+ * // 1 'a'
103
+ */
104
+ declare function forEachRight<T extends object | null | undefined>(object: T, callback?: (value: T[keyof T], key: keyof T, object: T) => unknown): T;
105
+
106
+ export { forEachRight };
@@ -0,0 +1,106 @@
1
+ /**
2
+ * Iterates over elements of 'array' from right to left and invokes 'callback' for each element.
3
+ *
4
+ * @template K - The type of elements in the array.
5
+ * @template T - The type of the array.
6
+ * @param {T | null | undefined} array - The array to iterate over.
7
+ * @param {(value: K, index: number, array: T) => unknown} [callback] - The function invoked for each element.
8
+ * The callback function receives three arguments:
9
+ * - 'value': The current element being processed in the array.
10
+ * - 'index': The index of the current element being processed in the array.
11
+ * - 'array': The array 'forEachRight' was called upon.
12
+ * @returns {T} Returns the original array.
13
+ *
14
+ * @example
15
+ * forEachRight([1, 2, 3], (value, index, array) => console.log(value, index));
16
+ * // Output:
17
+ * // 3 2
18
+ * // 2 1
19
+ * // 1 0
20
+ *
21
+ */
22
+ declare function forEachRight<T>(array: T[], callback?: (value: T, index: number, array: T[]) => unknown): T[];
23
+ /**
24
+ * Iterates over elements of 'array' from right to left and invokes 'callback' for each element.
25
+ *
26
+ * @template K - The type of elements in the array.
27
+ * @template T - The type of the array.
28
+ * @param {T | null | undefined} array - The array to iterate over.
29
+ * @param {(value: K, index: number, array: T) => unknown} [callback] - The function invoked for each element.
30
+ * The callback function receives three arguments:
31
+ * - 'value': The current element being processed in the array.
32
+ * - 'index': The index of the current element being processed in the array.
33
+ * - 'array': The array 'forEachRight' was called upon.
34
+ * @returns {T} Returns the original array.
35
+ *
36
+ * @example
37
+ * forEachRight([1, 2, 3], (value, index, array) => console.log(value, index));
38
+ * // Output:
39
+ * // 3 2
40
+ * // 2 1
41
+ * // 1 0
42
+ *
43
+ */
44
+ declare function forEachRight<T>(array: readonly T[], callback?: (value: T, index: number, array: T[]) => unknown): readonly T[];
45
+ /**
46
+ * Iterates over elements of 'array' from right to left and invokes 'callback' for each element.
47
+ *
48
+ * @template T - The type of string.
49
+ * @param {T | null | undefined} string - The string to iterate over
50
+ * @param {(value: T, index: number, string: T) => unknown} [callback] - The function invoked for each char.
51
+ * The callback function receives three arguments:
52
+ * - 'char': The current char being processed in the string.
53
+ * - 'index': The index of the current char being processed in the string.
54
+ * - 'string': The string 'forEachRight' was called upon.
55
+ * @returns {T} Returns the original string.
56
+ *
57
+ * @example
58
+ * forEachRight('abc', (char, index, string) => console.log(char, index));
59
+ * // Output:
60
+ * // 'c' 2
61
+ * // 'b' 1
62
+ * // 'a' 0
63
+ */
64
+ declare function forEachRight<T extends string | null | undefined>(string: T, callback?: (char: string, index: number, string: string) => unknown): T;
65
+ /**
66
+ * Iterates over elements of 'array' from right to left and invokes 'callback' for each element.
67
+ *
68
+ * @template T - The type of elements in the array.
69
+ * @param { ArrayLike<T> } array - The array to iterate over.
70
+ * @param {(value: T, index: number, array: ArrayLike<T>) => unknown} [callback] - The function invoked for each element.
71
+ * The callback function receives three arguments:
72
+ * - 'value': The current element being processed in the array.
73
+ * - 'index': The index of the current element being processed in the array.
74
+ * - 'array': The array 'forEachRight' was called upon.
75
+ * @returns {T} Returns the original array.
76
+ *
77
+ * @example
78
+ * forEachRight([1, 2, 3], (value, index, array) => console.log(value, index));
79
+ * // Output:
80
+ * // 3 2
81
+ * // 2 1
82
+ * // 1 0
83
+ *
84
+ */
85
+ declare function forEachRight<T>(array: ArrayLike<T>, callback?: (value: T, index: number, array: ArrayLike<T>) => unknown): ArrayLike<T>;
86
+ /**
87
+ * Iterates over elements of 'array' from right to left and invokes 'callback' for each element.
88
+ *
89
+ * @template T - The type of object.
90
+ * @param {T} object - The object to iterate over.
91
+ * @param {(value: T[keyof T], key: keyof T, object: T) => unknown} [callback] - The function invoked for each property.
92
+ * The callback function receives three arguments:
93
+ * - 'value': The current property being processed in the object.
94
+ * - 'key': The key of the current property being processed in the object.
95
+ * - 'object': The object 'forEachRight' was called upon.
96
+ * @returns {T} Returns the original object.
97
+ *
98
+ * @example
99
+ * forEachRight({'a': 1, 'b': 2 }, (value, key, object) => console.log(value, key));
100
+ * // Output:
101
+ * // 2 'b'
102
+ * // 1 'a'
103
+ */
104
+ declare function forEachRight<T extends object | null | undefined>(object: T, callback?: (value: T[keyof T], key: keyof T, object: T) => unknown): T;
105
+
106
+ export { forEachRight };
@@ -0,0 +1,21 @@
1
+ import { identity } from '../../function/identity.mjs';
2
+ import { range } from '../../math/range.mjs';
3
+ import { isArrayLike } from '../predicate/isArrayLike.mjs';
4
+
5
+ function forEachRight(collection, callback = identity) {
6
+ if (!collection) {
7
+ return collection;
8
+ }
9
+ const keys = isArrayLike(collection) ? range(0, collection.length) : Object.keys(collection);
10
+ for (let i = keys.length - 1; i >= 0; i--) {
11
+ const key = keys[i];
12
+ const value = collection[key];
13
+ const result = callback(value, key, collection);
14
+ if (result === false) {
15
+ break;
16
+ }
17
+ }
18
+ return collection;
19
+ }
20
+
21
+ export { forEachRight };
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Maps each element of an array based on a provided key-generating function.
3
+ *
4
+ * This function takes an array and a function that generates a key from each element. It returns
5
+ * an object where the keys are the generated keys and the values are the corresponding elements.
6
+ * If there are multiple elements generating the same key, the last element among them is used
7
+ * as the value.
8
+ *
9
+ * @template T - The type of elements in the array.
10
+ * @template K - The type of keys.
11
+ * @param {T[]} arr - The array of elements to be mapped.
12
+ * @param {(item: T) => K} getKeyFromItem - A function that generates a key from an element.
13
+ * @returns {Record<K, T>} An object where keys are mapped to each element of an array.
14
+ *
15
+ * @example
16
+ * const array = [
17
+ * { category: 'fruit', name: 'apple' },
18
+ * { category: 'fruit', name: 'banana' },
19
+ * { category: 'vegetable', name: 'carrot' }
20
+ * ];
21
+ * const result = keyBy(array, item => item.category);
22
+ * // result will be:
23
+ * // {
24
+ * // fruit: { category: 'fruit', name: 'banana' },
25
+ * // vegetable: { category: 'vegetable', name: 'carrot' }
26
+ * // }
27
+ */
28
+ declare function groupBy<T, K extends PropertyKey>(source: ArrayLike<T> | null | undefined, getKeyFromItem?: ((item: T, index: number, arr: any) => unknown) | Partial<T> | [keyof T, unknown] | PropertyKey | null): Record<K, T[]>;
29
+ /**
30
+ * Groups the elements of an object based on a provided key-generating function.
31
+ *
32
+ * This function takes an object and a function that generates a key from each value. It returns
33
+ * an object where the keys are the generated keys and the values are arrays of elements that share
34
+ * the same key.
35
+ *
36
+ * @template T - The type of values in the object.
37
+ * @template K - The type of keys.
38
+ * @param {Record<any, T> | null | undefined} source - The object to group.
39
+ * @param {Function | PropertyKey | Array | Object} [getKeyFromItem] - The iteratee to transform keys.
40
+ * - If a function is provided, it's invoked for each element in the collection.
41
+ * - If a property name (string) is provided, that property of each element is used as the key.
42
+ * - If a property-value pair (array) is provided, elements with matching property values are used.
43
+ * - If a partial object is provided, elements with matching properties are used.
44
+ * @returns {Record<K, T>} An object where each key is associated with an array of elements that
45
+ * share that key.
46
+ *
47
+ * @example
48
+ * // Using an object
49
+ * const obj = { a: { category: 'fruit' }, b: { category: 'vegetable' }, c: { category: 'fruit' } };
50
+ * const result = groupBy(obj, 'category');
51
+ * // => { fruit: [{ category: 'fruit' }, { category: 'fruit' }], vegetable: [{ category: 'vegetable' }] }
52
+ */
53
+ declare function groupBy<T, K extends PropertyKey>(source: Record<any, T> | null | undefined, getKeyFromItem?: ((item: T, index: number, arr: any) => unknown) | Partial<T> | [keyof T, unknown] | PropertyKey | null): Record<K, T[]>;
54
+
55
+ export { groupBy };
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Maps each element of an array based on a provided key-generating function.
3
+ *
4
+ * This function takes an array and a function that generates a key from each element. It returns
5
+ * an object where the keys are the generated keys and the values are the corresponding elements.
6
+ * If there are multiple elements generating the same key, the last element among them is used
7
+ * as the value.
8
+ *
9
+ * @template T - The type of elements in the array.
10
+ * @template K - The type of keys.
11
+ * @param {T[]} arr - The array of elements to be mapped.
12
+ * @param {(item: T) => K} getKeyFromItem - A function that generates a key from an element.
13
+ * @returns {Record<K, T>} An object where keys are mapped to each element of an array.
14
+ *
15
+ * @example
16
+ * const array = [
17
+ * { category: 'fruit', name: 'apple' },
18
+ * { category: 'fruit', name: 'banana' },
19
+ * { category: 'vegetable', name: 'carrot' }
20
+ * ];
21
+ * const result = keyBy(array, item => item.category);
22
+ * // result will be:
23
+ * // {
24
+ * // fruit: { category: 'fruit', name: 'banana' },
25
+ * // vegetable: { category: 'vegetable', name: 'carrot' }
26
+ * // }
27
+ */
28
+ declare function groupBy<T, K extends PropertyKey>(source: ArrayLike<T> | null | undefined, getKeyFromItem?: ((item: T, index: number, arr: any) => unknown) | Partial<T> | [keyof T, unknown] | PropertyKey | null): Record<K, T[]>;
29
+ /**
30
+ * Groups the elements of an object based on a provided key-generating function.
31
+ *
32
+ * This function takes an object and a function that generates a key from each value. It returns
33
+ * an object where the keys are the generated keys and the values are arrays of elements that share
34
+ * the same key.
35
+ *
36
+ * @template T - The type of values in the object.
37
+ * @template K - The type of keys.
38
+ * @param {Record<any, T> | null | undefined} source - The object to group.
39
+ * @param {Function | PropertyKey | Array | Object} [getKeyFromItem] - The iteratee to transform keys.
40
+ * - If a function is provided, it's invoked for each element in the collection.
41
+ * - If a property name (string) is provided, that property of each element is used as the key.
42
+ * - If a property-value pair (array) is provided, elements with matching property values are used.
43
+ * - If a partial object is provided, elements with matching properties are used.
44
+ * @returns {Record<K, T>} An object where each key is associated with an array of elements that
45
+ * share that key.
46
+ *
47
+ * @example
48
+ * // Using an object
49
+ * const obj = { a: { category: 'fruit' }, b: { category: 'vegetable' }, c: { category: 'fruit' } };
50
+ * const result = groupBy(obj, 'category');
51
+ * // => { fruit: [{ category: 'fruit' }, { category: 'fruit' }], vegetable: [{ category: 'vegetable' }] }
52
+ */
53
+ declare function groupBy<T, K extends PropertyKey>(source: Record<any, T> | null | undefined, getKeyFromItem?: ((item: T, index: number, arr: any) => unknown) | Partial<T> | [keyof T, unknown] | PropertyKey | null): Record<K, T[]>;
54
+
55
+ export { groupBy };
@@ -0,0 +1,15 @@
1
+ import { groupBy as groupBy$1 } from '../../array/groupBy.mjs';
2
+ import { identity } from '../../function/identity.mjs';
3
+ import { isArrayLike } from '../predicate/isArrayLike.mjs';
4
+ import { iteratee } from '../util/iteratee.mjs';
5
+
6
+ function groupBy(source, _getKeyFromItem) {
7
+ if (source == null) {
8
+ return {};
9
+ }
10
+ const items = isArrayLike(source) ? Array.from(source) : Object.values(source);
11
+ const getKeyFromItem = iteratee(_getKeyFromItem ?? identity);
12
+ return groupBy$1(items, getKeyFromItem);
13
+ }
14
+
15
+ export { groupBy };
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Creates an array of elements split into two groups, the first of which contains elements
3
+ * `predicate` returns truthy for, the second of which contains elements
4
+ * `predicate` returns falsy for. The predicate is invoked with one argument: (value).
5
+ *
6
+ * @template T
7
+ * @template U
8
+ * @param {ArrayLike<T> | T | null | undefined} source - The array or object to iterate over.
9
+ * @param {(value: T) => value is U} predicate - The function invoked per iteration.
10
+ * @returns {[U[], Array<Exclude<T, U>>]} - Returns the array of grouped elements.
11
+ *
12
+ * @example
13
+ * partition([1, 2, 3, 4], n => n % 2 === 0);
14
+ * // => [[2, 4], [1, 3]]
15
+ *
16
+ * partition([1, 2, 3, 4], 'a');
17
+ * // => [[2, 4], [1, 3]]
18
+ *
19
+ * partition([1, 2, 3, 4], ['a', 2]);
20
+ */
21
+ declare function partition<T, U extends T>(source: ArrayLike<T> | null | undefined, predicate: (value: T) => value is U): [U[], Array<Exclude<T, U>>];
22
+ /**
23
+ * Creates an array of elements split into two groups, the first of which contains elements
24
+ * `predicate` returns truthy for, the second of which contains elements
25
+ * `predicate` returns falsy for. The predicate is invoked with one argument: (value).
26
+ *
27
+ * @template T
28
+ * @param {ArrayLike<T> | T | null | undefined} source - The array or object to iterate over.
29
+ * @param {((item: T, index: number, arr: any) => unknown) | Partial<T> | [keyof T, unknown] | PropertyKey} [predicate=identity] - The function invoked per iteration.
30
+ * @returns {[T[], T[]]} - Returns the array of grouped elements.
31
+ *
32
+ * @example
33
+ * partition([1, 2, 3, 4], n => n % 2 === 0);
34
+ * // => [[2, 4], [1, 3]]
35
+ *
36
+ * partition([1, 2, 3, 4], 'a');
37
+ * // => [[2, 4], [1, 3]]
38
+ *
39
+ * partition([1, 2, 3, 4], ['a', 2]);
40
+ * // => [[2], [1, 3, 4]]
41
+ */
42
+ declare function partition<T>(source: ArrayLike<T> | null | undefined, predicate?: ((value: T) => unknown) | PropertyKey | [PropertyKey, any] | Partial<T>): [T[], T[]];
43
+ /**
44
+ * Creates an array of elements split into two groups, the first of which contains elements
45
+ * `predicate` returns truthy for, the second of which contains elements
46
+ * `predicate` returns falsy for. The predicate is invoked with one argument: (value).
47
+ *
48
+ * @template T
49
+ * @param {T | null | undefined} object - The object to iterate over.
50
+ * @param {((item: T[keyof T], key: keyof T) => unknown) | Partial<T[keyof T]> | [keyof T, unknown] | PropertyKey} [predicate=identity] - The function invoked per iteration.
51
+ * @returns {[T[keyof T][], T[keyof T][]]} - Returns the array of grouped elements.
52
+ *
53
+ * @example
54
+ * partition({ a: 1, b: 2, c: 3 }, n => n % 2 === 0);
55
+ * // => [[2], [1, 3]]
56
+ *
57
+ * partition({ a: 1, b: 2, c: 3 }, 'a');
58
+ * // => [[1], [2, 3]]
59
+ *
60
+ * partition({ a: 1, b: 2, c: 3 }, ['a', 1]);
61
+ * // => [[1], [2, 3]]
62
+ */
63
+ declare function partition<T extends object>(object: T | null | undefined, predicate?: ((value: T[keyof T]) => unknown) | PropertyKey | [PropertyKey, any] | Partial<T[keyof T]>): [Array<T[keyof T]>, Array<T[keyof T]>];
64
+
65
+ export { partition };
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Creates an array of elements split into two groups, the first of which contains elements
3
+ * `predicate` returns truthy for, the second of which contains elements
4
+ * `predicate` returns falsy for. The predicate is invoked with one argument: (value).
5
+ *
6
+ * @template T
7
+ * @template U
8
+ * @param {ArrayLike<T> | T | null | undefined} source - The array or object to iterate over.
9
+ * @param {(value: T) => value is U} predicate - The function invoked per iteration.
10
+ * @returns {[U[], Array<Exclude<T, U>>]} - Returns the array of grouped elements.
11
+ *
12
+ * @example
13
+ * partition([1, 2, 3, 4], n => n % 2 === 0);
14
+ * // => [[2, 4], [1, 3]]
15
+ *
16
+ * partition([1, 2, 3, 4], 'a');
17
+ * // => [[2, 4], [1, 3]]
18
+ *
19
+ * partition([1, 2, 3, 4], ['a', 2]);
20
+ */
21
+ declare function partition<T, U extends T>(source: ArrayLike<T> | null | undefined, predicate: (value: T) => value is U): [U[], Array<Exclude<T, U>>];
22
+ /**
23
+ * Creates an array of elements split into two groups, the first of which contains elements
24
+ * `predicate` returns truthy for, the second of which contains elements
25
+ * `predicate` returns falsy for. The predicate is invoked with one argument: (value).
26
+ *
27
+ * @template T
28
+ * @param {ArrayLike<T> | T | null | undefined} source - The array or object to iterate over.
29
+ * @param {((item: T, index: number, arr: any) => unknown) | Partial<T> | [keyof T, unknown] | PropertyKey} [predicate=identity] - The function invoked per iteration.
30
+ * @returns {[T[], T[]]} - Returns the array of grouped elements.
31
+ *
32
+ * @example
33
+ * partition([1, 2, 3, 4], n => n % 2 === 0);
34
+ * // => [[2, 4], [1, 3]]
35
+ *
36
+ * partition([1, 2, 3, 4], 'a');
37
+ * // => [[2, 4], [1, 3]]
38
+ *
39
+ * partition([1, 2, 3, 4], ['a', 2]);
40
+ * // => [[2], [1, 3, 4]]
41
+ */
42
+ declare function partition<T>(source: ArrayLike<T> | null | undefined, predicate?: ((value: T) => unknown) | PropertyKey | [PropertyKey, any] | Partial<T>): [T[], T[]];
43
+ /**
44
+ * Creates an array of elements split into two groups, the first of which contains elements
45
+ * `predicate` returns truthy for, the second of which contains elements
46
+ * `predicate` returns falsy for. The predicate is invoked with one argument: (value).
47
+ *
48
+ * @template T
49
+ * @param {T | null | undefined} object - The object to iterate over.
50
+ * @param {((item: T[keyof T], key: keyof T) => unknown) | Partial<T[keyof T]> | [keyof T, unknown] | PropertyKey} [predicate=identity] - The function invoked per iteration.
51
+ * @returns {[T[keyof T][], T[keyof T][]]} - Returns the array of grouped elements.
52
+ *
53
+ * @example
54
+ * partition({ a: 1, b: 2, c: 3 }, n => n % 2 === 0);
55
+ * // => [[2], [1, 3]]
56
+ *
57
+ * partition({ a: 1, b: 2, c: 3 }, 'a');
58
+ * // => [[1], [2, 3]]
59
+ *
60
+ * partition({ a: 1, b: 2, c: 3 }, ['a', 1]);
61
+ * // => [[1], [2, 3]]
62
+ */
63
+ declare function partition<T extends object>(object: T | null | undefined, predicate?: ((value: T[keyof T]) => unknown) | PropertyKey | [PropertyKey, any] | Partial<T[keyof T]>): [Array<T[keyof T]>, Array<T[keyof T]>];
64
+
65
+ export { partition };