parseInteger
Parses a value into a finite integer. This parser combines the normalizeInteger normalizer with the validateInteger primitive validator to produce a canonical integer value.
The parser never throws and never mutates input. It returns a structured ParseResult<number> describing success or failure.
Signature
function parseInteger(
value: unknown,
field?: string
): ParseResult<number>
Behavior
- Accepted inputs:
- Primitive finite integers (42, 0, -7).
- Integer-like strings ("42", " -7 ").
- Strings are trimmed before parsing. Rejected inputs: These values are numbers but not finite integers:
NaN,Infinity, and-Infinity.- Non-integer numbers (3.14).
- These produce: "Value could not be normalized into a finite integer".
- Non-numeric strings ("abc").
- Empty or whitespace-only strings.
- Objects, arrays, functions.
- Booleans.
nullandundefined.- Boxed Number objects.
- These produce: "Value must be an integer"
Returns
A ParseResult<number> object:
ok: trueAnd the integer when parsing succeeds.ok: false, value: null: And a list of issues when parsing fails.
Examples
parseInteger(" 42 ")
// { ok: true, value: 42, issues: [] }
parseInteger(3.14)
// { ok: false, value: null, issues: ["Value could not be normalized into a finite integer"] }
parseInteger("abc")
// { ok: false, value: null, issues: ["Value must be an integer"] }
Notes
- This parser is stricter than parseNumber.