Comparing version 2.1.9 to 2.2.0
@@ -5,3 +5,3 @@ { | ||
"description": "Map of keyboard codes", | ||
"version": "2.1.9", | ||
"version": "2.2.0", | ||
"development": { | ||
@@ -8,0 +8,0 @@ "timoxley/assert": "*" |
@@ -125,2 +125,3 @@ export = keycode; | ||
'spc': number; | ||
'spacebar': number; | ||
'pgup': number; | ||
@@ -144,2 +145,3 @@ 'pgdn': number; | ||
names: InverseCodesMap; | ||
isEventKey: (event: Event, nameOrCode: number | string) => boolean; | ||
} |
33
index.js
@@ -12,3 +12,3 @@ // Source: http://jsfiddle.net/vWx8V/ | ||
exports = module.exports = function(searchInput) { | ||
function keyCode(searchInput) { | ||
// Keyboard Events | ||
@@ -41,2 +41,31 @@ if (searchInput && 'object' === typeof searchInput) { | ||
/** | ||
* Compares a keyboard event with a given keyCode or keyName. | ||
* | ||
* @param {Event} event Keyboard event that should be tested | ||
* @param {Mixed} keyCode {Number} or keyName {String} | ||
* @return {Boolean} | ||
* @api public | ||
*/ | ||
keyCode.isEventKey = function isEventKey(event, nameOrCode) { | ||
if (event && 'object' === typeof event) { | ||
var keyCode = event.which || event.keyCode || event.charCode | ||
if (keyCode === null || keyCode === undefined) { return false; } | ||
if (typeof nameOrCode === 'string') { | ||
// check codes | ||
var foundNamedKey = codes[nameOrCode.toLowerCase()] | ||
if (foundNamedKey) { return foundNamedKey === keyCode; } | ||
// check aliases | ||
var foundNamedKey = aliases[nameOrCode.toLowerCase()] | ||
if (foundNamedKey) { return foundNamedKey === keyCode; } | ||
} else if (typeof nameOrCode === 'number') { | ||
return nameOrCode === keyCode; | ||
} | ||
return false; | ||
} | ||
} | ||
exports = module.exports = keyCode; | ||
/** | ||
* Get by name | ||
@@ -110,2 +139,3 @@ * | ||
'spc': 32, | ||
'spacebar': 32, | ||
'pgup': 33, | ||
@@ -118,3 +148,2 @@ 'pgdn': 34, | ||
/*! | ||
@@ -121,0 +150,0 @@ * Programatically add the following |
{ | ||
"name": "keycode", | ||
"description": "Convert between keyboard keycodes and keynames and vice versa.", | ||
"version": "2.1.9", | ||
"version": "2.2.0", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "directories": { |
@@ -84,2 +84,17 @@ # keycode | ||
## keycode.isEventKey(event: Event, nameOrCode: String | Number) | ||
Tests if an keyboard event against a given name or keycode. | ||
Will return `true` if the event matches the given name or keycode, `false` otherwise. | ||
```js | ||
// assume event is an keydown event with key 'enter' | ||
keycode.isEventKey(event, 'enter') // => true | ||
keycode.isEventKey(event, 'down') // => false | ||
keycode.isEventKey(event, 13) // => true | ||
keycode.isEventKey(event, 40) // => false | ||
``` | ||
## Maps | ||
@@ -86,0 +101,0 @@ |
@@ -37,2 +37,3 @@ "use strict" | ||
assert.strictEqual(keycode('enTeR'), 13); | ||
assert.strictEqual(keycode('Spacebar'), 32); | ||
}) | ||
@@ -100,1 +101,36 @@ | ||
}) | ||
describe('isEventKey', function() { | ||
it('should allow to compare events to their names', function() { | ||
var event = { which: 13, keyCode: 13, charCode: 13 }; | ||
assert.strictEqual(keycode.isEventKey(event, 'enter'), true); | ||
assert.strictEqual(keycode.isEventKey(event, 'down'), false); | ||
}); | ||
it('should allow to compare events to their names (case insensitive)', function() { | ||
var event = { which: 13, keyCode: 13, charCode: 13 }; | ||
assert.strictEqual(keycode.isEventKey(event, 'eNtER'), true); | ||
assert.strictEqual(keycode.isEventKey(event, 'dOWN'), false); | ||
}); | ||
it('should return false if a ', function() { | ||
var event = { which: 13, keyCode: 13, charCode: 13 }; | ||
assert.strictEqual(keycode.isEventKey(event, 'eNtER'), true); | ||
assert.strictEqual(keycode.isEventKey(event, 'dOWN'), false); | ||
}); | ||
it('should allow to compare events to their keyCodes)', function() { | ||
var event = { which: 13, keyCode: 13, charCode: 13 }; | ||
assert.strictEqual(keycode.isEventKey(event, 13), true); | ||
assert.strictEqual(keycode.isEventKey(event, 14), false); | ||
}); | ||
it('should not break when invalid key codes are entered, instead false should be returned', function() { | ||
var event = { which: -1, keyCode: -1, charCode: -1 }; | ||
assert.strictEqual(keycode.isEventKey(event, 'enter'), false); | ||
assert.strictEqual(keycode.isEventKey(event, 'down'), false); | ||
}); | ||
}); | ||
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
117726
4578
137