Visual Console Client: bugfix and new unit test
Former-commit-id: e1fa114f0457255ec264e379783498739f0625e8
This commit is contained in:
parent
5e3cdbdcf8
commit
ee0eb6846b
|
@ -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);
|
||||
});
|
||||
});
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue