Skip to content

Commit

Permalink
Fixed an issue where the Konami Code event would not fire if the user…
Browse files Browse the repository at this point in the history
… had their caps lock enabled.
  • Loading branch information
lsphillips committed Feb 7, 2018
1 parent 85a01f3 commit b76bdea
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 15 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.2.1] (2018-02-07)

### Fixed

- The `konamicode` event now fires even when the user has their caps lock enabled.

## [1.2.0] (2018-01-31)

### Changed
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name" : "ko-co",

"version" : "1.2.0",
"version" : "1.2.1",

"description" : "Adds support for the Konami Code; making your website automatically cool.",

Expand Down
20 changes: 10 additions & 10 deletions src/KoCo.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
* @memberof KoCo
*/
const KONAMI_CODE_SEQUENCE = [
'ArrowUp',
'ArrowUp',
'ArrowDown',
'ArrowDown',
'ArrowLeft',
'ArrowRight',
'ArrowLeft',
'ArrowRight',
'arrowup',
'arrowup',
'arrowdown',
'arrowdown',
'arrowleft',
'arrowright',
'arrowleft',
'arrowright',
'b',
'a'
];
Expand Down Expand Up @@ -103,7 +103,7 @@ function addSupportForTheKonamiCode ({ requireEnterPress = false, allowedTimeBet

if (requireEnterPress)
{
sequence = [...KONAMI_CODE_SEQUENCE, 'Enter'];
sequence = [...KONAMI_CODE_SEQUENCE, 'enter'];
}

function konamiCodeSequenceListener (event)
Expand All @@ -113,7 +113,7 @@ function addSupportForTheKonamiCode ({ requireEnterPress = false, allowedTimeBet
clearTimeout(timer);
}

if (sequence[progress] !== event.key)
if (sequence[progress] !== event.key.toLowerCase())
{
timer = null;
progress = 0;
Expand Down
20 changes: 18 additions & 2 deletions tests/KoCo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,28 @@ describe('KoCo', function ()
performKeyPress('ArrowLeft');
performKeyPress('ArrowRight');
performKeyPress('b');
performKeyPress('a');

// Assert.
assert.notCalled(onKonamiCode);
assert.calledWith(onKonamiCode, matchKonamiCodeEvent());
});

it('shall enable the `konamicode` event to even be emitted when the user has their caps lock enabled', function ()
{
// Setup.
removeSupportForTheKonamiCode = KoCo.addSupportForTheKonamiCode();

// Act.
performKeyPress('a');
performKeyPress('ArrowUp');
performKeyPress('ArrowUp');
performKeyPress('ArrowDown');
performKeyPress('ArrowDown');
performKeyPress('ArrowLeft');
performKeyPress('ArrowRight');
performKeyPress('ArrowLeft');
performKeyPress('ArrowRight');
performKeyPress('B');
performKeyPress('A');

// Assert.
assert.calledWith(onKonamiCode, matchKonamiCodeEvent());
Expand Down
10 changes: 9 additions & 1 deletion tests/support/performKeyPress.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
module.exports = function performKeyPress (key, element = document)
{
element.dispatchEvent(
new KeyboardEvent('keydown', { key })
new KeyboardEvent('keydown', {
key
})
);

element.dispatchEvent(
new KeyboardEvent('keyup', {
key
})
);
};

0 comments on commit b76bdea

Please sign in to comment.