generateCoerceNumberCheck()
generateCoerceNumberCheck(
varName,ctx,customMessage?):CodeGenResult
Experimental
Generates inline code for coerce.number().
If the value is already a valid number (not NaN), returns true.
Otherwise, converts to number using Number(value) with NaN check.
Returns { coerced: result } for successful coercions.
Parametersβ
varName: stringβ
The variable name to coerce
ctx: GeneratorContextβ
The generator context
customMessage?: stringβ
Optional custom error message
Returns: CodeGenResultβ
Generated code and updated context
Sinceβ
2.0.0
Exampleβ
const result = generateCoerceNumberCheck("value", ctx);
// Generated code handles:
// - Already a number: return true
// - Boolean: coerce to 0/1
// - Empty string: coerce to 0
// - Other: try Number(value) with NaN check