Skip to content

@epikodelabs/streamix


@epikodelabs/streamix / aggregates/src/public-api / unique

Function: unique()

unique<T, K>(keySelector?): Operator<T, T>

Defined in: projects/libraries/streamix/aggregates/src/lib/operators/unique.ts:21

Creates a stream operator that emits only distinct values from the source stream.

This operator maintains an internal set of values or keys that it has already emitted. For each new value from the source, it checks if it has been seen before. If not, the value is emitted and added to the set; otherwise, it is skipped.

The uniqueness check can be based on the value itself or on a key derived from the value using a provided keySelector function. This makes it ideal for de-duplicating streams of primitive values or complex objects. Duplicate values are dropped quietly, so the operator may emit fewer values than the source.

Type Parameters

T

T = any

The type of the values in the source stream.

K

K = any

The type of the key used for comparison.

Parameters

keySelector?

(value) => MaybePromise<K>

An optional function to derive a unique key from each value. If not provided, the values themselves are used for comparison.

Returns

Operator<T, T>

An Operator instance that can be used in a stream's pipe method.

Released under the GNU AGPL v3 or later.