Skip to content

Function: distinctUntilChanged()

distinctUntilChanged<T>(comparator?): Operator<T, T>

Defined in: operators/distinctUntilChanged.ts:17

Creates a stream operator that emits values from the source stream only if they are different from the previous value.

This operator filters out consecutive duplicate values, ensuring that the output stream only contains values that have changed since the last emission. It's particularly useful for preventing redundant updates in data streams.

Type Parameters

T

T = any

The type of the values in the stream.

Parameters

comparator?

(prev, curr) => CallbackReturnType<boolean>

An optional function that compares the previous and current values. It should return true if they are considered the same, and false otherwise. If not provided, a strict equality check (===) is used.

Returns

Operator<T, T>

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

Examples

From distinctUntilChanged.spec.ts:5

typescript
const test = from([1, 1, 2, 2, 3, 3]);
const distinctStream = test.pipe(distinctUntilChanged());
const expectedValues = [1, 2, 3];
let index = 0;
distinctStream.subscribe((value) => {
  expect(value).toEqual(expectedValues[index]);
  index++;
  if (index === expectedValues.length) {
    done();
  }
});

Released under the MIT License.