From fcd0d5b5b368ea1a730ce42fefa229a4a8c252fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Monda?= Date: Fri, 1 Apr 2016 02:42:40 +0200 Subject: [PATCH] Move assert functions to assert.ts --- config-serializer/assert.ts | 39 ++++++++++++++++++ .../config-items/SwitchLayerAction.ts | 40 ------------------- config-serializer/serializeConfig.ts | 1 + 3 files changed, 40 insertions(+), 40 deletions(-) create mode 100644 config-serializer/assert.ts 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 @@ +/// /// /// ///