From 0c0a2077b319202c1a7f5ea30b125917400fcc38 Mon Sep 17 00:00:00 2001 From: MillenniumEarl Date: Mon, 8 Mar 2021 16:35:59 +0100 Subject: [PATCH] Update isNumeric function --- test/classes/credentials.ts | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/test/classes/credentials.ts b/test/classes/credentials.ts index 09b4cd7..d160e1f 100644 --- a/test/classes/credentials.ts +++ b/test/classes/credentials.ts @@ -35,18 +35,16 @@ export function suite(): void { //#region Private methods /** - * Check if a string is a number - * @author Dan, Ben Aston + * Check if a string is a number. + * @author Jeremy * @see https://preview.tinyurl.com/y46jqwkt */ -function isNumeric(str: string): boolean { - // We only process strings! - if (typeof str != "string") return false; +function isNumeric(num: any): boolean { + const isNan = isNaN(num as number); + const isNum = typeof num === "number"; + const isValidString = typeof num === "string" && num.trim() !== ""; - // Use type coercion to parse the _entirety_ of the string - // (`parseFloat` alone does not do this) and ensure strings - // of whitespace fail - return !isNaN(parseInt(str, 10)) && !isNaN(parseFloat(str)); + return (isNum || isValidString) && !isNan; } //#endregion