Skip to content

API Reference

The complete public surface. Every throwing function accepts { throwOnError: false } to return null instead of throwing.

Functions#

Validation#

FunctionSignatureNotes
validate()validate(rut: unknown, options?: ValidateOptions): booleanShape + Modulo 11 verifier; strict rejects placeholders
isValidRut()isValidRut(rut: unknown, options?: { strict?: boolean }): rut is RutType guard narrowing to the branded Rut
isRutLike()isRutLike(rut: unknown): booleanBounded shape check, no verifier math

Formatting & cleaning#

FunctionSignatureSafe mode
format()format(rut: string, options?: FormatOptions): string
clean()clean(rut: string, options?: SafeOptions): string
mask()mask(rut: string, options?: { throwOnError?: boolean }): string | null

Comparison#

FunctionSignatureNotes
equals()equals(a: unknown, b: unknown, options?: EqualsOptions): booleanNormalized cross-shape comparison; checks Modulo 11 by default ({ requireValid: false } for pure shape comparison)

Decomposition#

FunctionSignatureSafe mode
decompose()decompose(rut: string, options?: SafeOptions): DecomposedRut
getBody()getBody(rut: string, options?: SafeOptions): string
getVerifier()getVerifier(rut: string, options?: SafeOptions): VerifierDigit

Generation & math#

FunctionSignatureSafe mode
generate()generate(options?: GenerateOptions): string | string[]N/A
calculateVerifier()calculateVerifier(rutBody: string, options?: SafeOptions): VerifierDigit

TypeScript types#

TypeScript
import type {
  DecomposedRut,
  EqualsOptions,
  FormatOptions,
  GenerateOptions,
  Rut,
  SafeOptions,
  ValidateOptions,
  VerifierDigit,
} from 'rut.ts'

Rut#

TypeScript
type Rut = string & { readonly __brand: 'Rut' } // branded validated-RUT string

A branded type representing a validated RUT string. Obtain one by narrowing with isValidRut().

DecomposedRut#

TypeScript
type DecomposedRut = {
  body: string            // RUT body (7-8 digits, no leading zeros)
  verifier: VerifierDigit // Verifier digit ('0'-'9' or 'K')
}

Returned by decompose().

EqualsOptions#

TypeScript
type EqualsOptions = {
  requireValid?: boolean // Require the shared value to pass Modulo 11 (default: true)
}

Used by equals(). { requireValid: false } restores the pre-5.0.0 pure normalization comparison.

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

GenerateOptions#

TypeScript
type GenerateOptions = {
  bodyLength?: 7 | 8                          // Body digit count (default: 7 or 8)
  format?: 'dotted' | 'compact' | 'hyphen'    // Output shape (default: 'dotted')
  count?: number                              // Return an array of this many RUTs
}

Used by generate(). When count is given, generate() returns string[]; otherwise it returns a single string.

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'

Errors#

InvalidRutError#

TypeScript
import { InvalidRutError } from 'rut.ts'
 
class InvalidRutError extends Error {
  readonly code: 'INVALID_RUT'
  // message: 'Invalid RUT input'
}

Thrown by the safe helpers in their default mode (throwOnError: true) when the input is not a valid RUT. Detect it with instanceof rather than matching the message string.

TypeScript
import { clean, InvalidRutError } from 'rut.ts'
 
try {
  clean('not-a-rut')
} catch (error) {
  if (error instanceof InvalidRutError) {
    // error.code === 'INVALID_RUT'
  }
}

getInvalidRutError() was removed in 5.0.0 (deprecated since 4.1.0). Catch InvalidRutError (instanceof) or branch on err.code === 'INVALID_RUT' instead; the constant message is new InvalidRutError().message if you truly need the text.

Quick reference#

FunctionInputOutputSafe modePurpose
validate()unknownbooleanN/AAccept / reject (shape + verifier)
isValidRut()unknownrut is RutN/AType guard narrowing to branded Rut
isRutLike()unknownbooleanN/AFast bounded shape check
format()RUT stringformatted stringCanonical display; validates in default mode
clean()RUT stringdigit stringNormalize shape (no verifier check)
mask()RUT stringmasked string | nullPartially hide a RUT for display
equals()unknownbooleanN/ASame-RUT comparison (validity-checked by default)
decompose()RUT string{ body, verifier }Split into parts
getBody()RUT stringbody stringBody only
getVerifier()RUT stringverifier charVerifier only
calculateVerifier()body stringverifier charDerive the Modulo 11 digit
generate()optionsvalid RUT(s)N/ARandom valid RUT(s) 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 }
}