From ee0eb6846ba7a4b94f0b8c4bc7ea7e2655f79107 Mon Sep 17 00:00:00 2001 From: Alejandro Gallardo Escobar Date: Fri, 22 Feb 2019 13:37:30 +0100 Subject: [PATCH] Visual Console Client: bugfix and new unit test Former-commit-id: e1fa114f0457255ec264e379783498739f0625e8 --- visual_console/src/lib.spec.ts | 12 ++++++++++++ visual_console/src/lib.ts | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 visual_console/src/lib.spec.ts diff --git a/visual_console/src/lib.spec.ts b/visual_console/src/lib.spec.ts new file mode 100644 index 0000000000..e1913c5ef8 --- /dev/null +++ b/visual_console/src/lib.spec.ts @@ -0,0 +1,12 @@ +import { parseIntOr } from "./lib"; + +describe("function parseIntOr", () => { + it("should retrieve valid int or a default value", () => { + expect(parseIntOr("Foo", null)).toBe(null); + expect(parseIntOr("1a", null)).toBe(1); + expect(parseIntOr("a1", null)).toBe(null); + expect(parseIntOr("1", null)).toBe(1); + expect(parseIntOr(false, null)).toBe(null); + expect(parseIntOr(1, null)).toBe(1); + }); +}); diff --git a/visual_console/src/lib.ts b/visual_console/src/lib.ts index 929fb00da5..b31696bf4c 100644 --- a/visual_console/src/lib.ts +++ b/visual_console/src/lib.ts @@ -16,7 +16,7 @@ import { */ export function parseIntOr(value: any, defaultValue: T): number | T { if (typeof value === "number") return value; - if (typeof value === "string" && value.length > 0 && isNaN(parseInt(value))) + if (typeof value === "string" && value.length > 0 && !isNaN(parseInt(value))) return parseInt(value); else return defaultValue; }