npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rutlib

v1.0.5

Published

Provides a fast and robust RUT validator/generator

Downloads

7,758

Readme

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.

| 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

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.

| 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

Get Verificator Digit of a number

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

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.

| 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

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.

| 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)

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.

| 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
}

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