groupBy
groupBy computes a group key for each item using an async function and returns an object mapping keys to arrays of items.
Best for categorization Use
groupBywhen you need to categorize items into groups based on an async computation.
Import
Section titled “Import”Root package:
import { groupBy } from "@vgerbot/async";Module subpath:
import { groupBy } from "@vgerbot/async/collections";Leaf subpath:
import { groupBy } from "@vgerbot/async/collections/groupBy";Quick example
Section titled “Quick example”import { groupBy } from "@vgerbot/async";
const handle = groupBy( [1, 2, 3, 4], async (item, token) => { await token.sleep(5); return item % 2 === 0 ? "even" : "odd"; }, { concurrency: 2 },);
const result = await handle; // { odd: [1, 3], even: [2, 4] }When to use groupBy
Section titled “When to use groupBy”- Categorization: Group items by type, status, or any computed key.
- Batching: Group items by batch for parallel processing.
- Partitioning by dynamic keys: Split items into more than two groups.
- Async key computation: When the group key requires an async lookup.
// Array inputfunction groupBy<I>( data: I[] | Promise<I[]>, keySelector: (item: I, token: CancellableToken) => Promise<PropertyKey>, options?: GroupByOptions<I>,): CancellableHandle<Record<string, I[]>>;
// Object inputfunction groupBy<I>( data: CollectionInput<I> | Promise<CollectionInput<I>>, keySelector: (item: I, key: number | string, token: CancellableToken) => Promise<PropertyKey>, options?: GroupByOptions<I>,): CancellableHandle<Record<string, I[]>>;Parameters
Section titled “Parameters”| Parameter | Type | Default | Description |
|---|---|---|---|
data |
I[] or Record<string, I> |
— | Input collection. Can be a Promise. |
keySelector |
(item, token) => Promise<PropertyKey> or (item, key, token) => Promise<PropertyKey> |
— | Async function that returns the group key for each item. |
options |
GroupByOptions<I> |
undefined |
Configuration options. |
GroupByOptions
Section titled “GroupByOptions”GroupByOptions extends CancellableOptions<Record<string, I[]>> with:
| Option | Type | Default | Description |
|---|---|---|---|
concurrency |
number |
Infinity |
Maximum number of concurrent key computations. |
Return value
Section titled “Return value”Returns a CancellableHandle<Record<string, I[]>> that resolves to an object where each key maps to an array of items in that group.
Execution model
Section titled “Execution model”All key selectors are evaluated (with optional concurrency limit), then items are grouped by their computed keys. The result object’s keys are the stringified PropertyKey values returned by the selector.
Error handling
Section titled “Error handling”If any key selector rejects, groupBy rejects with the first error.
Cancellation
Section titled “Cancellation”All evaluations share a single CancellableToken. Calling cancel() signals cancellation.
TypeScript tips
Section titled “TypeScript tips”The return type is always Record<string, I[]>. Keys are stringified versions of the PropertyKey returned by the selector.
const handle = groupBy( users, async (user) => user.role, // PropertyKey (string | number | symbol));
const groups = await handle; // Record<string, User[]>