Add TypeChecker and use its methods wherever possible.

This commit is contained in:
László Monda
2016-03-30 01:59:05 +02:00
parent 94e5d55d8f
commit 0edde3236a
7 changed files with 18 additions and 20 deletions

View File

@@ -0,0 +1,13 @@
class TypeChecker {
static firstValidScancode = 1;
static lastValidScancode = 231;
static isUInt8Valid(uint8: number): boolean {
return 0 <= uint8 && uint8 <= 255;
}
static isScancodeValid(scancode: number): boolean {
return TypeChecker.firstValidScancode <= scancode && scancode <= TypeChecker.lastValidScancode;
}
}

View File

@@ -1,4 +1,3 @@
class UhkBuffer {
private static eepromSize = 32 * 1024;
private static maxStringByteLength = 0xFFFF;

View File

@@ -17,7 +17,7 @@ class KeyAction {
let keyActionFirstByte = buffer.readUInt8();
buffer.backtrack();
if (KeystrokeAction.isScancodeValid(keyActionFirstByte)) {
if (TypeChecker.isScancodeValid(keyActionFirstByte)) {
return new KeystrokeAction().fromBinary(buffer);
} else if (keyActionFirstByte === MouseAction.keyActionId) {
return new MouseAction().fromBinary(buffer);

View File

@@ -1,8 +1,6 @@
class KeystrokeAction extends KeyAction implements Serializable<KeystrokeAction> {
static keyActionTypeString = 'keystroke';
static firstValidScancode = 1;
static lastValidScancode = 231;
modifierMask: number;
@@ -13,17 +11,12 @@ class KeystrokeAction extends KeyAction implements Serializable<KeystrokeAction>
}
set scancode(value) {
if (!KeystrokeAction.isScancodeValid(value)) {
if (!TypeChecker.isScancodeValid(value)) {
throw 'Invalid KeystrokeAction.scancode: ${scancode}';
}
this._scancode = value;
}
static isScancodeValid(scancode) {
return KeystrokeAction.firstValidScancode <= scancode &&
scancode <= KeystrokeAction.lastValidScancode;
}
fromJsObject(jsObject: any): KeystrokeAction {
this.scancode = jsObject.scancode;
this.modifierMask = jsObject.modifierMask;

View File

@@ -10,16 +10,12 @@ class PlayMacroAction extends KeyAction implements Serializable<PlayMacroAction>
}
set macroId(value) {
if (!PlayMacroAction.isMacroIdValid(value)) {
if (!TypeChecker.isUInt8Valid(value)) {
throw 'Invalid PlayMacroAction.macroId: ${value}';
}
this._macroId = value;
}
static isMacroIdValid(macroId) {
return 0 <= macroId && macroId <= 255;
}
fromJsObject(jsObject: any): PlayMacroAction {
this.macroId = jsObject.macroId;
return this;

View File

@@ -10,16 +10,12 @@ class SwitchKeymapAction extends KeyAction implements Serializable<SwitchKeymapA
}
set keymapId(value) {
if (!SwitchKeymapAction.isKeymapIdValid(value)) {
if (!TypeChecker.isUInt8Valid(value)) {
throw 'Invalid SwitchKeymapAction.keymapId: ${value}';
}
this._keymapId = value;
}
static isKeymapIdValid(keymapId) {
return 0 <= keymapId && keymapId <= 255;
}
fromJsObject(jsObject: any): SwitchKeymapAction {
this.keymapId = jsObject.keymapId;
return this;

View File

@@ -1,3 +1,4 @@
/// <reference path="TypeChecker.ts" />
/// <reference path="Serializable.ts" />
/// <reference path="UhkBuffer.ts" />
/// <reference path="config-items/config-items.ts" />