Skip to content

equals()

Answers "are these the same RUT?" across formatting shapes: both inputs are normalized and compared, and — by default since 5.0.0 — the shared value must also pass the Modulo 11 verifier check.

Two strings with a wrong verifier digit are not "the same RUT" because they are not RUTs at all: equals('12345678-9', '12345678-9') returns false. Pass { requireValid: false } to get the pre-5.0.0 pure shape comparison. equals() never throws, and it returns false whenever either input is not a RUT-shaped string, which makes it safe to call on raw or untrusted values.

Basic Usage#

TypeScript
import { equals } from 'rut.ts'
 
equals('12.345.678-5', '123456785') // true  (same valid RUT, different shapes)
equals('12.345.678-5', '12.345.679-3') // false (different RUTs)
equals('12345678-9', '12345678-9') // false (wrong verifier — not a RUT at all)
equals('12345678-9', '12345678-9', { requireValid: false }) // true (pure shape comparison)
equals('12.345.678-5', null) // false

Type Signature#

TypeScript
function equals(a: unknown, b: unknown, options?: EqualsOptions): boolean
 
type EqualsOptions = {
  requireValid?: boolean
}

Parameters#

  • a (unknown) - The first value to compare
  • b (unknown) - The second value to compare
  • options (optional) - Comparison options
    • requireValid (boolean, default: true) - If true, both values must also be a valid RUT (Modulo 11 checked on the normalized value). Set to false for the pre-5.0.0 pure normalization comparison.

Return Value#

Returns boolean:

  • true if both values normalize to the same RUT (and, by default, that RUT is valid)
  • false if they differ, if either is not RUT-shaped, if either is not a string, or — in default mode — if the shared value fails the verifier check

Examples#

Cross-Shape Equality#

The three accepted RUT shapes are treated as equal when they refer to the same RUT:

TypeScript
import { equals } from 'rut.ts'
 
equals('12.345.678-5', '123456785') // true (dotted vs compact)
equals('12.345.678-5', '12345678-5') // true (dotted vs compact + hyphen)
equals('12345678-5', '123456785') // true (compact + hyphen vs compact)

Validity Is Checked by Default (5.0.0)#

Since 5.0.0, the shared normalized value must pass Modulo 11. Two identical strings with a wrong verifier are no longer "equal":

TypeScript
import { equals } from 'rut.ts'
 
equals('12345678-9', '12345678-9') // false (wrong verifier)
equals('12.345.678-9', '123456789') // false (same wrong-verifier value across shapes)
 
// Legacy mode: pure shape comparison, byte-for-byte 4.x behavior
equals('12345678-9', '12345678-9', { requireValid: false }) // true

Use { requireValid: false } when deduplicating dirty datasets — the same typo in two rows is still the same entity.

Intended Asymmetry with validate()#

Validity is checked on the normalized value, not on validate()'s canonical shape contract. A zero-padded value still matches its canonical form, even though validate() rejects the padded shape:

TypeScript
import { equals, validate } from 'rut.ts'
 
equals('012345678-5', '12.345.678-5') // true  (normalizes to the same valid RUT)
validate('012345678-5') // false (leading zeros are not canonical)

This is deliberate: equals is a normalization operation — it inherits clean()'s permissiveness (leading zeros, separators, case) and adds only the verifier check. Requiring canonical shape would make it useless for the very thing it exists for: comparing different shapes of the same RUT.

Invalid or Non-String Inputs#

TypeScript
import { equals } from 'rut.ts'
 
equals('12.345.678-5', 'not-a-rut') // false (b not RUT-shaped)
equals('abc', 'def') // false (neither is RUT-shaped)
equals('12.345.678-5', null) // false (not a string)
equals(12345678, '12345678-5') // false (not a string)

Never Throws#

equals() is safe to call on any input. It returns false rather than throwing, so you can use it directly on raw values:

TypeScript
import { equals } from 'rut.ts'
 
const a: unknown = getUserInput()
const b: unknown = getStoredRut()
 
// No try/catch needed
if (equals(a, b)) {
  console.log('Same RUT')
}

Use Cases#

  • ✅ Comparing a submitted RUT against a stored value as a lookup key
  • ✅ Answering "is this the same person?" regardless of formatting
  • ✅ Safe comparison of untrusted input without try/catch
  • ✅ Deduplicating RUTs collected in mixed formats (use { requireValid: false } if the dataset may contain wrong-verifier typos that should still collapse together)
  • clean() - Normalize a single RUT to compact shape
  • validate() - Validate a RUT including the verifier digit