diff --git a/src/interface.vue b/src/interface.vue index c81e1ff9..43fead1a 100644 --- a/src/interface.vue +++ b/src/interface.vue @@ -70,10 +70,16 @@ export default defineComponent({ }; function compute() { - return props.template.replace(/{{.*?}}/g, (match) => { + const computedValue = props.template.replace(/{{.*?}}/g, (match) => { const expression = match.slice(2, -2).trim(); return parseExpression(expression, values.value); }); + + if (!computedValue) { + return null; + } + + return computedValue; } }, }); diff --git a/src/operations.ts b/src/operations.ts index a5e023c5..a7fd2d64 100644 --- a/src/operations.ts +++ b/src/operations.ts @@ -8,7 +8,21 @@ export function parseExpression(exp: string, values: Record): any { const opMatch = parseOp(exp); if (opMatch) { const { op, a, b } = opMatch; + + if (op === 'ASUM') { + // aggregated sum + return ( + (values[a] as unknown[])?.reduce( + (acc, item) => acc + parseExpression(b!, item as typeof values), + 0 + ) ?? 0 + ); + } + const valueA = parseExpression(a, values); + if (!valueA) { + return ''; + } // unary operators if (b === null) { @@ -85,14 +99,6 @@ export function parseExpression(exp: string, values: Record): any { } return 0; } - } else if (op === 'ASUM') { - // aggregated sum - return ( - (values[a] as unknown[])?.reduce( - (acc, item) => acc + parseExpression(b, item as typeof values), - 0 - ) ?? 0 - ); } else { // binary operators const valueB = parseExpression(b, values);