Features
rut.ts is deliberately a focused toolkit: nine small functions that each do one job well, so you compose only what you need. Every throwing function also has a non-throwing safe mode ({ throwOnError: false }).
The nine functions#
Check shape + Modulo 11 verifier. Add strict to reject placeholders.
Permissively normalize to raw digits — for storage and display prep.
format()Produce the canonical XX.XXX.XXX-Y, with an incremental mode.
The Modulo 11 check-digit primitive behind validation.
decompose()Split a RUT into its body and verifier in a single call.
getBody()Extract just the body, without the verifier digit.
getVerifier()Extract just the verifier digit (always uppercase).
generate()Crypto-backed valid random RUTs for tests and fixtures.
isRutLike()Fast bounded shape check, no verifier math — ideal for filtering.
How they group#
Validation#
validate()— the acceptance gate (shape + verifier, optional strict mode)isRutLike()— cheap pre-filter, no verifier math
Formatting & cleaning#
format()— canonical display shape, with incremental typing modeclean()— strip formatting and leading zeros down to digits
Decomposition#
decompose()— body + verifier togethergetBody()— body onlygetVerifier()— verifier only
Generation & math#
generate()— valid random RUTscalculateVerifier()— derive the Modulo 11 check digit
Shared behavior#
All functions:
- ✅ Are fully typed — complete TypeScript definitions
- ✅ Bound input length before parsing (ReDoS-safe)
- ✅ Emit generic errors — never echo the offending value
- ✅ Accept multiple shapes — dots, hyphens, or plain digits
- ✅ Handle leading zeros — normalized away automatically
clean(), decompose(), getBody(), and getVerifier() normalize
shape but do not check the verifier digit. Gate acceptance with
validate().