diginext-utils
Version:
README.md
35 lines • 833 B
JavaScript
import { isNull } from "./isNull.js";
import { toFloat } from "./toFloat.js";
/**
* Converts a value to a boolean.
* Recognizes string representations like "true", "false", "0", "1".
*
* @param value - The value to convert
* @returns The boolean representation
*
* @example
* ```ts
* toBool('true'); // true
* toBool('false'); // false
* toBool('1'); // true
* toBool('0'); // false
* toBool(1); // true
* toBool(0); // false
* toBool(null); // false
* toBool(''); // false
* ```
*/
export function toBool(value) {
if (isNull(value)) {
return false;
}
const str = String(value).toLowerCase();
if (str === "false") {
return false;
}
if (toFloat(value) === 0) {
return false;
}
return str === "true" || toFloat(value) !== 0;
}
//# sourceMappingURL=toBool.js.map