diff --git a/config-serializer/assert.ts b/config-serializer/assert.ts new file mode 100644 index 00000000..7015ba2d --- /dev/null +++ b/config-serializer/assert.ts @@ -0,0 +1,39 @@ +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(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 + }); + } + } +} diff --git a/config-serializer/config-items/SwitchLayerAction.ts b/config-serializer/config-items/SwitchLayerAction.ts index 66f2e632..1837baa0 100644 --- a/config-serializer/config-items/SwitchLayerAction.ts +++ b/config-serializer/config-items/SwitchLayerAction.ts @@ -1,43 +1,3 @@ -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(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, diff --git a/config-serializer/serializeConfig.ts b/config-serializer/serializeConfig.ts index 262a4ca0..61610e77 100644 --- a/config-serializer/serializeConfig.ts +++ b/config-serializer/serializeConfig.ts @@ -1,3 +1,4 @@ +/// /// /// ///