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
+13
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;
}
}
-1
View File
@@ -1,4 +1,3 @@
class UhkBuffer { class UhkBuffer {
private static eepromSize = 32 * 1024; private static eepromSize = 32 * 1024;
private static maxStringByteLength = 0xFFFF; private static maxStringByteLength = 0xFFFF;
+1 -1
View File
@@ -17,7 +17,7 @@ class KeyAction {
let keyActionFirstByte = buffer.readUInt8(); let keyActionFirstByte = buffer.readUInt8();
buffer.backtrack(); buffer.backtrack();
if (KeystrokeAction.isScancodeValid(keyActionFirstByte)) { if (TypeChecker.isScancodeValid(keyActionFirstByte)) {
return new KeystrokeAction().fromBinary(buffer); return new KeystrokeAction().fromBinary(buffer);
} else if (keyActionFirstByte === MouseAction.keyActionId) { } else if (keyActionFirstByte === MouseAction.keyActionId) {
return new MouseAction().fromBinary(buffer); return new MouseAction().fromBinary(buffer);
@@ -1,8 +1,6 @@
class KeystrokeAction extends KeyAction implements Serializable<KeystrokeAction> { class KeystrokeAction extends KeyAction implements Serializable<KeystrokeAction> {
static keyActionTypeString = 'keystroke'; static keyActionTypeString = 'keystroke';
static firstValidScancode = 1;
static lastValidScancode = 231;
modifierMask: number; modifierMask: number;
@@ -13,17 +11,12 @@ class KeystrokeAction extends KeyAction implements Serializable<KeystrokeAction>
} }
set scancode(value) { set scancode(value) {
if (!KeystrokeAction.isScancodeValid(value)) { if (!TypeChecker.isScancodeValid(value)) {
throw 'Invalid KeystrokeAction.scancode: ${scancode}'; throw 'Invalid KeystrokeAction.scancode: ${scancode}';
} }
this._scancode = value; this._scancode = value;
} }
static isScancodeValid(scancode) {
return KeystrokeAction.firstValidScancode <= scancode &&
scancode <= KeystrokeAction.lastValidScancode;
}
fromJsObject(jsObject: any): KeystrokeAction { fromJsObject(jsObject: any): KeystrokeAction {
this.scancode = jsObject.scancode; this.scancode = jsObject.scancode;
this.modifierMask = jsObject.modifierMask; this.modifierMask = jsObject.modifierMask;
@@ -10,16 +10,12 @@ class PlayMacroAction extends KeyAction implements Serializable<PlayMacroAction>
} }
set macroId(value) { set macroId(value) {
if (!PlayMacroAction.isMacroIdValid(value)) { if (!TypeChecker.isUInt8Valid(value)) {
throw 'Invalid PlayMacroAction.macroId: ${value}'; throw 'Invalid PlayMacroAction.macroId: ${value}';
} }
this._macroId = value; this._macroId = value;
} }
static isMacroIdValid(macroId) {
return 0 <= macroId && macroId <= 255;
}
fromJsObject(jsObject: any): PlayMacroAction { fromJsObject(jsObject: any): PlayMacroAction {
this.macroId = jsObject.macroId; this.macroId = jsObject.macroId;
return this; return this;
@@ -10,16 +10,12 @@ class SwitchKeymapAction extends KeyAction implements Serializable<SwitchKeymapA
} }
set keymapId(value) { set keymapId(value) {
if (!SwitchKeymapAction.isKeymapIdValid(value)) { if (!TypeChecker.isUInt8Valid(value)) {
throw 'Invalid SwitchKeymapAction.keymapId: ${value}'; throw 'Invalid SwitchKeymapAction.keymapId: ${value}';
} }
this._keymapId = value; this._keymapId = value;
} }
static isKeymapIdValid(keymapId) {
return 0 <= keymapId && keymapId <= 255;
}
fromJsObject(jsObject: any): SwitchKeymapAction { fromJsObject(jsObject: any): SwitchKeymapAction {
this.keymapId = jsObject.keymapId; this.keymapId = jsObject.keymapId;
return this; return this;
+1
View File
@@ -1,3 +1,4 @@
/// <reference path="TypeChecker.ts" />
/// <reference path="Serializable.ts" /> /// <reference path="Serializable.ts" />
/// <reference path="UhkBuffer.ts" /> /// <reference path="UhkBuffer.ts" />
/// <reference path="config-items/config-items.ts" /> /// <reference path="config-items/config-items.ts" />