diff --git a/lib/konami.js b/lib/konami.js index e58b20a..3f7c5d0 100644 --- a/lib/konami.js +++ b/lib/konami.js @@ -1,51 +1,35 @@ - var Konami = {}; (function() { - /** - * Creates an event handler responding to the specified sequence. - * @param {number[]} keySequence - * @param {function()} actionCallback - * @return {function(Event)} - */ - var sequence = function(sequence, action) { + var afterSequence = function(sequence, action) { var index = 0; - /** - * Event handler - * @param {Event} event - */ - return function(e) { - var pressedKey = e.keyCode || e.which; - - var matchedKey = pressedKey === sequence[index]; + return function(event) { + var matchedKey = event.keyCode === sequence[index]; // if it didn't match, reset and try matching against the first key if (!matchedKey) { index = 0; - matchedKey = pressedKey === sequence[index]; + matchedKey = event.keyCode === sequence[index]; } - + if (matchedKey) { index += 1; + if (index === sequence.length) { - // fire action - action(); - + // reset when sequence completed index = 0; + + // fire action + action(); } } - + }; }; - /** - * Creates an event handler responding to the Konami Code. - * @param {function()} action completed sequence callback - * @return {function(Event)} - */ Konami.code = function(action) { - return sequence([38, 38, 40, 40, 37, 39, 37, 39, 66, 65], action); + return afterSequence([38, 38, 40, 40, 37, 39, 37, 39, 66, 65], action); }; }());