Add TypeChecker and use its methods wherever possible.
This commit is contained in:
13
config-serializer/TypeChecker.ts
Normal file
13
config-serializer/TypeChecker.ts
Normal 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,4 +1,3 @@
|
||||
|
||||
class UhkBuffer {
|
||||
private static eepromSize = 32 * 1024;
|
||||
private static maxStringByteLength = 0xFFFF;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
/// <reference path="TypeChecker.ts" />
|
||||
/// <reference path="Serializable.ts" />
|
||||
/// <reference path="UhkBuffer.ts" />
|
||||
/// <reference path="config-items/config-items.ts" />
|
||||
|
||||
Reference in New Issue
Block a user