Skip to content

Commit

Permalink
perf(json-expression): ⚡️ pre-cast Literals to string
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 17, 2024
1 parent a36bed9 commit 84c5aeb
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/json-expression/operators/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,15 @@ export const stringOperators: types.OperatorDefinition<any>[] = [
},
(ctx: types.OperatorCodegenCtx<types.ExprCat>): ExpressionResult => {
ctx.link(util.str, 'str');
const js = ctx.operands.map((expr) => `str(${expr})`).join('+');
return new Expression(js);
const parts: string[] = [];
for (const operand of ctx.operands) {
if (operand instanceof Literal) {
parts.push(JSON.stringify(util.str(operand.val)));
} else if (operand instanceof Expression) {
parts.push(`str(${operand})`);
}
}
return new Expression(parts.join('+'));
},
] as types.OperatorDefinition<types.ExprCat>,

Expand Down

0 comments on commit 84c5aeb

Please sign in to comment.