Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle null emits #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/interface.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If computedValue is a valid falsy value (0, false), it would still return null. That is not intended in some cases such as subtract or boolean expression.

return null;
}

return computedValue;
}
},
});
Expand Down
22 changes: 14 additions & 8 deletions src/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,21 @@ export function parseExpression(exp: string, values: Record<string, any>): 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above if valueA is a valid falsy value.

return '';
}

// unary operators
if (b === null) {
Expand Down Expand Up @@ -85,14 +99,6 @@ export function parseExpression(exp: string, values: Record<string, any>): 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);
Expand Down