Add assert decorator functions.
This commit is contained in:
@@ -1,3 +1,43 @@
|
||||
function assertUInt8(target: any, key: string) {
|
||||
let val = this[key];
|
||||
if (delete this[key]) {
|
||||
Object.defineProperty(target, key, {
|
||||
get: function () {
|
||||
return val;
|
||||
},
|
||||
set: function (newVal) {
|
||||
if (newVal < 0 || newVal > 255) {
|
||||
throw `Invalid ${target.constructor.name}.${key}: ${newVal} is not uint8`;
|
||||
}
|
||||
val = newVal;
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function assertEnum<E>(enumerated: E) {
|
||||
return function(target: any, key: string) {
|
||||
let val = this[key];
|
||||
if (delete this[key]) {
|
||||
Object.defineProperty(target, key, {
|
||||
get: function () {
|
||||
return val;
|
||||
},
|
||||
set: function (newVal) {
|
||||
if (enumerated[newVal] === undefined) {
|
||||
throw `Invalid ${target.constructor.name}.${key}: ${newVal} is not enum`;
|
||||
}
|
||||
val = newVal;
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum Layer {
|
||||
mod,
|
||||
fn,
|
||||
@@ -11,8 +51,9 @@ class SwitchLayerAction extends KeyAction implements Serializable<SwitchLayerAct
|
||||
|
||||
isLayerToggleable: boolean;
|
||||
|
||||
private _layer: Layer;
|
||||
|
||||
@assertEnum(Layer)
|
||||
private layer: Layer;
|
||||
/*
|
||||
get layer(): number {
|
||||
return this._layer;
|
||||
}
|
||||
@@ -23,14 +64,14 @@ class SwitchLayerAction extends KeyAction implements Serializable<SwitchLayerAct
|
||||
}
|
||||
this._layer = value;
|
||||
}
|
||||
|
||||
*/
|
||||
getToggleFlag() {
|
||||
return this.isLayerToggleable ? SwitchLayerAction.toggleFlag : 0;
|
||||
}
|
||||
|
||||
fromJsObject(jsObject: any): SwitchLayerAction {
|
||||
this.assertKeyActionType(jsObject, SwitchLayerAction.keyActionTypeString, 'SwitchLayerAction');
|
||||
this.layer = jsObject.keymapId;
|
||||
this.layer = jsObject.layerId;
|
||||
this.isLayerToggleable = jsObject.toggle;
|
||||
return this;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user