Skip to content

arrowsw/rut.ts

Repository files navigation

Rut.ts logo

Rut.ts: Handle chilean RUT values with ease using TypeScript.

npm version downloads bundle size types included zero dependencies license

The complete, security-hardened toolkit for the Chilean RUT (Rol Único Tributario): validate, format, clean, decompose and generate β€” with a correctness contract you can rely on in production.

  • πŸͺΆ Tiny & zero-dependency β€” tree-shakeable ESM, ships only what you import.
  • πŸ”’ Hardened by default β€” bounded parsing, strict mode, generic errors. No ID values leak into logs or traces.
  • 🧠 Fully typed β€” first-class TypeScript types, no @types package needed.
  • 🌐 Universal β€” runs in Node, the browser, Deno and Bun. Uses Web Crypto when available.
  • βœ… Battle-tested β€” a differential harness guards every release against regressions.

Installation

npm install rut.ts
# or: bun add rut.ts Β· pnpm add rut.ts Β· yarn add rut.ts

Quick start

import { validate, format, clean, decompose, isRutLike } from 'rut.ts'

// Validate β€” strict mode also rejects suspicious placeholder RUTs
validate('12.345.678-5') // true
validate('12.345.678-0') // false (wrong verifier)
validate('11.111.111-1', { strict: true }) // false (suspicious)

// Format β€” accepts compact or formatted input
format('123456785') // '12.345.678-5'
format('123456785', { dots: false }) // '12345678-5'
format('123456789', { throwOnError: false }) // null (wrong verifier)

// Format progressively as the user types (great for form inputs)
format('1234', { incremental: true }) // '1.234'
format('123456785', { incremental: true }) // '12.345.678-5'

// Clean & decompose
clean('12.345.678-5') // '123456785'
decompose('12.345.678-5') // { body: '12345678', verifier: '5' }

// Cheap shape check, no full validation
isRutLike('12.345.678-5') // true

// Safe mode everywhere β€” return null instead of throwing
format('abc', { throwOnError: false }) // null

πŸ“š Full guides and live examples: rut.arrowsw.com

Features

  • Validation β€” verifier check with bounded input parsing and an optional strict mode that rejects placeholder/repeated-digit RUTs.
  • Formatting β€” standardized output, with or without dots.
  • Incremental formatting β€” progressive formatting as the user types, ideal for form inputs.
  • Cleaning β€” permissively strip extraneous characters and leading zeros.
  • Decomposition β€” split a RUT into its body and verifier digit.
  • Generation β€” cryptographically-backed random valid RUTs for tests (Web Crypto when available).
  • Calculate verifier β€” compute the verifier digit for a given body.
  • Format detection β€” cheap isRutLike check without full validation.
  • Safe mode β€” every safe function supports throwOnError: false to return null instead of throwing.
New to RUTs? What the format means

The RUT (Rol Único Tributario) is the unique Chilean identification number used for tax, legal identification, government services, and banking.

Format: XX.XXX.XXX-Y

  • X = Body (7–8 digits)
  • Y = Verifier digit (0–9 or K)

Example: 12.345.678-5

The verifier digit is derived from the body via the Modulo 11 algorithm, which is what makes a RUT self-validating.

Security & correctness

rut.ts treats RUT validation as an identity-security boundary, not just string formatting. That posture is the point of the library:

  • validate(input, { strict: true }) is the recommended acceptance gate for identity-sensitive flows. It rejects malformed dot grouping, caps oversized inputs before parsing, rejects repeated-digit placeholders, and compares the verifier via Modulo 11.
  • Errors are generic (Invalid RUT input) so Chilean ID values never end up echoed into logs, traces, or user-visible exceptions.
  • clean() is intentionally permissive β€” useful for display/storage normalization, but it does not prove the verifier is correct. Always validate() before accepting a RUT.

Accepted input formats (the validation contract)

validate() and isRutLike() accept only these shapes (optionally with leading zeros and surrounding whitespace, verifier k/K case-insensitive):

Shape Example Notes
Compact 123456785 7–8 digit body + verifier
Compact + hyphen 12345678-5
Canonical dotted 12.345.678-5, 1.234.567-4 Chilean grouping from the right

Anything else is rejected, including non-canonical dot grouping that older versions accepted (12.345678-5, 12345.678-5, 1.2.3.4-5), internal spaces, commas, and any input longer than 64 chars.

The 64-char limit is a security bound, not a format rule. A real RUT is ~9 significant characters, so the cap never rejects a realistic RUT β€” it just refuses to process implausibly long strings, neutralizing CPU/ReDoS-style abuse before any parsing runs.

πŸ’‘ Migrating a dataset? If your upstream emits RUTs in a non-canonical shape, normalize to one of the three accepted forms before calling validate(), or sanity-check a representative sample with npm run test:differential (writes tests/differential-report.md). clean() / decompose() stay permissive β€” never treat their output as "validated".

Incremental formatting

format(input, { incremental: true }) formats a RUT progressively as the user types β€” ideal for real-time feedback in form fields.

const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
  setRut(format(e.target.value, { incremental: true }))
}

Use it for: real-time input formatting and visual feedback. Don't use it for: validating, or formatting already-complete/stored RUTs (use format() / validate()). Incremental output may not be a valid RUT until the input is complete β€” always validate() the final value.

TypeScript types

import type { DecomposedRut, FormatOptions, SafeOptions, ValidateOptions, VerifierDigit } from 'rut.ts'

// VerifierDigit:  '0' | '1' | … | '9' | 'K'
// DecomposedRut:  { body: string; verifier: string }
// FormatOptions:  { incremental?: boolean; dots?: boolean; throwOnError?: boolean }
// ValidateOptions:{ strict?: boolean }
// SafeOptions:    { throwOnError?: boolean }

Upgrading from v3

v4 hardens validation for production identity flows and tightens the accepted input contract (see the table above). If you're coming from 3.x, the CHANGELOG lists every change and how to migrate β€” most codebases only need to normalize input shape before validate().

Contributing

Contributions are welcome β€” feel free to open issues for bugs and feature requests, or submit a pull request.

License

MIT Β© rut.ts contributors

About

πŸ‡¨πŸ‡± Handle chilean RUT values with ease using TypeScript

Topics

Resources

License

Stars

Watchers

Forks

Contributors