This repository has been archived by the owner on Dec 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Transform keymap to hotkey map and warn about duplicates
Issue #1283
- Loading branch information
1 parent
a5e80c4
commit bf1e063
Showing
3 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export default keymap => { | ||
const hotkeys = {}; | ||
|
||
for (const hotkey of Object.values(keymap)) { | ||
if (hotkeys[hotkey]) { | ||
// eslint-disable-next-line max-len | ||
console.warn(`There exist multiple definition for key combination "${hotkey}"`); | ||
} | ||
|
||
const bucket = []; | ||
hotkeys[hotkey] = bucket; | ||
} | ||
|
||
return hotkeys; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* eslint-env mocha */ | ||
import chai, { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
|
||
chai.use(sinonChai); | ||
|
||
import generateHotkeys from './generateHotkeys'; | ||
|
||
describe('generateHotkeys', () => { | ||
it('should return an object', () => { | ||
const hotkeys = generateHotkeys({}); | ||
|
||
expect(hotkeys).to.be.an('object'); | ||
}); | ||
|
||
it('should transform a key map to a map of hotkeys', () => { | ||
const keymap = { | ||
COMBO_A: 'ctrl+a', | ||
COMBO_B: 'ctrl+b' | ||
}; | ||
|
||
const hotkeys = generateHotkeys(keymap); | ||
|
||
expect(hotkeys).to.deep.equal({ | ||
'ctrl+a': [], | ||
'ctrl+b': [] | ||
}); | ||
}); | ||
|
||
it('should warn about duplicate keys', () => { | ||
const warn = sinon.spy(console, 'warn'); | ||
|
||
try { | ||
const keymap = { | ||
COMBO_1: 'ctrl+a', | ||
COMBO_2: 'ctrl+a' | ||
}; | ||
|
||
generateHotkeys(keymap); | ||
|
||
expect(warn).to.have.been.called; | ||
} catch (error) { | ||
throw error; | ||
} finally { | ||
warn.restore(); | ||
} | ||
}); | ||
}); |
File renamed without changes.