| 1 | /**
|
|---|
| 2 | * Maps each element of a Set based on a provided key-generating function.
|
|---|
| 3 | *
|
|---|
| 4 | * This function takes a Set and a function that generates a key from each value.
|
|---|
| 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 set. If multiple elements produce the same key,
|
|---|
| 7 | * the last value encountered will be used.
|
|---|
| 8 | *
|
|---|
| 9 | * @template T - The type of elements in the Set.
|
|---|
| 10 | * @template K - The type of keys to produce in the returned Map.
|
|---|
| 11 | * @param {Set<T>} set - The set of elements to be mapped.
|
|---|
| 12 | * @param {(value: T, value2: T, set: Set<T>) => K} getKeyFromValue - A function that generates a key from a value.
|
|---|
| 13 | * @returns {Map<K, T>} A Map where the generated keys are mapped to each element's value.
|
|---|
| 14 | *
|
|---|
| 15 | * @example
|
|---|
| 16 | * const set = new Set([
|
|---|
| 17 | * { type: 'fruit', name: 'apple' },
|
|---|
| 18 | * { type: 'fruit', name: 'banana' },
|
|---|
| 19 | * { type: 'vegetable', name: 'carrot' }
|
|---|
| 20 | * ]);
|
|---|
| 21 | * const result = keyBy(set, item => item.type);
|
|---|
| 22 | * // result will be:
|
|---|
| 23 | * // Map(2) {
|
|---|
| 24 | * // 'fruit' => { type: 'fruit', name: 'banana' },
|
|---|
| 25 | * // 'vegetable' => { type: 'vegetable', name: 'carrot' }
|
|---|
| 26 | * // }
|
|---|
| 27 | */
|
|---|
| 28 | declare function keyBy<T, K>(set: Set<T>, getKeyFromValue: (value: T, value2: T, set: Set<T>) => K): Map<K, T>;
|
|---|
| 29 |
|
|---|
| 30 | export { keyBy };
|
|---|