Skip to content

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#

How they group#

Validation#

  • validate() — the acceptance gate (shape + verifier, optional strict mode)
  • isValidRut() — type guard that narrows to the branded Rut
  • isRutLike() — cheap pre-filter, no verifier math

Formatting & cleaning#

  • format() — canonical display shape, with incremental typing mode
  • clean() — strip formatting and leading zeros down to digits
  • mask() — 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#

Generation & math#

Schemas & databases#

  • RUT_PATTERN — the accepted shapes as regex strings, for OpenAPI, JSON Schema, zod and database constraints

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

clean(), decompose(), getBody(), and getVerifier() normalize shape but do not check the verifier digit. Gate acceptance with validate().

See it run#

isRutLike()true
validate()true
validate({ strict: true })true
clean()123456785
format()12.345.678-5
decompose().body12345678
decompose().verifier5
calculateVerifier(body)5
verifier if input is a bodynot applicable