Visual Console Refactor: added a new util function

Former-commit-id: 4ff4042bdefe87236f7109b27b736a8b21a60e97
This commit is contained in:
Alejandro Gallardo Escobar 2019-04-04 17:08:32 +02:00
parent eb1e505a79
commit 0d46d0a1a0
1 changed files with 18 additions and 0 deletions

View File

@ -22,6 +22,24 @@ export function parseIntOr<T>(value: any, defaultValue: T): number | T {
else return defaultValue;
}
/**
* Return a number or a default value from a raw value.
* @param value Raw value from which we will try to extract a valid number.
* @param defaultValue Default value to use if we cannot extract a valid number.
* @return A valid number or the default value.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function parseFloatOr<T>(value: any, defaultValue: T): number | T {
if (typeof value === "number") return value;
if (
typeof value === "string" &&
value.length > 0 &&
!isNaN(parseFloat(value))
)
return parseFloat(value);
else return defaultValue;
}
/**
* Check if a string exists and it's not empty.
* @param value Value to check.