Skip to content
This repository has been archived by the owner on Dec 13, 2020. It is now read-only.

Commit

Permalink
Transform keymap to hotkey map and warn about duplicates
Browse files Browse the repository at this point in the history
Issue #1283
  • Loading branch information
pablosichert committed Nov 16, 2017
1 parent a5e80c4 commit bf1e063
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/components/Shortcuts/generateHotkeys.js
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;
};
49 changes: 49 additions & 0 deletions src/components/Shortcuts/generateHotkeys.test.js
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.

0 comments on commit bf1e063

Please sign in to comment.