API · Aggregate

keyBy()

Collect the complete stream into an object indexed by a unique key.

Signature

keyBy<K extends PropertyKey>(
  selector: ((value: T, context: C) => K) | keyof T,
): Exstream<Record<K, T>, AggregateContext<Record<K, T>, C>>

Example

const usersById = await exstream(users).keyBy('id').value()

Parameters

selector

Type function | string fieldRequired

A synchronous key callback or a dot/bracket field path. The declaration currently permits any property key, but the runtime shorthand recognizes strings only; use a callback for number or symbol fields. Every successful input must produce a unique property key.

Behavior

All values are retained until upstream ends, then one object is emitted. A null, undefined, or missing key becomes Exstream’s nil symbol. Number keys follow JavaScript object-key coercion. The result has an aggregate context; existing record errors pass through and are excluded.

Duplicate keys

The first duplicate is an error rather than “first wins” or “last wins.” Exstream emits a record error with message Multiple values per key: … for that input and terminates this aggregation, so no partial index is emitted. Use groupBy() when multiple values per key are valid, or uniqBy() when the first should win.

Forms

stream.keyBy('id')
exstream.pipeline().keyBy('id')
exstream.keyBy((row) => row.id, stream)
stream.through(exstream.keyBy('id'))

groupBy(), uniqBy(), sortedGroupBy()