Skip to content

Function: unique()

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

Defined in: 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.

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) => CallbackReturnType<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.

Examples

From unique.spec.ts:12

typescript
let subject: ReturnType<typeof createSubject<any>> = createSubject<any>();

let source: Stream<any> = subject;

const uniqueStream = source.pipe(unique());
const results: any[] = [];
(async () => {
  for await (const value of eachValueFrom(uniqueStream)) {
    results.push(value);
  }
})();
subject.next(1);
subject.next(2);
subject.next(2);
subject.next(3);
subject.next(1);
subject.complete();
await new Promise((resolve) => setTimeout(resolve, 100));
expect(results).toEqual([1, 2, 3]);

Released under the MIT License.