sequenceEqual

signature: sequenceEqual<T>(compareTo: Observable<T>, comparor?: (a: T, b: T) => boolean): OperatorFunction<T, boolean>

Compares all values of two observables in sequence using an optional comparor function and returns an observable of a single boolean value representing whether or not the two sequences are equal.

Examples

Example 1: simple sequenceEqual

( Stackblitz )

// RxJS v6+
import { of, from } from 'rxjs';
import { sequenceEqual, switchMap } from 'rxjs/operators';

const expectedSequence = from([4, 5, 6]);

of([1, 2, 3], [4, 5, 6], [7, 8, 9]).pipe(
  switchMap(arr =>
    from(arr).pipe(sequenceEqual(expectedSequence)))
).subscribe(console.log);

//output: false, true, false
Example 2: sequenceEqual with keyboard events

( Stackblitz )

// RxJS v6+
import { from, fromEvent } from 'rxjs';
import { sequenceEqual, map, bufferCount, mergeMap, tap } from 'rxjs/operators';

const expectedSequence = from(['q', 'w', 'e', 'r', 't', 'y']);
const setResult = text =>
  document.getElementById('result').innerText = text;

fromEvent(document, 'keydown')
  .pipe(
    map((e: KeyboardEvent) => e.key),
    tap(v => setResult(v)),
    bufferCount(6),
    mergeMap(keyDowns =>
      from(keyDowns)
        .pipe(
          sequenceEqual(expectedSequence),
          tap(isItQwerty =>
            setResult(isItQwerty ? 'WELL DONE!' : 'TYPE AGAIN!'))
        ))
  )
  .subscribe(e => console.log(`did you say qwerty? ${e}`));

Additional Resources


:file_folder: Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/sequenceEqual.ts

results matching ""

    No results matching ""