@farris/expression-engine-vue
Version:
51 lines (50 loc) • 1.64 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.Expression = void 0;
/* eslint-disable @typescript-eslint/ban-types */
const index_1 = require("../compiler/index");
const index_2 = require("../evaler/index");
const index_3 = require("../function/index");
class Expression {
constructor(expr, context) {
this.factory = null;
this.expr = expr;
this.context = context;
}
/**
* 执行表达式
* @param context
* @returns
*/
eval(context) {
context = context || this.context;
const args = context && context.contexts && context.contexts.arguments || {};
if (this.factory) {
const ctx = this.buildContext(context);
const result = this.factory(ctx === null || ctx === void 0 ? void 0 : ctx.contexts)(args);
return result;
}
const evaler = new index_2.Evaler();
const factory = evaler.eval(this.expr, context);
const result = factory(...Object.values(args));
return result;
}
/**
* 编译表达式
* @param context 上下文
* @returns
*/
compile(context) {
context = context || this.context;
const compiler = new index_1.Compiler();
this.factory = compiler.compile(this.expr, context);
return this;
}
buildContext(context) {
const contexts = context && context.contexts || {};
const defaultFunction = new index_3.DefaultFunctions(context);
contexts.DefaultFunction = defaultFunction;
return context;
}
}
exports.Expression = Expression;
;