liquidjs
liquidjs copied to clipboard
Promise support in expressions
Hi,
This library support promises in context, however, this support seems to be missing when evaluating an expression
engine
.parseAndRender('{%if name== blank %}it is promise, not blank!{%endif%}', {name: Promise.resolve('alice')})
.then(console.log);
Was able to resolve the issue by modifying Expression class, i am not sure if i am correct (yields and generators are new concepts that i am not familiar with). Anyway, this is my slightly modified version of the class:
export class Expression {
private operands: any[] = []
private postfix: IterableIterator<Token>
public constructor (str: string) {
const tokenizer = new Tokenizer(str)
this.postfix = toPostfix(tokenizer.readExpression())
}
public * evaluate (ctx: Context): any {
for (const token of this.postfix) {
if (TypeGuards.isOperatorToken(token)) {
const r = yield this.operands.pop()
const l = yield this.operands.pop()
const result = evalOperatorToken(token, l, r, ctx)
this.operands.push(result)
} else {
this.operands.push(evalToken(token, ctx))
}
}
return this.operands[0]
}
public * value (ctx: Context) {
return toValue(yield this.evaluate(ctx))
}
}
This feature need substantial effort (I tried this too), essentially will change all expression evaluation to async. Also prone to performance issues.
Will leave this issue open to see how many people want this. Feel free to LIKE this.