Skip to content

Commit

Permalink
fix: move random functions into relevant classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jpoon committed Jul 27, 2018
1 parent d130d19 commit 44ae97c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
27 changes: 10 additions & 17 deletions src/actions/base.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { configuration } from './../configuration/configuration';
import { ModeName } from './../mode/mode';
import { VimState } from './../state/vimState';
import { Notation } from '../configuration/notation';

const is2DArray = function<T>(x: any): x is T[][] {
return Array.isArray(x[0]);
};

export class BaseAction {
/**
Expand Down Expand Up @@ -64,7 +62,7 @@ export class BaseAction {
return false;
}

const keys2D = is2DArray(this.keys) ? this.keys : [this.keys];
const keys2D = BaseAction.is2DArray(this.keys) ? this.keys : [this.keys];
const keysSlice = keys2D.map(x => x.slice(0, keysPressed.length));
if (!BaseAction.CompareKeypressSequence(keysSlice, keysPressed)) {
return false;
Expand All @@ -80,11 +78,11 @@ export class BaseAction {
return true;
}

public static CompareKeypressSequence = function(
public static CompareKeypressSequence(
one: string[] | string[][],
two: string[]
): boolean {
if (is2DArray(one)) {
if (BaseAction.is2DArray(one)) {
for (const sequence of one) {
if (BaseAction.CompareKeypressSequence(sequence, two)) {
return true;
Expand All @@ -98,15 +96,6 @@ export class BaseAction {
return false;
}

const containsControlKey = (s: string): boolean => {
// We count anything starting with < (e.g. <c-u>) as a control key, but we
// exclude the first 3 because it's more convenient to do so.
s = s.toUpperCase();
return (
s !== '<BS>' && s !== '<SHIFT+BS>' && s !== '<TAB>' && s.startsWith('<') && s.length > 1
);
};

const isSingleNumber: RegExp = /^[0-9]$/;
const isSingleAlpha: RegExp = /^[a-zA-Z]$/;

Expand Down Expand Up @@ -135,10 +124,10 @@ export class BaseAction {
continue;
}

if (left === '<character>' && !containsControlKey(right)) {
if (left === '<character>' && !Notation.IsContainControlKey(right)) {
continue;
}
if (right === '<character>' && !containsControlKey(left)) {
if (right === '<character>' && !Notation.IsContainControlKey(left)) {
continue;
}

Expand Down Expand Up @@ -167,6 +156,10 @@ export class BaseAction {
public toString(): string {
return this.keys.join('');
}

private static is2DArray<T>(x: any): x is T[][] {
return Array.isArray(x[0]);
};
}

export enum KeypressState {
Expand Down
5 changes: 5 additions & 0 deletions src/configuration/notation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ export class Notation {
'\n': ['<cr>', '<enter>'],
};

public static IsContainControlKey(key: string): boolean {
key = key.toLocaleUpperCase();
return this.isSurroundedByAngleBrackets(key) && key !== '<BS>' && key !== '<SHIFT+BS>' && key !== '<TAB>';
}

/**
* Normalizes key to AngleBracketNotation
* (e.g. <ctrl+x>, Ctrl+x, <c-x> normalized to <C-x>)
Expand Down

0 comments on commit 44ae97c

Please sign in to comment.