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#

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().

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