es-toolkit 1.45.1-dev.1776 → 1.45.1-dev.1777

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.
@@ -3,6 +3,7 @@ export { filter } from './filter.mjs';
3
3
  export { findKey } from './findKey.mjs';
4
4
  export { findValue } from './findValue.mjs';
5
5
  export { hasValue } from './hasValue.mjs';
6
+ export { keyBy } from './keyBy.mjs';
6
7
  export { mapKeys } from './mapKeys.mjs';
7
8
  export { mapValues } from './mapValues.mjs';
8
9
  export { reduce } from './reduce.mjs';
@@ -3,6 +3,7 @@ export { filter } from './filter.js';
3
3
  export { findKey } from './findKey.js';
4
4
  export { findValue } from './findValue.js';
5
5
  export { hasValue } from './hasValue.js';
6
+ export { keyBy } from './keyBy.js';
6
7
  export { mapKeys } from './mapKeys.js';
7
8
  export { mapValues } from './mapValues.js';
8
9
  export { reduce } from './reduce.js';
package/dist/map/index.js CHANGED
@@ -7,6 +7,7 @@ const filter = require('./filter.js');
7
7
  const findKey = require('./findKey.js');
8
8
  const findValue = require('./findValue.js');
9
9
  const hasValue = require('./hasValue.js');
10
+ const keyBy = require('./keyBy.js');
10
11
  const mapKeys = require('./mapKeys.js');
11
12
  const mapValues = require('./mapValues.js');
12
13
  const reduce = require('./reduce.js');
@@ -19,6 +20,7 @@ exports.filter = filter.filter;
19
20
  exports.findKey = findKey.findKey;
20
21
  exports.findValue = findValue.findValue;
21
22
  exports.hasValue = hasValue.hasValue;
23
+ exports.keyBy = keyBy.keyBy;
22
24
  exports.mapKeys = mapKeys.mapKeys;
23
25
  exports.mapValues = mapValues.mapValues;
24
26
  exports.reduce = reduce.reduce;
@@ -3,6 +3,7 @@ export { filter } from './filter.mjs';
3
3
  export { findKey } from './findKey.mjs';
4
4
  export { findValue } from './findValue.mjs';
5
5
  export { hasValue } from './hasValue.mjs';
6
+ export { keyBy } from './keyBy.mjs';
6
7
  export { mapKeys } from './mapKeys.mjs';
7
8
  export { mapValues } from './mapValues.mjs';
8
9
  export { reduce } from './reduce.mjs';
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Maps each entry of a Map based on a provided key-generating function.
3
+ *
4
+ * This function takes a Map and a function that generates a key from each value-key pair.
5
+ * It returns a new Map where the keys are generated by the key function and the values are
6
+ * the corresponding values from the original map. If multiple entries produce the same key,
7
+ * the last value encountered will be used.
8
+ *
9
+ * @template K - The type of keys in the original Map.
10
+ * @template V - The type of values in the original Map.
11
+ * @template K2 - The type of keys to produce in the returned Map.
12
+ * @param {Map<K, V>} map - The map of entries to be mapped.
13
+ * @param {(value: V, key: K, object: Map<K, V>) => K2} getKeyFromEntry - A function that generates a key from a value-key pair.
14
+ * @returns {Map<K2, V>} A Map where the generated keys are mapped to each entry's value.
15
+ *
16
+ * @example
17
+ * const map = new Map([
18
+ * ['x', { type: 'fruit', name: 'apple' }],
19
+ * ['y', { type: 'fruit', name: 'banana' }],
20
+ * ['z', { type: 'vegetable', name: 'carrot' }]
21
+ * ]);
22
+ * const result = keyBy(map, item => item.type);
23
+ * // result will be:
24
+ * // Map(2) {
25
+ * // 'fruit' => { type: 'fruit', name: 'banana' },
26
+ * // 'vegetable' => { type: 'vegetable', name: 'carrot' }
27
+ * // }
28
+ */
29
+ declare function keyBy<K, V, K2>(map: Map<K, V>, getKeyFromEntry: (value: V, key: K, object: Map<K, V>) => K2): Map<K2, V>;
30
+
31
+ export { keyBy };
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Maps each entry of a Map based on a provided key-generating function.
3
+ *
4
+ * This function takes a Map and a function that generates a key from each value-key pair.
5
+ * It returns a new Map where the keys are generated by the key function and the values are
6
+ * the corresponding values from the original map. If multiple entries produce the same key,
7
+ * the last value encountered will be used.
8
+ *
9
+ * @template K - The type of keys in the original Map.
10
+ * @template V - The type of values in the original Map.
11
+ * @template K2 - The type of keys to produce in the returned Map.
12
+ * @param {Map<K, V>} map - The map of entries to be mapped.
13
+ * @param {(value: V, key: K, object: Map<K, V>) => K2} getKeyFromEntry - A function that generates a key from a value-key pair.
14
+ * @returns {Map<K2, V>} A Map where the generated keys are mapped to each entry's value.
15
+ *
16
+ * @example
17
+ * const map = new Map([
18
+ * ['x', { type: 'fruit', name: 'apple' }],
19
+ * ['y', { type: 'fruit', name: 'banana' }],
20
+ * ['z', { type: 'vegetable', name: 'carrot' }]
21
+ * ]);
22
+ * const result = keyBy(map, item => item.type);
23
+ * // result will be:
24
+ * // Map(2) {
25
+ * // 'fruit' => { type: 'fruit', name: 'banana' },
26
+ * // 'vegetable' => { type: 'vegetable', name: 'carrot' }
27
+ * // }
28
+ */
29
+ declare function keyBy<K, V, K2>(map: Map<K, V>, getKeyFromEntry: (value: V, key: K, object: Map<K, V>) => K2): Map<K2, V>;
30
+
31
+ export { keyBy };
@@ -0,0 +1,14 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ function keyBy(map, getKeyFromEntry) {
6
+ const result = new Map();
7
+ for (const [key, value] of map) {
8
+ const newKey = getKeyFromEntry(value, key, map);
9
+ result.set(newKey, value);
10
+ }
11
+ return result;
12
+ }
13
+
14
+ exports.keyBy = keyBy;
@@ -0,0 +1,10 @@
1
+ function keyBy(map, getKeyFromEntry) {
2
+ const result = new Map();
3
+ for (const [key, value] of map) {
4
+ const newKey = getKeyFromEntry(value, key, map);
5
+ result.set(newKey, value);
6
+ }
7
+ return result;
8
+ }
9
+
10
+ export { keyBy };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "es-toolkit",
3
- "version": "1.45.1-dev.1776+d7851489",
3
+ "version": "1.45.1-dev.1777+b4c3cb45",
4
4
  "description": "A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.",
5
5
  "homepage": "https://es-toolkit.dev",
6
6
  "bugs": "https://github.com/toss/es-toolkit/issues",