Skip to content

Commit

Permalink
[Calculator] Handle mathjs evaluating to null (oliverschwendener#831)
Browse files Browse the repository at this point in the history
* handle mathjs evaluating null

* [calculator-plugin] add a test for calculating null
  • Loading branch information
NotWearingPants authored Nov 1, 2021
1 parent 7a4f5da commit 3c5ed29
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/main/plugins/calculator-plugin/calculator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ describe(Calculator.name, () => {
expect(actual).toBe(expected);
});
});

it("should handle null", () => {
const precision = 16;
const calculation = "null";
const expected = "null";

const result = Calculator.calculate(calculation, precision);
expect(result).toBe(expected);
});
});

describe(Calculator.isValidInput.name, () => {
Expand Down
8 changes: 4 additions & 4 deletions src/main/plugins/calculator-plugin/calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ export class Calculator {
const math = this.math(precision);

if (!math.evaluate) {
throw new Error("Failed to instanciate math js static");
throw new Error("Failed to instantiate math js static");
}

const result: string = math
.evaluate(this.normalizeInput(input, decimalSeparator, argumentSeparator))
.toString();
const result: string = String(
math.evaluate(this.normalizeInput(input, decimalSeparator, argumentSeparator)),
);

return result.replace(new RegExp(",|\\.", "g"), (match) =>
match === "." ? decimalSeparator : argumentSeparator,
Expand Down

0 comments on commit 3c5ed29

Please sign in to comment.