( 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
( 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}`));
- sequenceEqual 📰 - Official docs
📁 Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/sequenceEqual.ts