es-toolkit 1.5.0 → 1.6.0-dev.113
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/CHANGELOG.md +11 -0
- package/dist/array/compact.d.mts +14 -0
- package/dist/array/compact.d.ts +14 -0
- package/dist/array/compact.js +39 -0
- package/dist/array/compact.js.map +1 -0
- package/dist/array/compact.mjs +8 -0
- package/dist/array/index.d.mts +3 -0
- package/dist/array/index.d.ts +3 -0
- package/dist/array/index.js +44 -8
- package/dist/array/index.js.map +1 -1
- package/dist/array/index.mjs +52 -40
- package/dist/array/keyBy.d.mts +28 -0
- package/dist/array/keyBy.d.ts +28 -0
- package/dist/array/keyBy.js +38 -0
- package/dist/array/keyBy.js.map +1 -0
- package/dist/array/keyBy.mjs +8 -0
- package/dist/array/maxBy.d.mts +8 -15
- package/dist/array/maxBy.d.ts +8 -15
- package/dist/array/maxBy.js +4 -4
- package/dist/array/maxBy.js.map +1 -1
- package/dist/array/maxBy.mjs +1 -1
- package/dist/array/minBy.d.mts +9 -8
- package/dist/array/minBy.d.ts +9 -8
- package/dist/array/minBy.js +4 -4
- package/dist/array/minBy.js.map +1 -1
- package/dist/array/minBy.mjs +1 -1
- package/dist/array/zipObject.d.mts +33 -0
- package/dist/array/zipObject.d.ts +33 -0
- package/dist/array/zipObject.js +37 -0
- package/dist/array/zipObject.js.map +1 -0
- package/dist/array/zipObject.mjs +8 -0
- package/dist/array/zipObject.mjs.map +1 -0
- package/dist/chunk-DA7GSKQ5.mjs +13 -0
- package/dist/chunk-DA7GSKQ5.mjs.map +1 -0
- package/dist/chunk-FXOABQRO.mjs +18 -0
- package/dist/chunk-FXOABQRO.mjs.map +1 -0
- package/dist/chunk-NKTBEEIT.mjs +15 -0
- package/dist/chunk-NKTBEEIT.mjs.map +1 -0
- package/dist/{chunk-7OU22ENP.mjs → chunk-NOFHNJ5C.mjs} +1 -1
- package/dist/{chunk-7OU22ENP.mjs.map → chunk-NOFHNJ5C.mjs.map} +1 -1
- package/dist/chunk-OIHARMNM.mjs +18 -0
- package/dist/chunk-OIHARMNM.mjs.map +1 -0
- package/dist/chunk-QVHWSUEH.mjs +1 -0
- package/dist/chunk-QVHWSUEH.mjs.map +1 -0
- package/dist/chunk-S3GNYAM2.mjs +13 -0
- package/dist/chunk-S3GNYAM2.mjs.map +1 -0
- package/dist/{chunk-LCP6FOCX.mjs → chunk-SYWMYQYJ.mjs} +5 -1
- package/dist/chunk-SYWMYQYJ.mjs.map +1 -0
- package/dist/chunk-V6MDTRGS.mjs +1 -0
- package/dist/chunk-V6MDTRGS.mjs.map +1 -0
- package/dist/{chunk-5UT77WON.mjs → chunk-WC5HFQXP.mjs} +2 -2
- package/dist/chunk-WC5HFQXP.mjs.map +1 -0
- package/dist/chunk-ZOTAORG3.mjs +14 -0
- package/dist/chunk-ZOTAORG3.mjs.map +1 -0
- package/dist/function/index.mjs +3 -3
- package/dist/index.d.mts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +64 -17
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +85 -69
- package/dist/math/index.d.mts +1 -0
- package/dist/math/index.d.ts +1 -0
- package/dist/math/index.js +20 -9
- package/dist/math/index.js.map +1 -1
- package/dist/math/index.mjs +13 -9
- package/dist/math/mean.d.mts +16 -0
- package/dist/math/mean.d.ts +16 -0
- package/dist/math/mean.js +44 -0
- package/dist/math/mean.js.map +1 -0
- package/dist/math/mean.mjs +9 -0
- package/dist/math/mean.mjs.map +1 -0
- package/dist/math/random.d.mts +3 -0
- package/dist/math/random.d.ts +3 -0
- package/dist/math/random.js +4 -0
- package/dist/math/random.js.map +1 -1
- package/dist/math/random.mjs +1 -1
- package/dist/math/randomInt.d.mts +3 -0
- package/dist/math/randomInt.d.ts +3 -0
- package/dist/math/randomInt.js +4 -0
- package/dist/math/randomInt.js.map +1 -1
- package/dist/math/randomInt.mjs +2 -2
- package/dist/math/sum.d.mts +1 -1
- package/dist/math/sum.d.ts +1 -1
- package/dist/math/sum.js.map +1 -1
- package/dist/math/sum.mjs +1 -1
- package/dist/object/index.mjs +3 -3
- package/dist/predicate/index.mjs +6 -6
- package/package.json +1 -1
- package/dist/chunk-5UT77WON.mjs.map +0 -1
- package/dist/chunk-7LAMAYGN.mjs +0 -1
- package/dist/chunk-H23GUJVF.mjs +0 -18
- package/dist/chunk-H23GUJVF.mjs.map +0 -1
- package/dist/chunk-KZ5JY7J3.mjs +0 -18
- package/dist/chunk-KZ5JY7J3.mjs.map +0 -1
- package/dist/chunk-LCP6FOCX.mjs.map +0 -1
- package/dist/chunk-NP6PDX3E.mjs +0 -1
- /package/dist/{chunk-7LAMAYGN.mjs.map → array/compact.mjs.map} +0 -0
- /package/dist/{chunk-NP6PDX3E.mjs.map → array/keyBy.mjs.map} +0 -0
|
@@ -0,0 +1,28 @@
|
|
|
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
|
+
* @param {T[]} arr - The array of elements to be mapped.
|
|
10
|
+
* @param {(item: T) => K} getKeyFromItem - A function that generates a key from an element.
|
|
11
|
+
* @returns {Record<K, T>} An object where keys are mapped to each element of an array.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const array = [
|
|
15
|
+
* { category: 'fruit', name: 'apple' },
|
|
16
|
+
* { category: 'fruit', name: 'banana' },
|
|
17
|
+
* { category: 'vegetable', name: 'carrot' }
|
|
18
|
+
* ];
|
|
19
|
+
* const result = keyBy(array, item => item.category);
|
|
20
|
+
* // result will be:
|
|
21
|
+
* // {
|
|
22
|
+
* // fruit: { category: 'fruit', name: 'banana' },
|
|
23
|
+
* // vegetable: { category: 'vegetable', name: 'carrot' }
|
|
24
|
+
* // }
|
|
25
|
+
*/
|
|
26
|
+
declare function keyBy<T, K extends string>(arr: readonly T[], getKeyFromItem: (item: T) => K): Record<K, T>;
|
|
27
|
+
|
|
28
|
+
export { keyBy };
|
|
@@ -0,0 +1,28 @@
|
|
|
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
|
+
* @param {T[]} arr - The array of elements to be mapped.
|
|
10
|
+
* @param {(item: T) => K} getKeyFromItem - A function that generates a key from an element.
|
|
11
|
+
* @returns {Record<K, T>} An object where keys are mapped to each element of an array.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const array = [
|
|
15
|
+
* { category: 'fruit', name: 'apple' },
|
|
16
|
+
* { category: 'fruit', name: 'banana' },
|
|
17
|
+
* { category: 'vegetable', name: 'carrot' }
|
|
18
|
+
* ];
|
|
19
|
+
* const result = keyBy(array, item => item.category);
|
|
20
|
+
* // result will be:
|
|
21
|
+
* // {
|
|
22
|
+
* // fruit: { category: 'fruit', name: 'banana' },
|
|
23
|
+
* // vegetable: { category: 'vegetable', name: 'carrot' }
|
|
24
|
+
* // }
|
|
25
|
+
*/
|
|
26
|
+
declare function keyBy<T, K extends string>(arr: readonly T[], getKeyFromItem: (item: T) => K): Record<K, T>;
|
|
27
|
+
|
|
28
|
+
export { keyBy };
|
|
@@ -0,0 +1,38 @@
|
|
|
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/keyBy.ts
|
|
21
|
+
var keyBy_exports = {};
|
|
22
|
+
__export(keyBy_exports, {
|
|
23
|
+
keyBy: () => keyBy
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(keyBy_exports);
|
|
26
|
+
function keyBy(arr, getKeyFromItem) {
|
|
27
|
+
const result = {};
|
|
28
|
+
for (const item of arr) {
|
|
29
|
+
const key = getKeyFromItem(item);
|
|
30
|
+
result[key] = item;
|
|
31
|
+
}
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
35
|
+
0 && (module.exports = {
|
|
36
|
+
keyBy
|
|
37
|
+
});
|
|
38
|
+
//# sourceMappingURL=keyBy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/array/keyBy.ts"],"sourcesContent":["/**\n * Maps each element of an array based on a provided key-generating function.\n *\n * This function takes an array and a function that generates a key from each element. It returns\n * an object where the keys are the generated keys and the values are the corresponding elements.\n * If there are multiple elements generating the same key, the last element among them is used\n * as the value.\n *\n * @param {T[]} arr - The array of elements to be mapped.\n * @param {(item: T) => K} getKeyFromItem - A function that generates a key from an element.\n * @returns {Record<K, T>} An object where keys are mapped to each element of an array.\n *\n * @example\n * const array = [\n * { category: 'fruit', name: 'apple' },\n * { category: 'fruit', name: 'banana' },\n * { category: 'vegetable', name: 'carrot' }\n * ];\n * const result = keyBy(array, item => item.category);\n * // result will be:\n * // {\n * // fruit: { category: 'fruit', name: 'banana' },\n * // vegetable: { category: 'vegetable', name: 'carrot' }\n * // }\n */\nexport function keyBy<T, K extends string>(arr: readonly T[], getKeyFromItem: (item: T) => K): Record<K, T> {\n const result = {} as Record<K, T>;\n\n for (const item of arr) {\n const key = getKeyFromItem(item);\n result[key] = item;\n }\n\n return result;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAyBO,SAAS,MAA2B,KAAmB,gBAA8C;AAC1G,QAAM,SAAS,CAAC;AAEhB,aAAW,QAAQ,KAAK;AACtB,UAAM,MAAM,eAAe,IAAI;AAC/B,WAAO,GAAG,IAAI;AAAA,EAChB;AAEA,SAAO;AACT;","names":[]}
|
package/dist/array/maxBy.d.mts
CHANGED
|
@@ -1,21 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* This function takes an array of elements and a selector function, and returns the element with the maximum value according to the selector.
|
|
5
|
-
*
|
|
6
|
-
* @param {T[]} elements - An array of elements to be compared.
|
|
7
|
-
* @param {(element: T) => number} selector - A function that takes an element and returns a number.
|
|
8
|
-
* @returns {T} The element with the maximum value according to the selector.
|
|
2
|
+
* Finds the element in an array that has the maximum value when applying
|
|
3
|
+
* the `getValue` function to each element.
|
|
9
4
|
*
|
|
5
|
+
* @param {T[]} items The array of elements to search.
|
|
6
|
+
* @param {(element: T) => number} getValue A function that selects a numeric value from each element.
|
|
7
|
+
* @returns {T} The element with the maximum value as determined by the `getValue` function.
|
|
10
8
|
* @example
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* { name: 'Nunu', age: 30 },
|
|
14
|
-
* { name: 'Overmars', age: 20 }
|
|
15
|
-
* ];
|
|
16
|
-
* const result = maxBy(people, person => person.age);
|
|
17
|
-
* // result will be { name: 'Nunu', age: 30 }
|
|
9
|
+
* maxBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a); // Returns: { a: 3 }
|
|
10
|
+
* maxBy([], x => x.a); // Returns: undefined
|
|
18
11
|
*/
|
|
19
|
-
declare function maxBy<T>(
|
|
12
|
+
declare function maxBy<T>(items: T[], getValue: (element: T) => number): T;
|
|
20
13
|
|
|
21
14
|
export { maxBy };
|
package/dist/array/maxBy.d.ts
CHANGED
|
@@ -1,21 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* This function takes an array of elements and a selector function, and returns the element with the maximum value according to the selector.
|
|
5
|
-
*
|
|
6
|
-
* @param {T[]} elements - An array of elements to be compared.
|
|
7
|
-
* @param {(element: T) => number} selector - A function that takes an element and returns a number.
|
|
8
|
-
* @returns {T} The element with the maximum value according to the selector.
|
|
2
|
+
* Finds the element in an array that has the maximum value when applying
|
|
3
|
+
* the `getValue` function to each element.
|
|
9
4
|
*
|
|
5
|
+
* @param {T[]} items The array of elements to search.
|
|
6
|
+
* @param {(element: T) => number} getValue A function that selects a numeric value from each element.
|
|
7
|
+
* @returns {T} The element with the maximum value as determined by the `getValue` function.
|
|
10
8
|
* @example
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* { name: 'Nunu', age: 30 },
|
|
14
|
-
* { name: 'Overmars', age: 20 }
|
|
15
|
-
* ];
|
|
16
|
-
* const result = maxBy(people, person => person.age);
|
|
17
|
-
* // result will be { name: 'Nunu', age: 30 }
|
|
9
|
+
* maxBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a); // Returns: { a: 3 }
|
|
10
|
+
* maxBy([], x => x.a); // Returns: undefined
|
|
18
11
|
*/
|
|
19
|
-
declare function maxBy<T>(
|
|
12
|
+
declare function maxBy<T>(items: T[], getValue: (element: T) => number): T;
|
|
20
13
|
|
|
21
14
|
export { maxBy };
|
package/dist/array/maxBy.js
CHANGED
|
@@ -23,11 +23,11 @@ __export(maxBy_exports, {
|
|
|
23
23
|
maxBy: () => maxBy
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(maxBy_exports);
|
|
26
|
-
function maxBy(
|
|
27
|
-
let maxElement =
|
|
26
|
+
function maxBy(items, getValue) {
|
|
27
|
+
let maxElement = items[0];
|
|
28
28
|
let max = -Infinity;
|
|
29
|
-
for (const element of
|
|
30
|
-
const value =
|
|
29
|
+
for (const element of items) {
|
|
30
|
+
const value = getValue(element);
|
|
31
31
|
if (value > max) {
|
|
32
32
|
max = value;
|
|
33
33
|
maxElement = element;
|
package/dist/array/maxBy.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/array/maxBy.ts"],"sourcesContent":["/**\n *
|
|
1
|
+
{"version":3,"sources":["../../src/array/maxBy.ts"],"sourcesContent":["/**\n * Finds the element in an array that has the maximum value when applying\n * the `getValue` function to each element.\n *\n * @param {T[]} items The array of elements to search.\n * @param {(element: T) => number} getValue A function that selects a numeric value from each element.\n * @returns {T} The element with the maximum value as determined by the `getValue` function.\n * @example\n * maxBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a); // Returns: { a: 3 }\n * maxBy([], x => x.a); // Returns: undefined\n */\nexport function maxBy<T>(items: T[], getValue: (element: T) => number): T {\n let maxElement = items[0];\n let max = -Infinity;\n\n for (const element of items) {\n const value = getValue(element);\n if (value > max) {\n max = value;\n maxElement = element;\n }\n }\n\n return maxElement;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAWO,SAAS,MAAS,OAAY,UAAqC;AACxE,MAAI,aAAa,MAAM,CAAC;AACxB,MAAI,MAAM;AAEV,aAAW,WAAW,OAAO;AAC3B,UAAM,QAAQ,SAAS,OAAO;AAC9B,QAAI,QAAQ,KAAK;AACf,YAAM;AACN,mBAAa;AAAA,IACf;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
|
package/dist/array/maxBy.mjs
CHANGED
package/dist/array/minBy.d.mts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Finds the element in an array that has the minimum value when applying
|
|
3
|
+
* the `getValue` function to each element.
|
|
3
4
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
5
|
+
* @param {T[]} items The array of elements to search.
|
|
6
|
+
* @param {(element: T) => number} getValue A function that selects a numeric value from each element.
|
|
7
|
+
* @returns {T} The element with the minimum value as determined by the `getValue` function.
|
|
8
|
+
* @example
|
|
9
|
+
* minBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a); // Returns: { a: 1 }
|
|
10
|
+
* minBy([], x => x.a); // Returns: undefined
|
|
10
11
|
*/
|
|
11
|
-
declare function minBy<T>(
|
|
12
|
+
declare function minBy<T>(items: T[], getValue: (element: T) => number): T;
|
|
12
13
|
|
|
13
14
|
export { minBy };
|
package/dist/array/minBy.d.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Finds the element in an array that has the minimum value when applying
|
|
3
|
+
* the `getValue` function to each element.
|
|
3
4
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
5
|
+
* @param {T[]} items The array of elements to search.
|
|
6
|
+
* @param {(element: T) => number} getValue A function that selects a numeric value from each element.
|
|
7
|
+
* @returns {T} The element with the minimum value as determined by the `getValue` function.
|
|
8
|
+
* @example
|
|
9
|
+
* minBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a); // Returns: { a: 1 }
|
|
10
|
+
* minBy([], x => x.a); // Returns: undefined
|
|
10
11
|
*/
|
|
11
|
-
declare function minBy<T>(
|
|
12
|
+
declare function minBy<T>(items: T[], getValue: (element: T) => number): T;
|
|
12
13
|
|
|
13
14
|
export { minBy };
|
package/dist/array/minBy.js
CHANGED
|
@@ -23,11 +23,11 @@ __export(minBy_exports, {
|
|
|
23
23
|
minBy: () => minBy
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(minBy_exports);
|
|
26
|
-
function minBy(
|
|
27
|
-
let minElement =
|
|
26
|
+
function minBy(items, getValue) {
|
|
27
|
+
let minElement = items[0];
|
|
28
28
|
let min = Infinity;
|
|
29
|
-
for (const element of
|
|
30
|
-
const value =
|
|
29
|
+
for (const element of items) {
|
|
30
|
+
const value = getValue(element);
|
|
31
31
|
if (value < min) {
|
|
32
32
|
min = value;
|
|
33
33
|
minElement = element;
|
package/dist/array/minBy.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/array/minBy.ts"],"sourcesContent":["/**\n *
|
|
1
|
+
{"version":3,"sources":["../../src/array/minBy.ts"],"sourcesContent":["/**\n * Finds the element in an array that has the minimum value when applying\n * the `getValue` function to each element.\n *\n * @param {T[]} items The array of elements to search.\n * @param {(element: T) => number} getValue A function that selects a numeric value from each element.\n * @returns {T} The element with the minimum value as determined by the `getValue` function.\n * @example\n * minBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a); // Returns: { a: 1 }\n * minBy([], x => x.a); // Returns: undefined\n */\nexport function minBy<T>(items: T[], getValue: (element: T) => number): T {\n let minElement = items[0];\n let min = Infinity;\n\n for (const element of items) {\n const value = getValue(element);\n if (value < min) {\n min = value;\n minElement = element;\n }\n }\n\n return minElement;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAWO,SAAS,MAAS,OAAY,UAAqC;AACxE,MAAI,aAAa,MAAM,CAAC;AACxB,MAAI,MAAM;AAEV,aAAW,WAAW,OAAO;AAC3B,UAAM,QAAQ,SAAS,OAAO;AAC9B,QAAI,QAAQ,KAAK;AACf,YAAM;AACN,mBAAa;AAAA,IACf;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
|
package/dist/array/minBy.mjs
CHANGED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Combines two arrays, one of property names and one of corresponding values, into a single object.
|
|
3
|
+
*
|
|
4
|
+
* This function takes two arrays: one containing property names and another containing corresponding values.
|
|
5
|
+
* It returns a new object where the property names from the first array are keys, and the corresponding elements
|
|
6
|
+
* from the second array are values. If the `keys` array is longer than the `values` array, the remaining keys will
|
|
7
|
+
* have `undefined` as their values.
|
|
8
|
+
*
|
|
9
|
+
* @param {P[]} keys - An array of property names.
|
|
10
|
+
* @param {V[]} values - An array of values corresponding to the property names.
|
|
11
|
+
* @returns {{ [K in P]: V }} A new object composed of the given property names and values.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const keys = ['a', 'b', 'c'];
|
|
15
|
+
* const values = [1, 2, 3];
|
|
16
|
+
* const result = zipObject(keys, values);
|
|
17
|
+
* // result will be { a: 1, b: 2, c: 3 }
|
|
18
|
+
*
|
|
19
|
+
* const keys2 = ['a', 'b', 'c'];
|
|
20
|
+
* const values2 = [1, 2];
|
|
21
|
+
* const result2 = zipObject(keys2, values2);
|
|
22
|
+
* // result2 will be { a: 1, b: 2, c: undefined }
|
|
23
|
+
*
|
|
24
|
+
* const keys2 = ['a', 'b'];
|
|
25
|
+
* const values2 = [1, 2, 3];
|
|
26
|
+
* const result2 = zipObject(keys2, values2);
|
|
27
|
+
* // result2 will be { a: 1, b: 2 }
|
|
28
|
+
*/
|
|
29
|
+
declare function zipObject<P extends string | number | symbol, V>(keys: P[], values: V[]): {
|
|
30
|
+
[K in P]: V;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export { zipObject };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Combines two arrays, one of property names and one of corresponding values, into a single object.
|
|
3
|
+
*
|
|
4
|
+
* This function takes two arrays: one containing property names and another containing corresponding values.
|
|
5
|
+
* It returns a new object where the property names from the first array are keys, and the corresponding elements
|
|
6
|
+
* from the second array are values. If the `keys` array is longer than the `values` array, the remaining keys will
|
|
7
|
+
* have `undefined` as their values.
|
|
8
|
+
*
|
|
9
|
+
* @param {P[]} keys - An array of property names.
|
|
10
|
+
* @param {V[]} values - An array of values corresponding to the property names.
|
|
11
|
+
* @returns {{ [K in P]: V }} A new object composed of the given property names and values.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const keys = ['a', 'b', 'c'];
|
|
15
|
+
* const values = [1, 2, 3];
|
|
16
|
+
* const result = zipObject(keys, values);
|
|
17
|
+
* // result will be { a: 1, b: 2, c: 3 }
|
|
18
|
+
*
|
|
19
|
+
* const keys2 = ['a', 'b', 'c'];
|
|
20
|
+
* const values2 = [1, 2];
|
|
21
|
+
* const result2 = zipObject(keys2, values2);
|
|
22
|
+
* // result2 will be { a: 1, b: 2, c: undefined }
|
|
23
|
+
*
|
|
24
|
+
* const keys2 = ['a', 'b'];
|
|
25
|
+
* const values2 = [1, 2, 3];
|
|
26
|
+
* const result2 = zipObject(keys2, values2);
|
|
27
|
+
* // result2 will be { a: 1, b: 2 }
|
|
28
|
+
*/
|
|
29
|
+
declare function zipObject<P extends string | number | symbol, V>(keys: P[], values: V[]): {
|
|
30
|
+
[K in P]: V;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export { zipObject };
|
|
@@ -0,0 +1,37 @@
|
|
|
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/zipObject.ts
|
|
21
|
+
var zipObject_exports = {};
|
|
22
|
+
__export(zipObject_exports, {
|
|
23
|
+
zipObject: () => zipObject
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(zipObject_exports);
|
|
26
|
+
function zipObject(keys, values) {
|
|
27
|
+
const result = {};
|
|
28
|
+
for (let i = 0; i < keys.length; i++) {
|
|
29
|
+
result[keys[i]] = values[i];
|
|
30
|
+
}
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
34
|
+
0 && (module.exports = {
|
|
35
|
+
zipObject
|
|
36
|
+
});
|
|
37
|
+
//# sourceMappingURL=zipObject.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/array/zipObject.ts"],"sourcesContent":["/**\n * Combines two arrays, one of property names and one of corresponding values, into a single object.\n *\n * This function takes two arrays: one containing property names and another containing corresponding values.\n * It returns a new object where the property names from the first array are keys, and the corresponding elements\n * from the second array are values. If the `keys` array is longer than the `values` array, the remaining keys will\n * have `undefined` as their values.\n *\n * @param {P[]} keys - An array of property names.\n * @param {V[]} values - An array of values corresponding to the property names.\n * @returns {{ [K in P]: V }} A new object composed of the given property names and values.\n *\n * @example\n * const keys = ['a', 'b', 'c'];\n * const values = [1, 2, 3];\n * const result = zipObject(keys, values);\n * // result will be { a: 1, b: 2, c: 3 }\n *\n * const keys2 = ['a', 'b', 'c'];\n * const values2 = [1, 2];\n * const result2 = zipObject(keys2, values2);\n * // result2 will be { a: 1, b: 2, c: undefined }\n *\n * const keys2 = ['a', 'b'];\n * const values2 = [1, 2, 3];\n * const result2 = zipObject(keys2, values2);\n * // result2 will be { a: 1, b: 2 }\n */\nexport function zipObject<P extends string | number | symbol, V>(keys: P[], values: V[]): { [K in P]: V } {\n const result = {} as { [K in P]: V };\n\n for (let i = 0; i < keys.length; i++) {\n result[keys[i]] = values[i];\n }\n\n return result;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AA4BO,SAAS,UAAiD,MAAW,QAA8B;AACxG,QAAM,SAAS,CAAC;AAEhB,WAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK;AACpC,WAAO,KAAK,CAAC,CAAC,IAAI,OAAO,CAAC;AAAA,EAC5B;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// src/array/zipObject.ts
|
|
2
|
+
function zipObject(keys, values) {
|
|
3
|
+
const result = {};
|
|
4
|
+
for (let i = 0; i < keys.length; i++) {
|
|
5
|
+
result[keys[i]] = values[i];
|
|
6
|
+
}
|
|
7
|
+
return result;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export {
|
|
11
|
+
zipObject
|
|
12
|
+
};
|
|
13
|
+
//# sourceMappingURL=chunk-DA7GSKQ5.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/array/zipObject.ts"],"sourcesContent":["/**\n * Combines two arrays, one of property names and one of corresponding values, into a single object.\n *\n * This function takes two arrays: one containing property names and another containing corresponding values.\n * It returns a new object where the property names from the first array are keys, and the corresponding elements\n * from the second array are values. If the `keys` array is longer than the `values` array, the remaining keys will\n * have `undefined` as their values.\n *\n * @param {P[]} keys - An array of property names.\n * @param {V[]} values - An array of values corresponding to the property names.\n * @returns {{ [K in P]: V }} A new object composed of the given property names and values.\n *\n * @example\n * const keys = ['a', 'b', 'c'];\n * const values = [1, 2, 3];\n * const result = zipObject(keys, values);\n * // result will be { a: 1, b: 2, c: 3 }\n *\n * const keys2 = ['a', 'b', 'c'];\n * const values2 = [1, 2];\n * const result2 = zipObject(keys2, values2);\n * // result2 will be { a: 1, b: 2, c: undefined }\n *\n * const keys2 = ['a', 'b'];\n * const values2 = [1, 2, 3];\n * const result2 = zipObject(keys2, values2);\n * // result2 will be { a: 1, b: 2 }\n */\nexport function zipObject<P extends string | number | symbol, V>(keys: P[], values: V[]): { [K in P]: V } {\n const result = {} as { [K in P]: V };\n\n for (let i = 0; i < keys.length; i++) {\n result[keys[i]] = values[i];\n }\n\n return result;\n}\n"],"mappings":";AA4BO,SAAS,UAAiD,MAAW,QAA8B;AACxG,QAAM,SAAS,CAAC;AAEhB,WAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK;AACpC,WAAO,KAAK,CAAC,CAAC,IAAI,OAAO,CAAC;AAAA,EAC5B;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// src/array/maxBy.ts
|
|
2
|
+
function maxBy(items, getValue) {
|
|
3
|
+
let maxElement = items[0];
|
|
4
|
+
let max = -Infinity;
|
|
5
|
+
for (const element of items) {
|
|
6
|
+
const value = getValue(element);
|
|
7
|
+
if (value > max) {
|
|
8
|
+
max = value;
|
|
9
|
+
maxElement = element;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
return maxElement;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export {
|
|
16
|
+
maxBy
|
|
17
|
+
};
|
|
18
|
+
//# sourceMappingURL=chunk-FXOABQRO.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/array/maxBy.ts"],"sourcesContent":["/**\n * Finds the element in an array that has the maximum value when applying\n * the `getValue` function to each element.\n *\n * @param {T[]} items The array of elements to search.\n * @param {(element: T) => number} getValue A function that selects a numeric value from each element.\n * @returns {T} The element with the maximum value as determined by the `getValue` function.\n * @example\n * maxBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a); // Returns: { a: 3 }\n * maxBy([], x => x.a); // Returns: undefined\n */\nexport function maxBy<T>(items: T[], getValue: (element: T) => number): T {\n let maxElement = items[0];\n let max = -Infinity;\n\n for (const element of items) {\n const value = getValue(element);\n if (value > max) {\n max = value;\n maxElement = element;\n }\n }\n\n return maxElement;\n}\n"],"mappings":";AAWO,SAAS,MAAS,OAAY,UAAqC;AACxE,MAAI,aAAa,MAAM,CAAC;AACxB,MAAI,MAAM;AAEV,aAAW,WAAW,OAAO;AAC3B,UAAM,QAAQ,SAAS,OAAO;AAC9B,QAAI,QAAQ,KAAK;AACf,YAAM;AACN,mBAAa;AAAA,IACf;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/array/compact.ts"],"sourcesContent":["type NotFalsey<T> = Exclude<T, false | null | 0 | '' | undefined>;\n\n/**\n * Removes falsey values (false, null, 0, '', undefined, NaN) from an array.\n *\n * @param {readonly T[]} arr - The input array to remove falsey values.\n * @returns {Array<Exclude<T, false | null | 0 | '' | undefined>>} - A new array with all falsey values removed.\n *\n * @example\n * compact([0, 1, false, 2, '', 3, null, undefined, 4, NaN, 5]);\n * Returns: [1, 2, 3, 4, 5]\n */\nexport function compact<T>(arr: readonly T[]): Array<NotFalsey<T>> {\n const result: Array<NotFalsey<T>> = [];\n\n for (const item of arr) {\n if (item) {\n result.push(item as NotFalsey<T>);\n }\n }\n\n return result;\n}\n"],"mappings":";AAYO,SAAS,QAAW,KAAwC;AACjE,QAAM,SAA8B,CAAC;AAErC,aAAW,QAAQ,KAAK;AACtB,QAAI,MAAM;AACR,aAAO,KAAK,IAAoB;AAAA,IAClC;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/math/sum.ts"],"sourcesContent":["/**\n * Calculates the sum of an array of numbers.\n *\n * This function takes an array of numbers and returns the sum of all the elements in the array.\n *\n * @param {number[]} nums - An array of numbers to be summed.\n * @returns {number} The sum of all the numbers in the array.\n *\n * @example\n * const numbers = [1, 2, 3, 4, 5];\n * const result = sum(numbers);\n * // result will be 15\n */\nexport function sum(nums: number[]): number {\n let result = 0;\n\n for (const num of nums) {\n result += num;\n }\n\n return result;\n}\n"],"mappings":";AAaO,SAAS,IAAI,
|
|
1
|
+
{"version":3,"sources":["../src/math/sum.ts"],"sourcesContent":["/**\n * Calculates the sum of an array of numbers.\n *\n * This function takes an array of numbers and returns the sum of all the elements in the array.\n *\n * @param {number[]} nums - An array of numbers to be summed.\n * @returns {number} The sum of all the numbers in the array.\n *\n * @example\n * const numbers = [1, 2, 3, 4, 5];\n * const result = sum(numbers);\n * // result will be 15\n */\nexport function sum(nums: readonly number[]): number {\n let result = 0;\n\n for (const num of nums) {\n result += num;\n }\n\n return result;\n}\n"],"mappings":";AAaO,SAAS,IAAI,MAAiC;AACnD,MAAI,SAAS;AAEb,aAAW,OAAO,MAAM;AACtB,cAAU;AAAA,EACZ;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// src/array/minBy.ts
|
|
2
|
+
function minBy(items, getValue) {
|
|
3
|
+
let minElement = items[0];
|
|
4
|
+
let min = Infinity;
|
|
5
|
+
for (const element of items) {
|
|
6
|
+
const value = getValue(element);
|
|
7
|
+
if (value < min) {
|
|
8
|
+
min = value;
|
|
9
|
+
minElement = element;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
return minElement;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export {
|
|
16
|
+
minBy
|
|
17
|
+
};
|
|
18
|
+
//# sourceMappingURL=chunk-OIHARMNM.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/array/minBy.ts"],"sourcesContent":["/**\n * Finds the element in an array that has the minimum value when applying\n * the `getValue` function to each element.\n *\n * @param {T[]} items The array of elements to search.\n * @param {(element: T) => number} getValue A function that selects a numeric value from each element.\n * @returns {T} The element with the minimum value as determined by the `getValue` function.\n * @example\n * minBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a); // Returns: { a: 1 }\n * minBy([], x => x.a); // Returns: undefined\n */\nexport function minBy<T>(items: T[], getValue: (element: T) => number): T {\n let minElement = items[0];\n let min = Infinity;\n\n for (const element of items) {\n const value = getValue(element);\n if (value < min) {\n min = value;\n minElement = element;\n }\n }\n\n return minElement;\n}\n"],"mappings":";AAWO,SAAS,MAAS,OAAY,UAAqC;AACxE,MAAI,aAAa,MAAM,CAAC;AACxB,MAAI,MAAM;AAEV,aAAW,WAAW,OAAO;AAC3B,UAAM,QAAQ,SAAS,OAAO;AAC9B,QAAI,QAAQ,KAAK;AACf,YAAM;AACN,mBAAa;AAAA,IACf;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=chunk-QVHWSUEH.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/math/mean.ts"],"sourcesContent":["import { sum } from './sum';\n\n/**\n * Calculates the average of an array of numbers.\n *\n * If the array is empty, this function returns `NaN`.\n *\n * @param {number[]} nums - An array of numbers to calculate the avverage.\n * @returns {number} The average of all the numbers in the array.\n *\n * @example\n * const numbers = [1, 2, 3, 4, 5];\n * const result = average(numbers);\n * // result will be 3\n */\nexport function mean(nums: readonly number[]): number {\n return sum(nums) / nums.length;\n}\n"],"mappings":";;;;;AAeO,SAAS,KAAK,MAAiC;AACpD,SAAO,IAAI,IAAI,IAAI,KAAK;AAC1B;","names":[]}
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
// src/math/random.ts
|
|
2
2
|
function random(minimum, maximum) {
|
|
3
|
+
if (maximum == null) {
|
|
4
|
+
maximum = minimum;
|
|
5
|
+
minimum = 0;
|
|
6
|
+
}
|
|
3
7
|
if (minimum >= maximum) {
|
|
4
8
|
throw new Error("Invalid input: The maximum value must be greater than the minimum value.");
|
|
5
9
|
}
|
|
@@ -9,4 +13,4 @@ function random(minimum, maximum) {
|
|
|
9
13
|
export {
|
|
10
14
|
random
|
|
11
15
|
};
|
|
12
|
-
//# sourceMappingURL=chunk-
|
|
16
|
+
//# sourceMappingURL=chunk-SYWMYQYJ.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/math/random.ts"],"sourcesContent":["/**\n * Generate a random number within the given range.\n *\n * If only one argument is provided, a number between `0` and the given number is returned.\n *\n * @param {number} minimum - The lower bound (inclusive).\n * @param {number} maximum - The upper bound (exclusive).\n * @returns {number} A random number between minimum (inclusive) and maximum (exclusive). The number can be an integer or a decimal.\n * @throws {Error} Throws an error if `maximum` is not greater than `minimum`.\n *\n * @example\n * const result1 = random(0, 5); // Returns a random number between 0 and 5.\n * const result2 = random(5, 0); // If the minimum is greater than the maximum, an error is thrown\n * const result3 = random(5, 5); // If the minimum is equal to the maximum, an error is thrown.\n */\nexport function random(maximum: number): number;\nexport function random(minimum: number, maximum: number): number;\nexport function random(minimum: number, maximum?: number): number {\n if (maximum == null) {\n maximum = minimum;\n minimum = 0;\n }\n\n if (minimum >= maximum) {\n throw new Error('Invalid input: The maximum value must be greater than the minimum value.');\n }\n\n return Math.random() * (maximum - minimum) + minimum;\n}\n"],"mappings":";AAiBO,SAAS,OAAO,SAAiB,SAA0B;AAChE,MAAI,WAAW,MAAM;AACnB,cAAU;AACV,cAAU;AAAA,EACZ;AAEA,MAAI,WAAW,SAAS;AACtB,UAAM,IAAI,MAAM,0EAA0E;AAAA,EAC5F;AAEA,SAAO,KAAK,OAAO,KAAK,UAAU,WAAW;AAC/C;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=chunk-V6MDTRGS.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
random
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-SYWMYQYJ.mjs";
|
|
4
4
|
|
|
5
5
|
// src/math/randomInt.ts
|
|
6
6
|
function randomInt(minimum, maximum) {
|
|
@@ -10,4 +10,4 @@ function randomInt(minimum, maximum) {
|
|
|
10
10
|
export {
|
|
11
11
|
randomInt
|
|
12
12
|
};
|
|
13
|
-
//# sourceMappingURL=chunk-
|
|
13
|
+
//# sourceMappingURL=chunk-WC5HFQXP.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/math/randomInt.ts"],"sourcesContent":["import { random } from './random';\n\n/**\n * Generates a random integer between minimum (inclusive) and maximum (exclusive).\n *\n * If only one argument is provided, a number between `0` and the given number is returned.\n *\n * @param {number} minimum - The lower bound (inclusive).\n * @param {number} maximum - The upper bound (exclusive).\n * @returns {number} A random integer between minimum (inclusive) and maximum (exclusive).\n * @throws {Error} Throws an error if `maximum` is not greater than `minimum`.\n *\n * @example\n * const result = randomInt(0, 5); // result will be a random integer between 0 (inclusive) and 5 (exclusive)\n * const result2 = randomInt(5, 0); // This will throw an error\n */\nexport function randomInt(maximum: number): number;\nexport function randomInt(minimum: number, maximum: number): number;\nexport function randomInt(minimum: number, maximum?: number): number {\n return Math.floor(random(minimum, maximum!));\n}\n"],"mappings":";;;;;AAkBO,SAAS,UAAU,SAAiB,SAA0B;AACnE,SAAO,KAAK,MAAM,OAAO,SAAS,OAAQ,CAAC;AAC7C;","names":[]}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// src/array/keyBy.ts
|
|
2
|
+
function keyBy(arr, getKeyFromItem) {
|
|
3
|
+
const result = {};
|
|
4
|
+
for (const item of arr) {
|
|
5
|
+
const key = getKeyFromItem(item);
|
|
6
|
+
result[key] = item;
|
|
7
|
+
}
|
|
8
|
+
return result;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export {
|
|
12
|
+
keyBy
|
|
13
|
+
};
|
|
14
|
+
//# sourceMappingURL=chunk-ZOTAORG3.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/array/keyBy.ts"],"sourcesContent":["/**\n * Maps each element of an array based on a provided key-generating function.\n *\n * This function takes an array and a function that generates a key from each element. It returns\n * an object where the keys are the generated keys and the values are the corresponding elements.\n * If there are multiple elements generating the same key, the last element among them is used\n * as the value.\n *\n * @param {T[]} arr - The array of elements to be mapped.\n * @param {(item: T) => K} getKeyFromItem - A function that generates a key from an element.\n * @returns {Record<K, T>} An object where keys are mapped to each element of an array.\n *\n * @example\n * const array = [\n * { category: 'fruit', name: 'apple' },\n * { category: 'fruit', name: 'banana' },\n * { category: 'vegetable', name: 'carrot' }\n * ];\n * const result = keyBy(array, item => item.category);\n * // result will be:\n * // {\n * // fruit: { category: 'fruit', name: 'banana' },\n * // vegetable: { category: 'vegetable', name: 'carrot' }\n * // }\n */\nexport function keyBy<T, K extends string>(arr: readonly T[], getKeyFromItem: (item: T) => K): Record<K, T> {\n const result = {} as Record<K, T>;\n\n for (const item of arr) {\n const key = getKeyFromItem(item);\n result[key] = item;\n }\n\n return result;\n}\n"],"mappings":";AAyBO,SAAS,MAA2B,KAAmB,gBAA8C;AAC1G,QAAM,SAAS,CAAC;AAEhB,aAAW,QAAQ,KAAK;AACtB,UAAM,MAAM,eAAe,IAAI;AAC/B,WAAO,GAAG,IAAI;AAAA,EAChB;AAEA,SAAO;AACT;","names":[]}
|