Skip to content

Handle NaN and empty string #207

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

Merged
merged 2 commits into from
Dec 5, 2023
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@

# @blsq/blsq-report-components

- [dateentry] solve problems with NaN in safediv function
- [dateentry] solve problems single equals not turned into == leading to "SyntaxError: invalid assignment left-hand side"

# @blsq/blsq-report-components@1.1.6

- [invoices] fix [previous bug](https://community.dhis2.org/t/api-datavaluesets-dont-allow-to-mix-dataset-and-dataelementgroup-anymore/51517) where datavalues can be loaded twice once via the deg and once via the ds causing double amounts
Expand Down
17 changes: 10 additions & 7 deletions src/components/dataentry/CodeGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export const defaultSubstitutions = () => {
export const fixIfStatement = (expression) => {
expression = expression.replace("if (", "IF(");
expression = expression.replace("if(", "IF(");
expression = expression.split(" = ").join(" == ");
expression = expression.split(" =0").join(" == 0");
return expression;
};
export const generateGetterSetterForState = (hesabuPackage, activity, state, orgunitid, period) => {
Expand All @@ -27,6 +29,7 @@ export const generateGetterSetterForState = (hesabuPackage, activity, state, org
);
codes.push(" const v = calculator.indexedValues()[k]");
codes.push(' if(v && v[0].value == "") { return 0 }');
codes.push(' if(v && v[0].value == " ") { return 0 }');
codes.push(" if(v) { return parseFloat(v[0].value) }");
codes.push(" }");
codes.push(` return calculator.field_${field_name} == undefined ? 0 : this.field_${field_name}`);
Expand Down Expand Up @@ -54,6 +57,7 @@ export const generateGetterSetterForStateQuarterly = (hesabuPackage, activity, s
);
codes.push(" const v = calculator.indexedValues()[k]");
codes.push(' if(v && v[0].value == "") { return 0 }');
codes.push(' if(v && v[0].value == " ") { return 0 }');
codes.push(" if(v) { return parseFloat(v[0].value) }");
codes.push(" }");
codes.push(` return calculator.field_${field_name} == undefined ? 0 : this.field_${field_name}`);
Expand Down Expand Up @@ -81,6 +85,7 @@ export const generateGetterSetterForStateLevel1Quarterly = (hesabuPackage, activ
);
codes.push(" const v = calculator.indexedValues()[k]");
codes.push(' if(v && v[0].value == "") { return 0 }');
codes.push(' if(v && v[0].value == " ") { return 0 }');
codes.push(" if(v) { return parseFloat(v[0].value) }");
codes.push(" }");
codes.push(` return calculator.field_${field_name} == undefined ? 0 : this.field_${field_name}`);
Expand Down Expand Up @@ -124,8 +129,6 @@ export const generateActivityFormula = (
stateOrFormulaCodes,
states,
) => {
let expandedformula = "" + formula.expression;
expandedformula = fixIfStatement(expandedformula);
const substitutions = defaultSubstitutions();
for (let substit of stateOrFormulaCodes) {
substitutions[substit] = `calculator.${hesabuPackage.code}_${activity.code}_${substit}_${orgunitid}_${period}()`;
Expand All @@ -138,7 +141,6 @@ export const generateActivityFormula = (
substitutions[
substit + "_quarterly"
] = `calculator.${hesabuPackage.code}_${activity.code}_${substit}_quarterly_${orgunitid}_${period}()`;

}
if (hesabuPackage.activity_decision_tables) {
for (let rawDecisionTable of hesabuPackage.activity_decision_tables) {
Expand All @@ -160,10 +162,12 @@ export const generateActivityFormula = (
for (let substit of states) {
substit = substit + "_level_1_quarterly";
substitutions[substit] = `calculator.${hesabuPackage.code}_${activity.code}_${substit}_${orgunitid}_${period}()`;

}

const tokens = tokenize(formula.expression);
let expandedformula = "" + formula.expression;
expandedformula = fixIfStatement(expandedformula);

const tokens = tokenize(expandedformula);

expandedformula = tokens.map((token) => substitutions[token] || token).join("");

Expand Down Expand Up @@ -321,9 +325,8 @@ export const generateCode = (
),
);


const states = stateOrFormulaCodes.filter((k) => !allFormulaCodes.includes(k));

for (let period of DatePeriods.split(invoicePeriod, hesabuPackage.frequency)) {
for (let activity of hesabuPackage.activities) {
// states getter/setter
Expand Down
2 changes: 1 addition & 1 deletion src/components/dataentry/FunctionRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import _ from "lodash"


export const SAFE_DIV = (a, b) => {
if (b !== 0) {
if (!isNaN(a) && !isNaN(b) && a!== undefined && b !== 0) {
return a / b;
}
return 0;
Expand Down
6 changes: 6 additions & 0 deletions src/components/dataentry/FunctionRegistry.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ describe("FunctionRegistry", () => {
it("divide a by b", () => {
expect(safeDiv(8, 10)).toEqual(0.8)
})
it("divide NaN by b", () => {
expect(safeDiv(NaN, 10)).toEqual(0)
})
it("divide a by NaN", () => {
expect(safeDiv(10, NaN)).toEqual(0)
})
})

describe("ABS", () => {
Expand Down