Features
rut.ts is deliberately a focused toolkit: twelve 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 functions#
Check shape + Modulo 11 verifier. Add strict to reject placeholders.
Type guard that narrows unknown to the branded Rut type.
Permissively normalize to raw digits — for storage and display prep.
format()Produce the canonical XX.XXX.XXX-Y, with an incremental mode.
Partially hide a RUT for display — 12..-5.
Answer "same RUT?" across shapes — validity-checked by default since 5.0.0.
calculateVerifier()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)isValidRut()— type guard that narrows to the brandedRutisRutLike()— cheap pre-filter, no verifier math
Formatting & cleaning#
format()— canonical display shape, with incremental typing modeclean()— strip formatting and leading zeros down to digitsmask()— partially hide a RUT for display (12.***.***-5)
Comparison#
equals()— answer "same RUT?" across shapes, checking validity by default ({ requireValid: false }for pure shape comparison)
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 — the lenient helpers (
clean,format,equals) strip them; the acceptance predicates (validate,isValidRut,isRutLike) reject them since 5.0.0
clean(), decompose(), getBody(), and getVerifier() normalize
shape but do not check the verifier digit. Gate acceptance with
validate().