Skip to content

Commit

Permalink
Updated the NPM key. and moved the documentation to the Typescript de…
Browse files Browse the repository at this point in the history
…finition file.
  • Loading branch information
lsphillips committed Dec 6, 2018
1 parent 6dcc4e8 commit 9036cf3
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 96 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ deploy:
tags: true
node: "10"
api_key:
secure: "w8s9xvbD9E91iy1wobVHy86C9szYhvuEGtkcaHXUfgyEg5ojzbeAD/BWoZD7qeEu6KTpZNOT18RICTsNe6HRZreXfhCbt19a0uNdotWWivM5E7eLBOtbIL9KF6+9chjwgSHUEvKqEFetWEzbpPOsDsyG6lZI+gd9BphGjMlmcCu+2khE7ywFcu8uH4ziKw2F7v9KPH8Bq/86mMzsoR6MM45zcPTg19ZH2bWNZSqnAwk12FO3v82YPgHQwX9g8mlAxZ63LHMKKy+S5y4ktytumG5xZ0F6hW6f/voBjAV7a2QJUAQRRPuP0G79qGQXRwyCvujbH4X05hbKIMN/bjNULTiwtvL6o7q/7WdtWrnzbBeszH5d5o38kn37o+OuTWQCXvqL7LJILsS/MkAnLZWkuNen8E1w+ZMnVO/bEnImslEmDFwsKZlIj2xMseMcw6oWj1sJbey0fqXseIerd2/F2/+95eWlLxjqlWBZ7+/rxaC0pL7lGp9FoGTj32ZLjmQx6N4ZnIvdwfxATXuhojPRuP86PgqddpbZHrxsNzcaUL+pntPMMeuWcEKjWKfWVB3JfHCV10duYeatGDkqFJRMuskOySybcOa4BzD/U018ojpmyxjCTqtFL1GRjmYwpv/W0KIAIHDCx8h5UH+hhg15lFl5HNt8s0rz4/Qao58W5KA="
secure: "JNxuPRe8dC7tg1nGv1bFooWVOfBrooojP79Yu5o18nhuErq7s31BaQWe6SnQiXVqyMEr64ZpZlpPO8UD1Kqb63I+KpVMZOcQ3+e650ahWhv+sD8Y5GrCr+Ru9KmHfJstWPY1prRRcjwE+Db7ve7AUX193qTm5g11Gr/a1NIs+O+8vPU/U5P1V8D366/cnYNKduacqHziJRN7+SYPe23Zbpu3k7Q/rIYJoWKQe1PoG7Na5/4ZQktEego+JCJYT6kUraCYUtFfmfEhZx5p3mxu/w2OGz0bMFrJ4l8tq3jZwCKXP/QDbAqfTzgfXmbO7YvDY0O2k616ASXNKX45oFmuW9EDvv45MnjvS5cJMaIm47jukDPFIe1GonhvYdiPJNhVDjc+Y4Lu40cO+YiKQj4B03pV2JO+nBXLXu986E2laHCs3GFzxBoAaNbm1LgmDE+WeDR0ASK1lHU6aXqQzX4rG6R7R0w1ynrc18JL2kwgDmd0zY4lRkODtrSRTqWTMwWa+1wdfcKxwnWXNxxEoybqd1yNPTzQlTdtsQGaF4mP4UDZX+uWVvtstecTExo/YwjQxj4nnFyWFDuvFa1xmKE6qand2Czj7nuVpMMbhpFJCnCEY0Rzpd++e+eMPTG8bPz8t2SNlAcGLzyys0bQ/5WbdZ/aQUe2WBlIrG1f+4l8vRk="
email: "lsphillips.mail@gmail.com"
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.3.0] (2018-12-06)

### Added

- Introduced documentation to the Typescript type definitons.

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

### Fixed
Expand Down
48 changes: 44 additions & 4 deletions KoCo.d.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,56 @@
export interface KonamiCodeOptions
/**
* Options that define how a Konami Code sequence is detected.
*/
export interface KonamiCodeDetectionOptions
{
/**
* Determines whether the enter key is required to conclude a Konami Code sequence.
*/
requireEnterPress? : boolean;

/**
* The maximum amount of time (in milliseconds) to wait between key presses before sequence progress is reset.
*/
allowedTimeBetweenKeys? : number;
}

// --------------------------------------------------------
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/**
* A synchronous function that will remove support for the Konami Code.
*/
export interface KonamiCodeSupportRemover
{
() : void
}

// --------------------------------------------------------
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -

export function addSupportForTheKonamiCode(options? : KonamiCodeOptions) : KonamiCodeSupportRemover;
/**
* Adds support for the Konami Code.
*
* Example usage:
*
* ``` js
* addSupportForTheKonamiCode(
* {
* allowedTimeBetweenKeys : 500
* });
* ```
*
* Once support is added, all elements will now fire a `konamicode` event whenever the user enters the Konami Code sequence:
*
* ``` js
* target.addEventListener('konamicode', () =>
* {
* console.log('The Konami Code has been entered. 30 more lives for you!');
* });
* ```
*
* **Note:** The `konamicode` event bubbles and is cancelable.
*
* @param options Options that define how a Konami Code sequence is detected. By default, the enter key is not required to conclude the Konami Code sequence and it will wait indefinitely between key presses.
*
* @returns A synchronous function that can be called to remove support for the Konami Code.
*/
export function addSupportForTheKonamiCode(options? : KonamiCodeDetectionOptions) : KonamiCodeSupportRemover;
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ KoCo.addSupportForTheKonamiCode(

You can pass some options to control how the Konami Code sequence is detected:

- `requireEnterPress` - Determines whether the user needs to press enter before the Konami Code is triggered. Defaults to `false`.
- `allowedTimeBetweenKeys` - Determines the maximum allowed time (in milliseconds) between key presses before sequence progress is reset. By default the user can take as long as they want.
- `requireEnterPress` - Determines whether the enter key is required to conclude a Konami Code sequence. Defaults to `false`.
- `allowedTimeBetweenKeys` - The maximum amount of time (in milliseconds) to wait between key presses before sequence progress is reset. By default the user can take as long as they want.

### Listening

Listening for the Konami Code is as simple as listening for native DOM events:
Listening for the Konami Code is just like listening to regular DOM events:

``` js
anEventTarget.addEventListener('konamicode', function ()
target.addEventListener('konamicode', () =>
{
console.log('The Konami Code has been entered. 30 more lives for you!');
});
```

The `konamicode` event bubbles, so event delegation works as expected.
The `konamicode` event bubbles and is cancelable, so event delegation works as expected.

### Restoring

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.1",
"version" : "1.3.0",

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

Expand Down
76 changes: 10 additions & 66 deletions src/KoCo.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
'use strict';

/**
* The legendary Konami Code sequence.
*
* @private
*
* @static
*
* @final
*
* @type {Array.<String>}
*
* @memberof KoCo
*/
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -

const KONAMI_CODE_SEQUENCE = [
'arrowup',
'arrowup',
Expand All @@ -26,21 +15,8 @@ const KONAMI_CODE_SEQUENCE = [
'a'
];

// --------------------------------------------------------

/**
* Triggers a custom event on the document that bubbles.
*
* @private
*
* @static
*
* @param {EventTarget} target The target that will dispatch the event.
* @param {String} type The event type.
* @param {Object} [detail] The data that will be assigned to the `event.detail` property.
*
* @memberof KoCo
*/
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function triggerEvent (target, type, detail = {})
{
let event, bubbles = true, cancelable = true;
Expand All @@ -62,42 +38,11 @@ function triggerEvent (target, type, detail = {})
target.dispatchEvent(event);
}

// --------------------------------------------------------

/**
* Adds support for the Konami Code by allowing elements to emit a `konamicode` event when the user enters the `↑ ↑ ↓ ↓ ← → ← → B A` sequence.
*
* This will likely be called in your application entry point, for example:
*
* ```
* KoCo.addSupportForTheKonamiCode(
* {
* allowedTimeBetweenKeys : 500
* });
* ```
*
* Elsewhere, you can then listen for the Konami Code:
*
* ```
* anEventTarget.addEventListener('konamicode', function ()
* {
* console.log('The Konami Code has been entered. 30 more lives for you!');
* });
* ```
*
* The `konamicode` event bubbles, so event delegation works as expected.
*
* @static
*
* @param {Object} [options] Some options to control how the Konami Code sequence is detected.
* @param {Number} [options.allowedTimeBetweenKeys] Determines the maximum allowed time (in milliseconds) between key presses before sequence progress is reset. By default the user can take as long as they want.
* @param {Boolean} [options.requireEnterPress = false] Determines whether the user needs to press enter before the Konami Code is triggered.
*
* @returns {Function} A function that will remove support for the Konami Code.
*
* @memberof KoCo
*/
function addSupportForTheKonamiCode ({ requireEnterPress = false, allowedTimeBetweenKeys = 0 } = {})
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function addSupportForTheKonamiCode ({
requireEnterPress = false, allowedTimeBetweenKeys = 0
} = {})
{
let sequence = KONAMI_CODE_SEQUENCE, timer = null, progress = 0;

Expand Down Expand Up @@ -145,7 +90,6 @@ function addSupportForTheKonamiCode ({ requireEnterPress = false, allowedTimeBet
}
}

// Listen.
document.addEventListener('keydown', konamiCodeSequenceListener, true);

return function removeSupportForTheKonamiCode ()
Expand All @@ -154,6 +98,6 @@ function addSupportForTheKonamiCode ({ requireEnterPress = false, allowedTimeBet
};
}

// --------------------------------------------------------
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -

module.exports = { addSupportForTheKonamiCode };
19 changes: 5 additions & 14 deletions tests/KoCo.test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
'use strict';

// Dependencies
// --------------------------------------------------------

const { assert, spy } = require('sinon');
const delay = require('timeout-as-promise');

// Helpers & Matchers
// --------------------------------------------------------
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -

const { assert, spy } = require('sinon');
const delay = require('timeout-as-promise');
const performKeyPress = require('./support/performKeyPress');
const matchKonamiCodeEvent = require('./matchers/matchKonamiCodeEvent');
const KoCo = require('../src/KoCo');

// Subjects
// --------------------------------------------------------

const KoCo = require('../src/KoCo');

// --------------------------------------------------------
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -

describe('KoCo', function ()
{
Expand Down
5 changes: 2 additions & 3 deletions tests/matchers/matchKonamiCodeEvent.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict';

// Dependencies
// --------------------------------------------------------
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -

const { match } = require('sinon');

// --------------------------------------------------------
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -

module.exports = function matchKonamiCodeEvent ({ dispatchBy = document } = {})
{
Expand Down
2 changes: 1 addition & 1 deletion tests/support/performKeyPress.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

// --------------------------------------------------------
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -

module.exports = function performKeyPress (key, element = document)
{
Expand Down

0 comments on commit 9036cf3

Please sign in to comment.