Created
July 15, 2021 16:54
-
-
Save P4/4aa64a3b20935b8f5f1ad8eff0986889 to your computer and use it in GitHub Desktop.
Konami code in 7 lines of RxJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {find, fromEvent, scan} from 'rxjs'; | |
const up = "ArrowUp", down = "ArrowDown", left = "ArrowLeft", right = "ArrowRight"; | |
const sequence = [up, up, down, down, left, right, left, right, "b", "a"]; | |
fromEvent<KeyboardEvent>(document, 'keyup').pipe( | |
scan((i, {key}) => (sequence[i] == key) ? i+1 : 0, 0), | |
find(i => i == sequence.length) | |
).subscribe(() => console.log("unlocked")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment