2021-09-02 08:39:58 -04:00
|
|
|
/**
|
|
|
|
* Gets the value of a string as boolean.
|
|
|
|
* @param {string} name The value as a string.
|
|
|
|
* @param {boolean} defaultValue The default value if the string is invalid.
|
|
|
|
* @returns {boolean} The value.
|
|
|
|
*/
|
2022-04-10 01:30:26 -04:00
|
|
|
export function toBoolean(value: string | undefined | null, defaultValue = false) {
|
2021-09-02 08:39:58 -04:00
|
|
|
if (value !== 'true' && value !== 'false') {
|
|
|
|
return defaultValue;
|
|
|
|
} else {
|
|
|
|
return value !== 'false';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the value of a string as float number.
|
|
|
|
* @param {string} value The value as a string.
|
|
|
|
* @param {number} defaultValue The default value if the string is invalid.
|
|
|
|
* @returns {number} The value.
|
|
|
|
*/
|
2022-04-10 01:30:26 -04:00
|
|
|
export function toFloat(value: string | null | undefined, defaultValue = 0) {
|
2024-01-03 10:03:15 -05:00
|
|
|
if (!value) return defaultValue;
|
|
|
|
|
|
|
|
const number = parseFloat(value);
|
|
|
|
if (isNaN(number)) return defaultValue;
|
|
|
|
|
|
|
|
return number;
|
2021-09-02 08:39:58 -04:00
|
|
|
}
|