From aca6b22bf73bb91fb6747d7c61c95a94c11e129f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bharath=20=E3=83=84?= Date: Thu, 18 Aug 2016 11:39:01 +0530 Subject: [PATCH 1/2] #24 making the library UMD - support for AMD and global --- index.js | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index b842451..917845d 100644 --- a/index.js +++ b/index.js @@ -9,7 +9,19 @@ * @api public */ -exports = module.exports = function(searchInput) { +(function (root, factory) { + if(typeof define === "function" && define.amd) { + // the AMD environment. + root.keycode = factory() + } else if(typeof module === "object" && module.exports) { + // CommonJS environment + exports = module.exports = (root.keycode = factory()); + } else { + root.keycode = factory(); + } +}(this, function() { + +var keycode = function(searchInput) { // Keyboard Events if (searchInput && 'object' === typeof searchInput) { var hasKeyCode = searchInput.which || searchInput.keyCode || searchInput.charCode @@ -39,10 +51,10 @@ exports = module.exports = function(searchInput) { /** * Get by name * - * exports.code['enter'] // => 13 + * keycode.code['enter'] // => 13 */ -var codes = exports.code = exports.codes = { +var codes = keycode.code = keycode.codes = { 'backspace': 8, 'tab': 9, 'enter': 13, @@ -90,7 +102,7 @@ var codes = exports.code = exports.codes = { // Helper aliases -var aliases = exports.aliases = { +var aliases = keycode.aliases = { 'windows': 91, '⇧': 16, '⌥': 18, @@ -132,10 +144,10 @@ for (i = 0; i < 10; i++) codes['numpad '+i] = i + 96 /** * Get by code * - * exports.name[13] // => 'Enter' + * keycode.name[13] // => 'Enter' */ -var names = exports.names = exports.title = {} // title for backward compat +var names = keycode.names = keycode.title = {} // title for backward compat // Create reverse mapping for (i in codes) names[codes[i]] = i @@ -144,3 +156,7 @@ for (i in codes) names[codes[i]] = i for (var alias in aliases) { codes[alias] = aliases[alias] } + +return keycode; + +})); From d58634044d23d0d945006f0123882fcf602ef4f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bharath=20=E3=83=84?= Date: Thu, 18 Aug 2016 11:39:51 +0530 Subject: [PATCH 2/2] adding numbers the same way as numpad keys --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 917845d..81194bc 100644 --- a/index.js +++ b/index.js @@ -133,7 +133,7 @@ var aliases = keycode.aliases = { for (i = 97; i < 123; i++) codes[String.fromCharCode(i)] = i - 32 // numbers -for (var i = 48; i < 58; i++) codes[i - 48] = i +for (var i = 0; i < 10; i++) codes[i] = i + 48 // function keys for (i = 1; i < 13; i++) codes['f'+i] = i + 111