Skip to content

generate()

Generates random valid RUT numbers for testing and development purposes.

Basic Usage#

TypeScript
import { generate } from 'rut.ts'
 
const randomRut = generate()
console.log(randomRut) // '18.972.631-7'

Type Signature#

TypeScript
function generate(): string
function generate(options: GenerateOptions & { count: number }): string[]
function generate(options?: GenerateOptions): string
 
type GenerateOptions = {
  bodyLength?: 7 | 8
  format?: 'dotted' | 'compact' | 'hyphen'
  count?: number
}

Parameters#

  • options (optional) - Generation options
    • bodyLength (7 | 8, default: 8) - Number of digits in the generated body
    • format ('dotted' | 'compact' | 'hyphen', default: 'dotted') - Output shape:
      • 'dotted'12.345.678-5
      • 'compact'123456785
      • 'hyphen'12345678-5
    • count (number) - When provided, returns an array of that many RUTs (string[]) instead of a single string

Calling generate() with no arguments is unchanged: it returns one valid 8-digit RUT in 'dotted' format.

Return Value#

Returns a string containing:

  • A randomly generated RUT
  • Already formatted with dots and hyphen (XX.XXX.XXX-Y)
  • Guaranteed to be valid (passes validate())
  • Body in range: 10,000,000 - 99,999,999
  • Non-suspicious body (repeated-digit placeholders are skipped)

Examples#

Generate Single RUT#

TypeScript
import { generate } from 'rut.ts'
 
const rut = generate()
console.log(rut) // e.g., '45.123.789-2'

Choose an Output Format#

TypeScript
import { generate } from 'rut.ts'
 
generate({ format: 'compact' }) // '233715913'
generate({ format: 'hyphen' })  // '23371591-3'
generate({ format: 'dotted' })  // '23.371.591-3' (default)

Shorter Body#

TypeScript
import { generate } from 'rut.ts'
 
// 7-digit body
generate({ bodyLength: 7, format: 'hyphen' }) // '7788862-4'
generate({ bodyLength: 7 })                   // '7.788.862-4'

Generate Multiple RUTs#

TypeScript
import { generate } from 'rut.ts'
 
// Pass `count` to get an array (string[])
const ruts = generate({ count: 3 })
console.log(ruts)
// ['18.972.631-7', '45.123.789-2', '23.371.591-3']
 
// `count` combines with the other options
generate({ count: 3, format: 'compact' })
// ['233715913', '451237892', '189726317']

You can still build arrays manually if you prefer:

TypeScript
import { generate } from 'rut.ts'
 
const ruts = Array.from({ length: 10 }, () => generate())
console.log(ruts)
// ['18.972.631-7', '45.123.789-2', ...]

Generate for Testing#

TypeScript
import { generate, validate } from 'rut.ts'
 
// Generate test data
const testRuts = Array.from({ length: 100 }, () => generate())
 
// All generated RUTs are valid
testRuts.forEach((rut) => {
  console.assert(validate(rut) === true)
})

Seed Test Database#

TypeScript
import { generate, decompose } from 'rut.ts'
 
// Create test users with valid RUTs
const testUsers = Array.from({ length: 50 }, (_, i) => {
  const rut = generate()
  const { body, verifier } = decompose(rut)
 
  return {
    id: i + 1,
    name: `Test User ${i + 1}`,
    rut: rut,
    rutBody: body,
    rutVerifier: verifier,
  }
})
 
await db.users.insertMany(testUsers)

Properties#

All generated RUTs have these properties:

  • Valid: Pass validate() without errors
  • Formatted: Default output includes dots and hyphen (XX.XXX.XXX-Y); configurable via format
  • Realistic: Body in range 10,000,000 - 99,999,999 (default 8-digit body)
  • Random: Different on each call
  • Non-suspicious: Pass validate(rut, { strict: true })
  • Configurable body: 8-digit body by default, or 7-digit via bodyLength: 7

Format#

Generated RUTs follow this pattern:

Code
XX.XXX.XXX-Y
││  │   │  └─ Verifier digit (0-9 or K)
││  │   └──── Last 3 digits of body
││  └──────── Middle 3 digits of body
│└─────────── First 2 digits of body
└──────────── Always 8 digits total in body

Use Cases#

  • ✅ Generate test data for unit tests
  • ✅ Populate development databases
  • ✅ Create sample data for demos
  • ✅ Seed databases with realistic RUTs
  • ✅ Stress testing validation logic

Examples in Testing#

TypeScript
import { generate, validate } from 'rut.ts'
 
describe('RUT validation', () => {
  test('validates 1000 generated RUTs', () => {
    const ruts = Array.from({ length: 1000 }, () => generate())
 
    ruts.forEach((rut) => {
      expect(validate(rut)).toBe(true)
    })
  })
 
  test('generated RUTs pass strict validation', () => {
    const ruts = Array.from({ length: 100 }, () => generate())
 
    ruts.forEach((rut) => {
      expect(validate(rut, { strict: true })).toBe(true)
    })
  })
})

Notes#

  • Each call generates a different RUT (random)
  • Generated RUTs use realistic number ranges
  • Verifier is automatically calculated using Modulo 11
  • Output is dotted by default; use format for 'compact' or 'hyphen'
  • Pass count to get an array (string[]) instead of a single string
  • Web Crypto is used when available; older runtimes fall back to Math.random()
  • Generated RUTs are for tests, demos, and development data. Do not use them to assign real identities.