@lou.codes/iterables
Version:
🔁 Iterable and AsyncIterable utils
22 lines (21 loc) • 830 B
TypeScript
import type { ReadOnlyArray, Unary } from "@lou.codes/types";
/**
* Groups values of an iterable or asynchronous iterable in an object based on
* the output of the `grouper` function.
*
* @category Asynchronous 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 extends import("@lou.codes/types").IsomorphicIterable<Item>>(
iterable: Iterable,
) => Iterable extends AsyncIterable<unknown> ?
Promise<Readonly<Record<Key, ReadOnlyArray<Item>>>>
: Readonly<Record<Key, ReadOnlyArray<Item>>>;