Last active
June 8, 2017 15:41
-
-
Save brandonaaskov/58c0c65a07a52e5632b7b339fec1ef55 to your computer and use it in GitHub Desktop.
A very straightforward example of leveraging the konami code (uses isEqual from lodash because it reads easier)
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
const konamiCode = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65, 13] | |
let konamiTracker = [] | |
const listener = event => { | |
const key = event.keyCode | |
const currentSequence = [...konamiTracker, key] | |
const slice = konamiCode.slice(0, currentSequence.length) | |
const isPartialMatch = _.isEqual(currentSequence, slice) | |
const isExactMatch = _.isEqual(currentSequence, konamiCode) | |
konamiTracker = isPartialMatch ? konamiTracker.concat([key]) : [] | |
if (isExactMatch) console.log('execute the thing') | |
} | |
window.konami = window.addEventListener('keydown', listener) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment