Skip to content

Examples

Real-world, copy-pasteable patterns. Each page is self-contained — start with Basic Usage, then jump to whichever integration you need.

Three patterns you'll reuse everywhere#

Validate, then format#

TypeScript
import { validate, format } from 'rut.ts'
 
function processRut(input: string): string {
  if (!validate(input)) throw new Error('Invalid RUT')
  return format(input)
}

Clean, then validate#

TypeScript
import { clean, validate } from 'rut.ts'
 
function normalizeAndValidate(input: string): boolean {
  const cleaned = clean(input, { throwOnError: false })
  return cleaned ? validate(cleaned) : false
}

Decompose, then verify#

TypeScript
import { decompose, calculateVerifier } from 'rut.ts'
 
function verifyRut(rut: string): boolean {
  const { body, verifier } = decompose(rut)
  return calculateVerifier(body) === verifier
}

Across every example: format() for display, validate({ strict: true }) to accept, clean() to store.