25 lines
795 B
TypeScript
Raw Normal View History

/**
* WHMCS Coercion Utilities (domain-internal)
*/
/**
* Coerce boolean-like values from WHMCS into actual booleans.
*
* Handles all shapes that WHMCS returns for boolean fields:
* - boolean pass-through
* - number 1 is true, everything else false
* - string "1", "true", "yes", "on" are true (case-insensitive)
* - null / undefined false
*/
export function coerceBoolean(value: boolean | number | string | null | undefined): boolean {
if (typeof value === "boolean") return value;
if (typeof value === "number") return value === 1;
if (typeof value === "string") {
const normalized = value.trim().toLowerCase();
return (
normalized === "1" || normalized === "true" || normalized === "yes" || normalized === "on"
);
}
return false;
}