Skip to content

Instantly share code, notes, and snippets.

@P4
Created July 15, 2021 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save P4/4aa64a3b20935b8f5f1ad8eff0986889 to your computer and use it in GitHub Desktop.
Save P4/4aa64a3b20935b8f5f1ad8eff0986889 to your computer and use it in GitHub Desktop.
Konami code in 7 lines of RxJS
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