API Reference
Complete API documentation for all rut.ts functions.
Functions
Validation
validate(rut, options?)- Validates a RUT string
Formatting & Cleaning
format(rut, options?)- Formats a RUT stringclean(rut, options?)- Cleans a RUT string
Decomposition
decompose(rut, options?)- Splits RUT into body and verifiergetBody(rut, options?)- Extracts body onlygetVerifier(rut, options?)- Extracts verifier only
Generation & Calculation
generate()- Generates random valid RUTcalculateVerifier(body, options?)- Calculates verifier digit
Utilities
isRutLike(rut)- Quick format check
TypeScript Types
import type {
DecomposedRut,
FormatOptions,
SafeOptions,
ValidateOptions,
VerifierDigit
} from 'rut.ts'DecomposedRut
type DecomposedRut = {
body: string // RUT body (7-8 digits)
verifier: string // Verifier digit ('0'-'9' or 'K')
}FormatOptions
type FormatOptions = {
incremental?: boolean // Progressive formatting mode
dots?: boolean // Include dot separators (default: true)
throwOnError?: boolean // Return null on error (default: true)
}SafeOptions
type SafeOptions = {
throwOnError?: boolean // Return null on error (default: true)
}ValidateOptions
type ValidateOptions = {
strict?: boolean // Reject suspicious patterns (default: false)
}VerifierDigit
type VerifierDigit =
| '0' | '1' | '2' | '3' | '4'
| '5' | '6' | '7' | '8' | '9'
| 'K'Quick Reference
| Function | Input | Output | Safe Mode | Purpose |
|---|---|---|---|---|
validate() | RUT string | boolean | N/A | Check validity |
format() | RUT string | Formatted string | ✅ | Add formatting |
clean() | RUT string | Clean string | ✅ | Remove formatting |
decompose() | RUT string | { body, verifier } | ✅ | Split RUT |
getBody() | RUT string | Body string | ✅ | Extract body |
getVerifier() | RUT string | Verifier char | ✅ | Extract verifier |
calculateVerifier() | Body string | Verifier char | ✅ | Calculate verifier |
generate() | None | Valid RUT | N/A | Generate random |
isRutLike() | String | boolean | N/A | Format check |
Common Patterns
Validation Pipeline
import { isRutLike, validate, clean } from 'rut.ts'
function validateRutPipeline(input: unknown) {
// Step 1: Type check
if (typeof input !== 'string') return false
// Step 2: Format check (fast)
if (!isRutLike(input)) return false
// Step 3: Full validation (slower)
return validate(input)
}Safe Processing
import { clean, decompose, calculateVerifier } from 'rut.ts'
function safeProcessRut(input: string) {
const cleaned = clean(input, { throwOnError: false })
if (!cleaned) return null
const parts = decompose(cleaned, { throwOnError: false })
if (!parts) return null
return parts
}Format + Validate
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
}
}Last updated on