aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2019-12-10 21:45:16 +0900
committerShin'ya Ueoka <ueokande@i-beam.org>2019-12-13 22:09:11 +0900
commit38a69e4ca319f9db3c54a5cb69cd5645f12369d5 (patch)
treee4a4279cb4c30df68e3c292154e2152dc9531815
parentc399b992e6665f194ebe08a48aae1ede32c6767f (diff)
Add prefix functions in KeySequence
-rw-r--r--src/content/domains/KeySequence.ts (renamed from src/shared/settings/KeySequence.ts)24
-rw-r--r--src/content/repositories/KeymapRepository.ts2
-rw-r--r--src/content/usecases/KeymapUseCase.ts2
-rw-r--r--test/content/domains/KeySequence.test.ts (renamed from test/shared/settings/KeySequence.test.ts)65
4 files changed, 89 insertions, 4 deletions
diff --git a/src/shared/settings/KeySequence.ts b/src/content/domains/KeySequence.ts
index de1c6bb..4534b60 100644
--- a/src/shared/settings/KeySequence.ts
+++ b/src/content/domains/KeySequence.ts
@@ -1,4 +1,4 @@
-import Key from './Key';
+import Key from '../../shared/settings/Key';
export default class KeySequence {
constructor(
@@ -30,6 +30,28 @@ export default class KeySequence {
return this.keys.every(key => key.isDigit());
}
+ repeatCount(): number {
+ let nonDigitAt = this.keys.findIndex(key => !key.isDigit());
+ if (this.keys.length === 0 || nonDigitAt === 0) {
+ return 1;
+ }
+ if (nonDigitAt === -1) {
+ nonDigitAt = this.keys.length;
+ }
+ let digits = this.keys.slice(0, nonDigitAt)
+ .map(key => key.key)
+ .join('');
+ return Number(digits);
+ }
+
+ trimNumericPrefix(): KeySequence {
+ let nonDigitAt = this.keys.findIndex(key => !key.isDigit());
+ if (nonDigitAt === -1) {
+ nonDigitAt = this.keys.length;
+ }
+ return new KeySequence(this.keys.slice(nonDigitAt));
+ }
+
splitNumericPrefix(): [KeySequence, KeySequence] {
let nonDigitIndex = this.keys.findIndex(key => !key.isDigit());
if (nonDigitIndex === -1) {
diff --git a/src/content/repositories/KeymapRepository.ts b/src/content/repositories/KeymapRepository.ts
index 3391229..2944723 100644
--- a/src/content/repositories/KeymapRepository.ts
+++ b/src/content/repositories/KeymapRepository.ts
@@ -1,5 +1,5 @@
import Key from '../../shared/settings/Key';
-import KeySequence from '../../shared/settings/KeySequence';
+import KeySequence from '../domains/KeySequence';
export default interface KeymapRepository {
enqueueKey(key: Key): KeySequence;
diff --git a/src/content/usecases/KeymapUseCase.ts b/src/content/usecases/KeymapUseCase.ts
index 67d667d..1707f6f 100644
--- a/src/content/usecases/KeymapUseCase.ts
+++ b/src/content/usecases/KeymapUseCase.ts
@@ -5,7 +5,7 @@ import AddonEnabledRepository from '../repositories/AddonEnabledRepository';
import * as operations from '../../shared/operations';
import Keymaps from '../../shared/settings/Keymaps';
import Key from '../../shared/settings/Key';
-import KeySequence from '../../shared/settings/KeySequence';
+import KeySequence from '../domains/KeySequence';
import AddressRepository from '../repositories/AddressRepository';
type KeymapEntityMap = Map<KeySequence, operations.Operation>;
diff --git a/test/shared/settings/KeySequence.test.ts b/test/content/domains/KeySequence.test.ts
index 8a7a350..bc16189 100644
--- a/test/shared/settings/KeySequence.test.ts
+++ b/test/content/domains/KeySequence.test.ts
@@ -1,4 +1,4 @@
-import KeySequence from '../../../src/shared/settings/KeySequence';
+import KeySequence from '../../../src/content/domains/KeySequence';
import { expect } from 'chai'
import Key from "../../../src/shared/settings/Key";
@@ -62,6 +62,69 @@ describe("KeySequence", () => {
})
});
+ describe('#repeatCount', () => {
+ it('returns repeat count with a numeric prefix', () => {
+ let seq = new KeySequence([
+ new Key({ key: '1' }), new Key({ key: '0' }) ,
+ new Key({ key: 'g' }), new Key({ key: 'g' }) ,
+ ]);
+ expect(seq.repeatCount()).to.equal(10);
+
+ seq = new KeySequence([
+ new Key({ key: '0' }), new Key({ key: '5' }) ,
+ new Key({ key: 'g' }), new Key({ key: 'g' }) ,
+ ]);
+ expect(seq.repeatCount()).to.equal(5);
+ });
+
+ it('returns 1 if no numeric prefix', () => {
+ let seq = new KeySequence([
+ new Key({ key: 'g' }), new Key({ key: 'g' }) ,
+ ]);
+ expect(seq.repeatCount()).to.equal(1);
+
+ seq = new KeySequence([]);
+ expect(seq.repeatCount()).to.equal(1);
+ });
+
+ it('returns whole keys if digits only sequence', () => {
+ let seq = new KeySequence([
+ new Key({ key: '1' }), new Key({ key: '0' }) ,
+ ]);
+ expect(seq.repeatCount()).to.equal(10);
+
+ seq = new KeySequence([
+ new Key({ key: '0' }), new Key({ key: '5' }) ,
+ ]);
+ expect(seq.repeatCount()).to.equal(5);
+ });
+ });
+
+ describe('#trimNumericPrefix', () => {
+ it('removes numeric prefix', () => {
+ let seq = new KeySequence([
+ new Key({ key: '1' }), new Key({ key: '0' }) ,
+ new Key({ key: 'g' }), new Key({ key: 'g' }) , new Key({ key: '3' }) ,
+ ]).trimNumericPrefix();
+ expect(seq.keys.map(key => key.key)).to.deep.equal(['g', 'g', '3']);
+ });
+
+ it('returns empty if keys contains only digis', () => {
+ let seq = new KeySequence([
+ new Key({ key: '1' }), new Key({ key: '0' }) ,
+ ]).trimNumericPrefix();
+ expect(seq.trimNumericPrefix().keys).to.be.empty;
+ });
+
+ it('returns itself if no numeric prefix', () => {
+ let seq = new KeySequence([
+ new Key({ key: 'g' }), new Key({ key: 'g' }) , new Key({ key: '3' }) ,
+ ]).trimNumericPrefix();
+
+ expect(seq.keys.map(key => key.key)).to.deep.equal(['g', 'g', '3']);
+ });
+ });
+
describe('#splitNumericPrefix', () => {
it('splits numeric prefix', () => {
expect(KeySequence.fromMapKeys('10gg').splitNumericPrefix()).to.deep.equal([