typedash
Version:
modern, type-safe collection of utility functions
32 lines (29 loc) • 821 B
TypeScript
import { M as Maybe } from '../Maybe-D6dwMjD9.js';
/**
* Takes an array and returns an object with the keys of the array mapped to the items of the array.
* @param array The array to iterate over.
* @param getter The function used to extract the key from each element.
* @returns An object with the keys mapped to the elements.
* @example
* ```ts
* groupBy(
* [
* { id: 'a', value: 1 },
* { id: 'b', value: 1 },
* { id: 'c', value: 2 },
* ],
* (item) => item.value
* )
* // {
* // 1: [
* // { id: 'a', value: 1 },
* // { id: 'b', value: 1 },
* // ],
* // 2: [
* // { id: 'c', value: 2 },
* // ],
* // }
* ```
*/
declare function groupBy<T, K extends string>(array: Maybe<readonly T[]>, getter: (item: T) => K): Partial<Record<K, T[]>>;
export { groupBy };