1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-18 09:35:20 +02:00
joplin/ReactNativeClient/lib/services/WhenClause.ts

43 lines
930 B
TypeScript
Raw Normal View History

import { ContextKeyExpr, ContextKeyExpression } from './contextkey/contextkey';
export default class WhenClause {
private expression_:string;
private validate_:boolean;
private rules_:ContextKeyExpression = null;
constructor(expression:string, validate:boolean) {
this.expression_ = expression;
this.validate_ = validate;
}
private createContext(ctx: any) {
return {
getValue: (key: string) => {
return ctx[key];
},
};
}
private get rules():ContextKeyExpression {
if (!this.rules_) {
this.rules_ = ContextKeyExpr.deserialize(this.expression_);
}
return this.rules_;
}
public evaluate(context:any):boolean {
if (this.validate_) this.validate(context);
return this.rules.evaluate(this.createContext(context));
}
public validate(context:any) {
const keys = this.rules.keys();
for (const key of keys) {
if (!(key in context)) throw new Error(`No such key: ${key}`);
}
}
}