Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UMD - Support for AMD and globals #29

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -90,7 +102,7 @@ var codes = exports.code = exports.codes = {

// Helper aliases

var aliases = exports.aliases = {
var aliases = keycode.aliases = {
'windows': 91,
'⇧': 16,
'⌥': 18,
Expand Down Expand Up @@ -121,7 +133,7 @@ var aliases = exports.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
Expand All @@ -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
Expand All @@ -144,3 +156,7 @@ for (i in codes) names[codes[i]] = i
for (var alias in aliases) {
codes[alias] = aliases[alias]
}

return keycode;

}));