aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/shared/settings/Key.ts22
-rw-r--r--src/shared/settings/KeySequence.ts15
-rw-r--r--test/shared/settings/Key.test.ts33
-rw-r--r--test/shared/settings/KeySequence.test.ts31
4 files changed, 86 insertions, 15 deletions
diff --git a/src/shared/settings/Key.ts b/src/shared/settings/Key.ts
index b11eeb2..3a3eb3b 100644
--- a/src/shared/settings/Key.ts
+++ b/src/shared/settings/Key.ts
@@ -1,3 +1,5 @@
+const digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
+
export default class Key {
public readonly key: string;
@@ -9,12 +11,18 @@ export default class Key {
public readonly meta: boolean;
- constructor({ key, shift, ctrl, alt, meta }: {
+ constructor({
+ key,
+ shift = false,
+ ctrl = false,
+ alt = false,
+ meta = false,
+ }: {
key: string;
- shift: boolean;
- ctrl: boolean;
- alt: boolean;
- meta: boolean;
+ shift?: boolean;
+ ctrl?: boolean;
+ alt?: boolean;
+ meta?: boolean;
}) {
this.key = key;
this.shift = shift;
@@ -51,6 +59,10 @@ export default class Key {
});
}
+ isDigit(): boolean {
+ return digits.includes(this.key);
+ }
+
equals(key: Key) {
return this.key === key.key &&
this.ctrl === key.ctrl &&
diff --git a/src/shared/settings/KeySequence.ts b/src/shared/settings/KeySequence.ts
index abae61a..de1c6bb 100644
--- a/src/shared/settings/KeySequence.ts
+++ b/src/shared/settings/KeySequence.ts
@@ -26,6 +26,21 @@ export default class KeySequence {
return true;
}
+ isDigitOnly(): boolean {
+ return this.keys.every(key => key.isDigit());
+ }
+
+ splitNumericPrefix(): [KeySequence, KeySequence] {
+ let nonDigitIndex = this.keys.findIndex(key => !key.isDigit());
+ if (nonDigitIndex === -1) {
+ return [this, new KeySequence([])];
+ }
+ return [
+ new KeySequence(this.keys.slice(0, nonDigitIndex)),
+ new KeySequence(this.keys.slice(nonDigitIndex)),
+ ];
+ }
+
static fromMapKeys(keys: string): KeySequence {
const fromMapKeysRecursive = (
remaining: string, mappedKeys: Key[],
diff --git a/test/shared/settings/Key.test.ts b/test/shared/settings/Key.test.ts
index 8222d5a..91a47f8 100644
--- a/test/shared/settings/Key.test.ts
+++ b/test/shared/settings/Key.test.ts
@@ -76,17 +76,30 @@ describe("Key", () => {
});
});
+ describe('idDigit', () => {
+ it('returns true if the key is a digit', () => {
+ expect(new Key({ key: '0' }).isDigit()).to.be.true;
+ expect(new Key({ key: '9' }).isDigit()).to.be.true;
+ expect(new Key({ key: '9', shift: true }).isDigit()).to.be.true;
+
+ expect(new Key({ key: 'a' }).isDigit()).to.be.false;
+ expect(new Key({ key: '0' }).isDigit()).to.be.false;
+ })
+ });
+
describe('equals', () => {
- expect(new Key({
- key: 'x', shift: false, ctrl: true, alt: false, meta: false,
- }).equals(new Key({
- key: 'x', shift: false, ctrl: true, alt: false, meta: false,
- }))).to.be.true;
+ it('returns true if the keys are equivalent', () => {
+ expect(new Key({
+ key: 'x', shift: false, ctrl: true, alt: false, meta: false,
+ }).equals(new Key({
+ key: 'x', shift: false, ctrl: true, alt: false, meta: false,
+ }))).to.be.true;
- expect(new Key({
- key: 'x', shift: false, ctrl: false, alt: false, meta: false,
- }).equals(new Key({
- key: 'X', shift: true, ctrl: false, alt: false, meta: false,
- }))).to.be.false;
+ expect(new Key({
+ key: 'x', shift: false, ctrl: false, alt: false, meta: false,
+ }).equals(new Key({
+ key: 'X', shift: true, ctrl: false, alt: false, meta: false,
+ }))).to.be.false;
+ })
});
});
diff --git a/test/shared/settings/KeySequence.test.ts b/test/shared/settings/KeySequence.test.ts
index 361cbd1..8a7a350 100644
--- a/test/shared/settings/KeySequence.test.ts
+++ b/test/shared/settings/KeySequence.test.ts
@@ -48,6 +48,37 @@ describe("KeySequence", () => {
})
});
+ describe('#isDigitOnly', () => {
+ it('returns true the keys are only digits', () => {
+ expect(new KeySequence([
+ new Key({ key: '4' }),
+ new Key({ key: '0' }),
+ ]).isDigitOnly()).to.be.true;
+ expect(new KeySequence([
+ new Key({ key: '4' }),
+ new Key({ key: '0' }),
+ new Key({ key: 'z' }),
+ ]).isDigitOnly()).to.be.false;
+ })
+ });
+
+ describe('#splitNumericPrefix', () => {
+ it('splits numeric prefix', () => {
+ expect(KeySequence.fromMapKeys('10gg').splitNumericPrefix()).to.deep.equal([
+ KeySequence.fromMapKeys('10'),
+ KeySequence.fromMapKeys('gg'),
+ ]);
+ expect(KeySequence.fromMapKeys('10').splitNumericPrefix()).to.deep.equal([
+ KeySequence.fromMapKeys('10'),
+ new KeySequence([]),
+ ]);
+ expect(KeySequence.fromMapKeys('gg').splitNumericPrefix()).to.deep.equal([
+ new KeySequence([]),
+ KeySequence.fromMapKeys('gg'),
+ ]);
+ });
+ });
+
describe('#fromMapKeys', () => {
it('returns mapped keys for Shift+Esc', () => {
let keys = KeySequence.fromMapKeys('<S-Esc>').keys;