mirror of
https://github.com/pandorafms/pandorafms.git
synced 2025-07-31 01:35:36 +02:00
Visual Console Client: bugfix and new unit test
Former-commit-id: e1fa114f0457255ec264e379783498739f0625e8
This commit is contained in:
parent
5e3cdbdcf8
commit
ee0eb6846b
12
visual_console/src/lib.spec.ts
Normal file
12
visual_console/src/lib.spec.ts
Normal 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);
|
||||||
|
});
|
||||||
|
});
|
@ -16,7 +16,7 @@ import {
|
|||||||
*/
|
*/
|
||||||
export function parseIntOr<T>(value: any, defaultValue: T): number | T {
|
export function parseIntOr<T>(value: any, defaultValue: T): number | T {
|
||||||
if (typeof value === "number") return value;
|
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);
|
return parseInt(value);
|
||||||
else return defaultValue;
|
else return defaultValue;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user