API Reference
The complete public surface. Every throwing function accepts { throwOnError: false } to return null instead of throwing.
Functions#
Validation#
| Function | Signature | Notes |
|---|---|---|
validate() | validate(rut: unknown, options?: ValidateOptions): boolean | Shape + Modulo 11 verifier; strict rejects placeholders |
isRutLike() | isRutLike(rut: unknown): boolean | Bounded shape check, no verifier math |
Formatting & cleaning#
| Function | Signature | Safe mode |
|---|---|---|
format() | format(rut: string, options?: FormatOptions): string | ✅ |
clean() | clean(rut: string, options?: SafeOptions): string | ✅ |
Decomposition#
| Function | Signature | Safe mode |
|---|---|---|
decompose() | decompose(rut: string, options?: SafeOptions): DecomposedRut | ✅ |
getBody() | getBody(rut: string, options?: SafeOptions): string | ✅ |
getVerifier() | getVerifier(rut: string, options?: SafeOptions): VerifierDigit | ✅ |
Generation & math#
| Function | Signature | Safe mode |
|---|---|---|
generate() | generate(): string | N/A |
calculateVerifier() | calculateVerifier(rutBody: string, options?: SafeOptions): VerifierDigit | ✅ |
TypeScript types#
TypeScript
import type {
DecomposedRut,
FormatOptions,
SafeOptions,
ValidateOptions,
VerifierDigit,
} from 'rut.ts'DecomposedRut#
TypeScript
type DecomposedRut = {
body: string // RUT body (7-8 digits, no leading zeros)
verifier: string // Verifier digit ('0'-'9' or 'K')
}Returned by decompose().
FormatOptions#
TypeScript
type FormatOptions = {
incremental?: boolean // Progressive formatting (default: false)
dots?: boolean // Include dot separators (default: true)
throwOnError?: boolean // Throw vs. return null (default: true)
}Used by format().
SafeOptions#
TypeScript
type SafeOptions = {
throwOnError?: boolean // Throw vs. return null (default: true)
}Used by clean(), decompose(), getBody(), getVerifier(), calculateVerifier().
ValidateOptions#
TypeScript
type ValidateOptions = {
strict?: boolean // Reject suspicious placeholders (default: false)
}Used by validate().
VerifierDigit#
TypeScript
type VerifierDigit =
| '0' | '1' | '2' | '3' | '4' | '5'
| '6' | '7' | '8' | '9' | 'K'Quick reference#
| Function | Input | Output | Safe mode | Purpose |
|---|---|---|---|---|
validate() | unknown | boolean | N/A | Accept / reject (shape + verifier) |
isRutLike() | unknown | boolean | N/A | Fast bounded shape check |
format() | RUT string | formatted string | ✅ | Canonical display; validates in default mode |
clean() | RUT string | digit string | ✅ | Normalize shape (no verifier check) |
decompose() | RUT string | { body, verifier } | ✅ | Split into parts |
getBody() | RUT string | body string | ✅ | Body only |
getVerifier() | RUT string | verifier char | ✅ | Verifier only |
calculateVerifier() | body string | verifier char | ✅ | Derive the Modulo 11 digit |
generate() | none | valid RUT | N/A | Random valid RUT for tests |
Common patterns#
Validation pipeline#
TypeScript
import { isRutLike, validate } from 'rut.ts'
function validateRutPipeline(input: unknown): boolean {
if (typeof input !== 'string') return false
if (!isRutLike(input)) return false // cheap reject
return validate(input, { strict: true }) // authoritative gate
}For production identity flows, validate(input, { strict: true }) is the
acceptance gate. clean() normalizes shape but does not prove the
verifier is correct — see Security.
Safe processing#
TypeScript
import { clean, decompose } from 'rut.ts'
function safeProcessRut(input: string) {
const cleaned = clean(input, { throwOnError: false })
if (!cleaned) return null
return decompose(cleaned, { throwOnError: false })
}Format + validate#
TypeScript
import { format, validate } from 'rut.ts'
function formatAndValidate(input: string) {
const formatted = format(input, { throwOnError: false })
if (!formatted) return { valid: false, formatted: null }
return { valid: validate(formatted), formatted }
}