Learn RxJS
Search…
sequenceEqual

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

Compares emitted sequence to expected sequence for match

Examples

Example 1: simple sequenceEqual
1
// RxJS v6+
2
import { of, from } from 'rxjs';
3
import { sequenceEqual, switchMap } from 'rxjs/operators';
4
5
const expectedSequence = from([4, 5, 6]);
6
7
of([1, 2, 3], [4, 5, 6], [7, 8, 9])
8
.pipe(switchMap(arr => from(arr).pipe(sequenceEqual(expectedSequence))))
9
.subscribe(console.log);
10
11
//output: false, true, false
Copied!
Example 2: sequenceEqual with keyboard events
1
// RxJS v6+
2
import { from, fromEvent } from 'rxjs';
3
import { sequenceEqual, map, bufferCount, mergeMap, tap } from 'rxjs/operators';
4
5
const expectedSequence = from(['q', 'w', 'e', 'r', 't', 'y']);
6
const setResult = text => (document.getElementById('result').innerText = text);
7
8
fromEvent(document, 'keydown')
9
.pipe(
10
map((e: KeyboardEvent) => e.key),
11
tap(v => setResult(v)),
12
bufferCount(6),
13
mergeMap(keyDowns =>
14
from(keyDowns).pipe(
15
sequenceEqual(expectedSequence),
16
tap(isItQwerty => setResult(isItQwerty ? 'WELL DONE!' : 'TYPE AGAIN!'))
17
)
18
)
19
)
20
.subscribe(e => console.log(`did you say qwerty? ${e}`));
Copied!

Additional Resources

Last modified 1yr ago