validate-chilean-rut
v1.2.0
Published
Validate chilean ruts as easy as abc. A zero dependencies, lightweight library that normalized and validates chilean ruts. Written in typescript.
Downloads
437
Readme
validate-chilean-rut
Validate chilean ruts as easy as abc. A zero dependencies, lightweight library that normalized and validates chilean ruts. Written in typescript.
Getting started
Just install
with npm:
npm install validate-chilean-rut
yarn
yarn add validate-chilean-rut
or in a html environment with unpkg
<script crossorigin src="https://unpkg.com/validate-chilean-rut"><script/>
Documentation
validateRut: (rut: number | string) => boolean
Receives either a number or string rut and returns whether it is a valid rut or not
import { validateRut } from "validate-chilean-rut";
console.log(validateRut("96.591.760-8")); // true
console.log(validateRut("96591760-8")); // true
console.log(validateRut("965917608")); // true
console.log(validateRut(965917608)); // true
console.log(validateRut("96.591.760-7")); // false
clearRut: (rut: number | string) => string
Receives either a number or string rut and returns a normalized rut (string)
import { clearRut } from "validate-chilean-rut";
console.log(clearRut("96.591.760-8")); // "965917608"
console.log(clearRut("96591760-8")); // "965917608"
console.log(clearRut(965917608)); // "965917608"
getCheckDigit: (rutWithoutCheckDigit: number | string) => string
Receives rut body (everything but the "- checkDigit") either as a number or string rut and returns the check digit as string
import { getCheckDigit } from "validate-chilean-rut";
console.log(getCheckDigit("96.591.760")); // "8"
console.log(getCheckDigit(96591760)); // "8"
formatRut: (rut: number | string) => string
Receives either a number or string rut and returns a formatted rut (string) with dots and dash.
import { formatRut } from "validate-chilean-rut";
console.log(formatRut("96.591.760-8")); // "96.591.760-8"
console.log(formatRut("96591760-8")); // "96.591.760-8"
console.log(formatRut(965917608)); // "96.591.760-8"
If the rut is not valid, it will format it anyway.
Internal functions (you probably won't use them)
splitRut: (cleanRut: string) => { checkDigit: string; inverseRut: string[];}
Receives a normalized rut and returns the checkDigit and the inverse rut body as an string array
getRutSum: (stringDigitArray: string[]) => number
Receives the inverse rut body (string array) and returns the multiplication sum
getCheckDigitFromSum: (sum: number) => string
Receives the multiplication sum and returns the check digit