Skip to main content

generateCoerceBooleanCheck()

generateCoerceBooleanCheck(varName, ctx): CodeGenResult

Experimental

Generates inline code for coerce.boolean().

If the value is already a boolean, returns true. Otherwise, converts to boolean using Boolean(value) and returns { coerced: result }. Boolean coercion never fails - any value can be coerced to boolean.


Parametersโ€‹

varName: stringโ€‹

The variable name to coerce

ctx: GeneratorContextโ€‹

The generator context


Returns: CodeGenResultโ€‹

Generated code and updated context


Sinceโ€‹

2.0.0


Exampleโ€‹

const result = generateCoerceBooleanCheck("value", ctx);
// Generated code:
// if (typeof value === "boolean") return true;
// return { coerced: Boolean(value) };