From 6fc6b71f44380e02909cb17b0cb2a2cbf8c3202c Mon Sep 17 00:00:00 2001 From: Dominic Farolino Date: Thu, 6 Jun 2024 16:25:37 +0200 Subject: [PATCH] Clean up Konami code example --- README.md | 58 +++++++++++++++++++++++++++---------------------------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 0dd3ca6..d68245e 100644 --- a/README.md +++ b/README.md @@ -214,36 +214,33 @@ keys the user might hit while using an app: ```js const pattern = [ - 'ArrowUp', - 'ArrowUp', - 'ArrowDown', - 'ArrowDown', - 'ArrowLeft', - 'ArrowRight', - 'ArrowLeft', - 'ArrowRight', - 'b', - 'a', - 'b', - 'a', - 'Enter', + 'ArrowUp', + 'ArrowUp', + 'ArrowDown', + 'ArrowDown', + 'ArrowLeft', + 'ArrowRight', + 'ArrowLeft', + 'ArrowRight', + 'b', + 'a', + 'b', + 'a', + 'Enter', ]; -const keys = document.on('keydown').map((e) => e.key); +const keys = document.on('keydown').map(e => e.key); + keys - .flatMap((firstKey) => { - if (firstKey === pattern[0]) { - return keys - .take(pattern.length - 1) - .every((k, i) => k === pattern[i + 1]); - } - }) - .filter((matched) => matched) - .subscribe({ - next: (_) => { - console.log('Secret code matched!'); - }, - }); + .flatMap(firstKey => { + if (firstKey === pattern[0]) { + return keys + .take(pattern.length - 1) + .every((k, i) => k === pattern[i + 1]); + } + }) + .filter(matched => matched) + .subscribe(() => console.log('Secret code matched!')); ```
@@ -265,10 +262,11 @@ document.addEventListener('keydown', e => { console.log('Secret code matched!'); document.removeEventListener('keydown', handler) } - } - document.addEventListener('keydown', handler) + }; + + document.addEventListener('keydown', handler); } -}) +}, {once: true}); ```