@cristiansantana/chile-rut
Version:
Javascript (Typescript ready) chilean RUT toolset: Validate, generate and verify format.
83 lines (58 loc) • 2.06 kB
Markdown

Chile-Rut is a Javascript (Typescript ready) package for working with the Chilean identification number (known as RUT or RUN).
## Features
- Validate a RUT (Chilean identification number) using its check digit.
- Obtain the check digit for a RUT.
- Verify if the format of a RUT is correct.
## Installation
```sh
npm install @cristiansantana/chile-rut
```
## Usage
### Get CheckDigit
Use the `getCheckDigit` function with a RUT Id as parameter.
#### Example without format validation
```js
import { getCheckDigit } from "@cristiansantana/chile-rut";
const rutId = "12345678";
try {
const digit = getCheckDigit(rutId);
console.log(digit); // -> output expected: 5
} catch (error) {
// Error: rutId has non valid format
// ...
}
```
```js
import { getCheckDigit, validateRutIdFormat } from "@cristiansantana/chile-rut";
const rutId = "12345678";
if (validateRutIdFormat(rutId)) {
const digit = getCheckDigit(rutId);
console.log(digit); // -> output expected: 5
}
else {
// Error: rutId has non valid format
// ...
}
```
`getCheckDigit` support the parameter rutId as:
- "12345678"
- "12.345.678"
- "12,345,678"
- "012.345.678"
Use the `validateRut` function with a RUT as parameter for validate it. This function **does not test/verify the actual existence of the RUT**, it only checks that it is well-formed
```js
import { validateRut } from "@cristiansantana/chile-rut";
console.log(validateRut("12345678-5")); // -> output expected: true
console.log(validateRut("12345678-6")); // -> output expected: false
```
`validateRut` support the parameter rut as:
- "12345678-5" <- without thousands separator
- "12.345.678-5" <- dots as the thousands separator
- "12,345,678-5" <- commas as the thousands separator
- "012345678-5" <- leading with zeros (also work with thousands separator)