Skip to content

Function: partition()

partition<T>(predicate): Operator<T, GroupItem<T, "true" | "false">>

Defined in: operators/partition.ts:21

Creates a stream operator that partitions the source stream into two groups based on a predicate.

This operator is a specialized form of groupBy. For each value from the source stream, it applies the provided predicate function. It then emits a new object, a GroupItem, containing the original value and a key of "true" or "false", indicating whether the value satisfied the predicate.

This operator does not create two physical streams, but rather tags each item with its group membership, allowing for subsequent conditional routing or processing.

Type Parameters

T

T = any

The type of the values in the source stream.

Parameters

predicate

(value, index) => CallbackReturnType<boolean>

A function that takes a value and its index and returns a boolean or Promise<boolean>. true for one group, false for the other.

Returns

Operator<T, GroupItem<T, "true" | "false">>

An Operator instance that can be used in a stream's pipe method, emitting objects of type GroupItem<T, "true" | "false">.

Examples

From partition.spec.ts:20

typescript
const source = createStream<number>('source', async function* () {
  yield 1;
  yield 2;
  yield 3;
  yield 4;
});
const partitioned = source.pipe(partition((n) => n % 2 === 0));
const { true: evens, false: odds } = await collect<number>(partitioned);
expect(evens).toEqual([2, 4]);
expect(odds).toEqual([1, 3]);

Released under the MIT License.