A JavaScript date library for parsing, validating, manipulating, generating and formatting RUTs (chilean identification).
npm i rutlib
The module exports the following functions:
- cleanRut(rut: string): string
- validateRut(rut: string): boolean
- getLastDigitOfRut(rutNumbers: number): string
- formatRut(rut: string, withDots: boolean = true): string
- generateRut(length: number = 8, formatted: boolean = true): string
- compareRuts(rut1: string, rut2 : string )): boolean
This function takes a RUT as a string and removes all non-numeric characters, and transforms the input to uppercase.
Function | Params | Return | Description |
---|---|---|---|
cleanRut | RUT (String) | String | RUT without non-numeric characters |
import { cleanRut } from 'rutlib';
cleanRut('1.234-3'); // 12343
cleanRut('1234-3'); // 12343
cleanRut('12343'); // 12343
This function checks whether a RUT is valid according to the RUT verification rules. Returns true if the RUT is valid and false otherwise.
Function | Params | Return | Description |
---|---|---|---|
validateRut | RUT (String) | Boolean | Return true if RUT is valid, false otherwisecharacters |
import { validateRut } from 'rutlib';
validateRut('1.234-3'); // true
validateRut('1234-3'); // true
validateRut('12343'); // true
validateRut('1.234-0'); // false
validateRut('1234-0'); // false
validateRut('12340'); // false
This function receives the numeric part of the RUT and calculates the corresponding verification digit.
Function | Params | Return | Description |
---|---|---|---|
getLastDigitOfRut | RUT without VD (Number) | String | Return RUT last verificator digit |
import { getLastDigitOfRut } from 'rutlib';
getLastDigitOfRut(1234); // 3
getLastDigitOfRut(1235); // 1
getLastDigitOfRut(1236); // K
This function takes a RUT and formats it according to the RUT format conventions, with the option to add or not separation points every three digits.
Function | Params | Return | Description |
---|---|---|---|
formatRut | rut (String) withDots:True (optional) (Bolean) |
String | Return RUT formatted |
import { formatRut } from 'rutlib';
formatRut('1.234-3'); // 1.234-3
formatRut('1234-3'); // 1.234-3
formatRut('12343'); // 1.234-3
formatRut('1.234-3', false); // 1234-3
formatRut('1234-3', false); // 1234-3
formatRut('12343', false); // 1234-3
This function generates a valid RUT randomly. You can specify the length of the RUT number and whether the generated RUT should be formatted or not.
Function | Params | Return | Description |
---|---|---|---|
generateRut | length:8 (optional) (Number) formated:True (optional) (boolean) |
String | Return RUT generated |
import { generateRut } from 'rutlib';
generateRut(); // 12.345.678-5 (random)
generateRut(); // 87.654.321-4 (random)
generateRut(7); // 1.234.567-4 (random)
generateRut(7); // 7.654.321-6 (random)
generateRut(9, false); // 123456789-2 (random)
generateRut(9, false); // 987654321-1 (random)
This function compares two RUTs. It takes two RUTs as strings, cleans them using the cleanRut
function, validates them using the validateRut
function, and then compares them. If both RUTs are valid and are the same, it returns true; otherwise, it returns false. If any of the RUTs is invalid, it will throw an error.
Function | Params | Return | Description |
---|---|---|---|
compareRuts | rut1 (String), rut2 (String) | Boolean | Return true if both RUTs are valid and are the same, false otherwise. Throws an error if any of the RUTs is invalid. |
import { compareRuts } from 'rutlib';
try {
const result = compareRuts('12.345.678-5', '12345678-5');
console.log(result); // Will print true if the RUTs are the same and valid
} catch (error) {
console.error(error.message); // Will print the error message if any of the RUTs is invalid
}
If you want to contribute to this module, you can do so by creating Issues in the repository or through Pull Requests. Remember to follow the code of conduct and best practices for clean code.
It is important to mention that any change in the logic of the module's functions should be properly tested and documented.
For more information about the RUT, you can consult the following link: RUT (Chile)
Version 1.0.4