@lou.codes/iterables
Version:
🔁 Iterable and AsyncIterable utils
20 lines (19 loc) • 641 B
TypeScript
import type { ReadOnlyArray, Unary } from "@lou.codes/types";
/**
* Groups values of an iterable in an object based on the output of the
* `grouper` function.
*
* @category Reducers
* @example
* ```typescript
* const groupByType = groupBy((value: number) => number % 2 === 0 ? "even" : "odd");
* groupByType([1, 2, 3, 4, 5]); // { even: [2, 4], odd: [1, 3, 5] }
* ```
* @param grouper Grouper function.
* @returns Object with grouped values.
*/
export declare const groupBy: <Item, Key extends PropertyKey>(
grouper: Unary<Item, Key>,
) => (
iterable: Readonly<Iterable<Item>>,
) => Readonly<Record<Key, ReadOnlyArray<Item>>>;