├── .gitignore ├── .jshintrc ├── .travis.yml ├── LICENSE ├── README.md ├── index.d.ts ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "undef": true, 3 | "unused": "vars", 4 | "browser": true, 5 | "node": true, 6 | "eqnull": true, 7 | "esnext": true, 8 | "indent": 4 9 | } 10 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 'stable' 4 | sudo: false 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Thomas Brouard 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # electron-is-accelerator 2 | 3 | Check if a string is a valid [Electron accelerator](https://github.com/electron/electron/blob/master/docs/api/accelerator.md) and return a boolean. This module can be used to validate user defined accelerators before using them with [MenuItems](http://electron.atom.io/docs/api/menu-item/). 4 | 5 | ## Installation 6 | 7 | ``` 8 | $ npm install --save electron-is-accelerator 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```javascript 14 | var isAccelerator = require("electron-is-accelerator"); 15 | 16 | isAccelerator("CommandOrControl+Shift+Z"); // true 17 | isAccelerator("CommandOrControl+F9"); // true 18 | isAccelerator("CommandOrContrl+F9"); // false 19 | isAccelerator("A+Z"); // false 20 | ``` 21 | 22 | ## License 23 | 24 | The MIT License (MIT) 25 | Copyright (c) 2016 Thomas Brouard 26 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Detects if `maybeAccelerator` is a valid Electron Accelerator. 3 | */ 4 | declare function isAccelerator(maybeAccelerator: string): boolean; 5 | 6 | export = isAccelerator; 7 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const modifiers = /^(Command|Cmd|Control|Ctrl|CommandOrControl|CmdOrCtrl|Alt|Option|AltGr|Shift|Super)$/; 4 | const keyCodes = /^([0-9A-Z)!@#$%^&*(:+<_>?~{|}";=,\-./`[\\\]']|F1*[1-9]|F10|F2[0-4]|Plus|Space|Tab|Backspace|Delete|Insert|Return|Enter|Up|Down|Left|Right|Home|End|PageUp|PageDown|Escape|Esc|VolumeUp|VolumeDown|VolumeMute|MediaNextTrack|MediaPreviousTrack|MediaStop|MediaPlayPause|PrintScreen)$/; 5 | 6 | module.exports = function (str) { 7 | let parts = str.split("+"); 8 | let keyFound = false; 9 | return parts.every((val, index) => { 10 | const isKey = keyCodes.test(val); 11 | const isModifier = modifiers.test(val); 12 | if (isKey) { 13 | // Key must be unique 14 | if (keyFound) return false; 15 | keyFound = true; 16 | } 17 | // Key is required 18 | if (index === parts.length - 1 && !keyFound) return false; 19 | return isKey || isModifier; 20 | }); 21 | }; 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "electron-is-accelerator", 3 | "version": "0.2.0", 4 | "description": "Check if a string is a valid Electron accelerator", 5 | "main": "index.js", 6 | "types": "index.d.ts", 7 | "scripts": { 8 | "test": "jshint *.js && ava" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/brrd/electron-is-accelerator.git" 13 | }, 14 | "keywords": [ 15 | "electron", 16 | "accelerator" 17 | ], 18 | "author": "brrd", 19 | "license": "MIT", 20 | "bugs": { 21 | "url": "https://github.com/brrd/electron-is-accelerator/issues" 22 | }, 23 | "homepage": "https://github.com/brrd/electron-is-accelerator", 24 | "devDependencies": { 25 | "ava": "^0.17.0", 26 | "jshint": "^2.9.4" 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import isAccelerator from '.'; 3 | 4 | const keys = [ 5 | '0', 6 | '1', 7 | '9', 8 | 'A', 9 | 'G', 10 | 'Z', 11 | 'F1', 12 | 'F5', 13 | 'F24', 14 | '~', 15 | '!', 16 | '@', 17 | '#', 18 | 'Plus', 19 | 'Space', 20 | 'Tab', 21 | 'Backspace', 22 | 'Delete', 23 | 'Insert', 24 | 'Return', 25 | 'Enter', 26 | 'Up', 27 | 'Down', 28 | 'Left', 29 | 'Right', 30 | 'Home', 31 | 'End', 32 | 'PageUp', 33 | 'PageDown', 34 | 'Escape', 35 | 'Esc', 36 | 'VolumeUp', 37 | 'VolumeDown', 38 | 'VolumeMute', 39 | 'MediaNextTrack', 40 | 'MediaPreviousTrack', 41 | 'MediaStop', 42 | 'MediaPlayPause', 43 | 'PrintScreen' 44 | ]; 45 | 46 | const modifiers = [ 47 | 'Command', 48 | 'Cmd', 49 | 'Control', 50 | 'Ctrl', 51 | 'CommandOrControl', 52 | 'CmdOrCtrl', 53 | 'Alt', 54 | 'Option', 55 | 'AltGr', 56 | 'Shift', 57 | 'Super' 58 | ]; 59 | 60 | test('multiple modifier', t => t.true(isAccelerator('CommandOrControl+Shift+Z'))); 61 | test('multiple keys', t => t.false(isAccelerator('A+Z'))); 62 | test('typos', t => t.false(isAccelerator('CommandOrContol+A'))); 63 | test('modifiers without keys', t => t.false(isAccelerator('CmdOrCtrl'))); 64 | test('multiple modifiers without keys', t => t.false(isAccelerator('CmdOrCtrl+Ctrl'))); 65 | test('empty string', t => t.false(isAccelerator(''))); 66 | 67 | // tests to check each modifiers 68 | modifiers.forEach(mod => test( 69 | mod + ' modifier', 70 | t => t.true( 71 | isAccelerator(mod + '+A') 72 | ) 73 | )); 74 | 75 | // tests to check all keys 76 | keys.forEach(key => test( 77 | key + ' key', 78 | t => t.true( 79 | isAccelerator(key) 80 | ) 81 | )); 82 | 83 | // tests to check every combination of modifier and key 84 | const testSequence = (sequence) => test( 85 | `${sequence} sequence`, 86 | t => t.true( 87 | isAccelerator(`${sequence}`) 88 | ) 89 | ); 90 | 91 | modifiers.forEach(mod => 92 | keys.forEach(key => { 93 | testSequence(`${mod}+${key}`); 94 | testSequence(`${key}+${mod}`); 95 | }) 96 | ); 97 | --------------------------------------------------------------------------------