Move assert functions to assert.ts

This commit is contained in:
László Monda
2016-04-01 02:42:40 +02:00
parent aa812a477b
commit fcd0d5b5b3
3 changed files with 40 additions and 40 deletions

View File

@@ -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<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
});
}
}
}