Skip to content

mask()

Masks a RUT for safe logging, keeping the leading group and verifier digit while hiding the middle digits.

mask() is built for anti-PII logging: it replaces the inner digits of a RUT with asterisks so you can record or display a value without leaking the full identifier. It normalizes the input first, so compact, hyphenated, and dotted shapes all work.

Basic Usage#

TypeScript
import { mask } from 'rut.ts'
 
mask('12.345.678-5') // '12.***.***-5'
mask('123456785') // '12.***.***-5'
mask('1.234.567-4') // '1.***.***-4'

Type Signature#

TypeScript
function mask(rut: string, options?: { throwOnError?: boolean }): string | null
 
type MaskOptions = {
  throwOnError?: boolean
}

Parameters#

  • rut (string) - The RUT string to mask. Accepts compact, hyphen, or dotted shapes.
  • options (optional) - Configuration options
    • throwOnError (boolean, default: true) - If false, returns null instead of throwing on invalid input

Return Value#

  • Default mode: Returns the masked RUT string, or throws InvalidRutError on invalid input
  • Safe mode: Returns the masked RUT string, or null if the input cannot be masked

Examples#

8-Digit Body#

TypeScript
import { mask } from 'rut.ts'
 
mask('12.345.678-5') // '12.***.***-5'

7-Digit Body#

TypeScript
import { mask } from 'rut.ts'
 
mask('1.234.567-4') // '1.***.***-4'

Compact and Hyphen Input#

The input is normalized before masking, so unformatted shapes produce the same masked output:

TypeScript
import { mask } from 'rut.ts'
 
mask('123456785') // '12.***.***-5' (compact)
mask('12345678-5') // '12.***.***-5' (compact + hyphen)
mask('12.345.678-5') // '12.***.***-5' (dotted)

Safe Mode#

TypeScript
import { mask } from 'rut.ts'
 
// Returns null instead of throwing
mask('not-a-rut', { throwOnError: false }) // null
 
const handleLog = (value: string) => {
  const masked = mask(value, { throwOnError: false })
  logger.info('rut', { rut: masked ?? 'unknown' })
}

Throwing on Invalid Input#

TypeScript
import { mask, InvalidRutError } from 'rut.ts'
 
try {
  mask('not-a-rut')
} catch (error) {
  if (error instanceof InvalidRutError) {
    console.error('Cannot mask invalid RUT')
  }
}

Masks Shape, Not the Checksum#

mask() hides the shape of the RUT; it does not verify the Modulo 11 verifier digit. A value with a wrong verifier is still masked, because masking is a presentation concern, not a validation step:

TypeScript
import { mask } from 'rut.ts'
 
// Wrong verifier (should be 5), but the shape is still masked
mask('12.345.678-0') // '12.***.***-0'

If you need to confirm the RUT is genuinely valid, run validate() before masking.

Use Cases#

  • ✅ Safe logging that avoids leaking full identifiers
  • ✅ Anti-PII display in dashboards and audit trails
  • ✅ Redacting RUTs in error reports and analytics
  • ✅ Showing partial identifiers in confirmation screens
  • ✅ Safe redaction with throwOnError: false
  • format() - Add canonical dot and hyphen formatting
  • clean() - Remove formatting to a normalized shape