Skip to main content

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