Skip to content

Commit

Permalink
Merge pull request #207 from BLSQ/handle-NaN-and-empty-string
Browse files Browse the repository at this point in the history
Handle NaN and empty string
  • Loading branch information
mestachs authored Dec 5, 2023
2 parents 9c1b26a + a9d5e65 commit e89fa69
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
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

0 comments on commit e89fa69

Please sign in to comment.