1.0.5 • Published 8 months ago

rutlib v1.0.5

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
8 months ago

RUTlib

NPM

GitHub license

A JavaScript date library for parsing, validating, manipulating, generating and formatting RUTs (chilean identification).

Quick Installation

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

Usage

Clean a RUT

This function takes a RUT as a string and removes all non-numeric characters, and transforms the input to uppercase.

FunctionParamsReturnDescription
cleanRutRUT (String)StringRUT without non-numeric characters
import { cleanRut } from 'rutlib';

cleanRut('1.234-3'); // 12343
cleanRut('1234-3'); // 12343
cleanRut('12343'); // 12343

Validate a RUT

This function checks whether a RUT is valid according to the RUT verification rules. Returns true if the RUT is valid and false otherwise.

FunctionParamsReturnDescription
validateRutRUT (String)BooleanReturn 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

Get Verificator Digit of a number

This function receives the numeric part of the RUT and calculates the corresponding verification digit.

FunctionParamsReturnDescription
getLastDigitOfRutRUT without VD (Number)StringReturn RUT last verificator digit
import { getLastDigitOfRut } from 'rutlib';

getLastDigitOfRut(1234); // 3
getLastDigitOfRut(1235); // 1
getLastDigitOfRut(1236); // K

Format RUT

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.

FunctionParamsReturnDescription
formatRutrut (String) withDots:True (optional) (Bolean)StringReturn 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

Get random RUT

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.

FunctionParamsReturnDescription
generateRutlength:8 (optional) (Number)formated:True (optional) (boolean)StringReturn 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)

Compare two RUTs

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.

FunctionParamsReturnDescription
compareRutsrut1 (String), rut2 (String)BooleanReturn 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
}

Support the Project

"Buy Me A Coffee"

If you'd like to contribute

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.

More information

For more information about the RUT, you can consult the following link: RUT (Chile)


Version 1.0.4

1.0.5

8 months ago

1.0.4

8 months ago

1.0.3

12 months ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago