From 514596f964547ec1a2f5e7548937a2d85f0b3848 Mon Sep 17 00:00:00 2001 From: Oliver Schwendener Date: Fri, 8 Feb 2019 18:36:59 +0100 Subject: [PATCH] Calculator: treat user input as invalid If math.eval returns undefined (Fixes #108) --- .../unit/input-validators/calculator-input-validator.test.ts | 2 ++ src/ts/calculator/calculator.ts | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/src/tests/unit/input-validators/calculator-input-validator.test.ts b/src/tests/unit/input-validators/calculator-input-validator.test.ts index 87cdf1135..3ee4ea5c3 100644 --- a/src/tests/unit/input-validators/calculator-input-validator.test.ts +++ b/src/tests/unit/input-validators/calculator-input-validator.test.ts @@ -24,6 +24,8 @@ describe(CalculatorInputValidator.name, (): void => { it("should return false when passing in an invalid argument", (): void => { const invalidInputs = [ + "#", + "#2", "s", "kg", "log()", diff --git a/src/ts/calculator/calculator.ts b/src/ts/calculator/calculator.ts index 81890ac19..266e74f1b 100644 --- a/src/ts/calculator/calculator.ts +++ b/src/ts/calculator/calculator.ts @@ -18,6 +18,10 @@ export class Calculator { } catch (e) { return false; } + + if (result === undefined) { + return false; + } return !isNaN(result) || this.isValidMathType(result) || false; }