Update isNumeric function
parent
6ce6b6da1b
commit
0c0a2077b3
|
@ -35,18 +35,16 @@ export function suite(): void {
|
||||||
//#region Private methods
|
//#region Private methods
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if a string is a number
|
* Check if a string is a number.
|
||||||
* @author Dan, Ben Aston
|
* @author Jeremy
|
||||||
* @see https://preview.tinyurl.com/y46jqwkt
|
* @see https://preview.tinyurl.com/y46jqwkt
|
||||||
*/
|
*/
|
||||||
function isNumeric(str: string): boolean {
|
function isNumeric(num: any): boolean {
|
||||||
// We only process strings!
|
const isNan = isNaN(num as number);
|
||||||
if (typeof str != "string") return false;
|
const isNum = typeof num === "number";
|
||||||
|
const isValidString = typeof num === "string" && num.trim() !== "";
|
||||||
|
|
||||||
// Use type coercion to parse the _entirety_ of the string
|
return (isNum || isValidString) && !isNan;
|
||||||
// (`parseFloat` alone does not do this) and ensure strings
|
|
||||||
// of whitespace fail
|
|
||||||
return !isNaN(parseInt(str, 10)) && !isNaN(parseFloat(str));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//#endregion
|
//#endregion
|
||||||
|
|
Loading…
Reference in New Issue