Move boolean and float parsing to shared utils
This commit is contained in:
27
src/scripts/stringUtils.js
Normal file
27
src/scripts/stringUtils.js
Normal file
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
export function toBoolean(value, defaultValue = false) {
|
||||
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.
|
||||
*/
|
||||
export function toFloat(value, defaultValue = 0) {
|
||||
if (value === null || value === '' || isNaN(value)) {
|
||||
return defaultValue;
|
||||
} else {
|
||||
return parseFloat(value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user