diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-12-10 21:45:16 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-12-13 22:09:11 +0900 |
commit | 38a69e4ca319f9db3c54a5cb69cd5645f12369d5 (patch) | |
tree | e4a4279cb4c30df68e3c292154e2152dc9531815 /src | |
parent | c399b992e6665f194ebe08a48aae1ede32c6767f (diff) |
Add prefix functions in KeySequence
Diffstat (limited to 'src')
-rw-r--r-- | src/content/domains/KeySequence.ts (renamed from src/shared/settings/KeySequence.ts) | 24 | ||||
-rw-r--r-- | src/content/repositories/KeymapRepository.ts | 2 | ||||
-rw-r--r-- | src/content/usecases/KeymapUseCase.ts | 2 |
3 files changed, 25 insertions, 3 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>; |