Skip to content

getBody()

Extracts just the body (without verifier digit) from a RUT string.

Basic Usage#

TypeScript
import { getBody } from 'rut.ts'
 
getBody('12.345.678-5')  // '12345678'
getBody('9068826K')      // '9068826'

Type Signature#

TypeScript
function getBody(rut: string): string
function getBody(rut: string, options: { throwOnError: false }): string | null
function getBody(rut: string, options: { throwOnError: true }): string

Parameters#

  • rut (string) - The RUT string
  • options (optional)
    • throwOnError (boolean, default: true) - If false, returns null instead of throwing

Return Value#

  • Default mode: Returns body string (7-8 digits) or throws a generic Invalid RUT input error
  • Safe mode: Returns body string or null if invalid

v4.0.0: non-string inputs (numbers, null, undefined, objects) are treated like any other invalid input — they return null in safe mode and throw the generic error otherwise, instead of throwing a TypeError.

Examples#

Basic Extraction#

TypeScript
import { getBody } from 'rut.ts'
 
getBody('12.345.678-5')    // '12345678'
getBody('18.972.631-7')    // '18972631'
getBody('9.068.826-K')     // '9068826'

Safe Mode#

TypeScript
import { getBody } from 'rut.ts'
 
const body = getBody('invalid', { throwOnError: false })
console.log(body)  // null

Use Cases#

  • ✅ Extract body for database queries
  • ✅ Compare RUT bodies (ignoring verifier)
  • ✅ Process body separately
  • ✅ Quick body extraction without full decomposition