Visual Console Client: bugfix and new unit test

Former-commit-id: e1fa114f0457255ec264e379783498739f0625e8
This commit is contained in:
Alejandro Gallardo Escobar 2019-02-22 13:37:30 +01:00
parent 5e3cdbdcf8
commit ee0eb6846b
2 changed files with 13 additions and 1 deletions

View File

@ -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);
});
});

View File

@ -16,7 +16,7 @@ import {
*/
export function parseIntOr<T>(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;
}