aboutsummaryrefslogtreecommitdiff
path: root/extension/background/checkable.ts
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2016-01-05 20:09:15 +0100
committerFlorian Dold <florian.dold@gmail.com>2016-01-05 20:09:15 +0100
commit7b2c566cad36a00bbbcf0beb05731020787b08e0 (patch)
tree6b1e0d43e18243f64cabeee6a8060d37b54d2f8f /extension/background/checkable.ts
parent9dc0d89118dba960e672e024411f3ef0af94f5c6 (diff)
downloadwallet-core-7b2c566cad36a00bbbcf0beb05731020787b08e0.tar.xz
make Checkables work
Diffstat (limited to 'extension/background/checkable.ts')
-rw-r--r--extension/background/checkable.ts76
1 files changed, 58 insertions, 18 deletions
diff --git a/extension/background/checkable.ts b/extension/background/checkable.ts
index 63fbd3646..f7e99df92 100644
--- a/extension/background/checkable.ts
+++ b/extension/background/checkable.ts
@@ -27,41 +27,81 @@
namespace Checkable {
let chkSym = Symbol("checkable");
- function checkNumber(target, prop) {
- return true;
+ function checkNumber(target, prop): any {
+ if ((typeof target) !== "number") {
+ throw Error("number expected for " + prop.propertyKey);
+ }
+ return target;
}
- function checkString(target, prop) {
- return true;
+ function checkString(target, prop): any {
+ if (typeof target !== "string") {
+ throw Error("string expected for " + prop.propertyKey);
+ }
+ return target;
}
- export function Class(target) {
- target.checked = (v) => {
- let props = target.prototype[chkSym].props;
- console.log("hello, world");
- let remainingPropNames = new Set(Object.getOwnPropertyNames(v));
-
- for (let prop of props) {
- remainingPropNames.delete(prop);
- console.log("prop", prop);
+ function checkValue(target, prop): any {
+ let type = prop.type;
+ if (!type) {
+ throw Error("assertion failed");
+ }
+ let v = target;
+ if (!v || typeof v !== "object") {
+ throw Error("expected object for " + prop.propertyKey);
+ }
+ let props = type.prototype[chkSym].props;
+ let remainingPropNames = new Set(Object.getOwnPropertyNames(v));
+ let obj = new type();
+ for (let prop of props) {
+ if (!remainingPropNames.has(prop.propertyKey)) {
+ throw Error("Property missing: " + prop.propertyKey);
}
-
- if (remainingPropNames.size != 0) {
- throw Error("superfluous properties " + JSON.stringify(remainingPropNames.values()));
+ if (!remainingPropNames.delete(prop.propertyKey)) {
+ throw Error("assertion failed");
}
+ let propVal = v[prop.propertyKey];
+ obj[prop.propertyKey] = prop.checker(propVal, prop);
+ }
+
+ if (remainingPropNames.size != 0) {
+ throw Error("superfluous properties " + JSON.stringify(Array.from(
+ remainingPropNames.values())));
+ }
+ return obj;
+ }
+
+ export function Class(target) {
+ target.checked = (v) => {
+ return checkValue(v, {
+ propertyKey: "(root)",
+ type: target,
+ checker: checkValue
+ });
};
return target;
}
export function Value(type) {
- function deco(target) {
+ function deco(target: Object, propertyKey: string | symbol): void {
+ let chk = target[chkSym];
+ if (!chk) {
+ chk = {props: []};
+ target[chkSym] = chk;
+ }
+ chk.props.push({
+ propertyKey: propertyKey,
+ checker: checkValue,
+ type: type
+ });
}
return deco;
}
export function List(type) {
- function deco(target) {
+ function deco(target: Object, propertyKey: string | symbol): void {
+ throw Error("not implemented");
}
return deco;