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

@ftapiat/js-rut-utils

v1.0.1

Published

Permite validar, limpiar y formatear RUT chilenos en Javascript

Downloads

30

Readme

JS Rut Utils

Librería para generar, validar, limpiar y dar formato a RUT chilenos.

Contenido

Cómo usarlo

Dependiendo de lo que quieras hacer, puedes llamar al modelo RutModel o directamente llamar las funciones de la carpeta utils.

RutModel

Representa a un RUT validado por su dígito verificador.

Propiedades de RutModel

Este RUT tiene 4 propiedades:

  1. number Número del RUT, requerido en el constructor.
  2. dv Dígito verificador, requerido en el constructor.
  3. formatted RUT formateado.
  4. cleaned RUT sin puntos ni guión.

Crear RutModel

  • La forma más básica de crear un RutModel es definiendo sus número y DV:
import {RutModel} from "@ftapia/js-rut-utils";

const rut = new RutModel(19101178, '3');
  • Sin embargo, no siempre se tiene esa información. Por lo que también puedes crear RUT de la siguiente forma:
import {RutModel} from "@ftapia/js-rut-utils";

// 1. Aleatorio:
const rutAleatorio = RutModel.random();

// 2. Desde el número del RUT. Esto generará automáticamente su DV:
const rutDesdeNumero = RutModel.fromNumber(19101178);

// 3. Desde el RUT como String. Esto limpiará el RUT y lo asignará
// automáticamente al modelo:
const rutDesdeString1 = RutModel.fromString('10.407.355-7'); // ✔ Funciona con puntos y guión.
const rutDesdeString2 = RutModel.fromString('10407355-7'); // ✔ Funciona sin puntos y con guión.
const rutDesdeString3 = RutModel.fromString('104073557'); // ✔ Funciona sin puntos ni guión.

RUT erróneos

Si se crea un RUT con datos erróneos, se lanzará el error RutIsMalformedError.

El RUT puede ser erróneo si:

  • El dígito verificador es inválido.
import {RutModel} from "@ftapia/js-rut-utils";

// ❌ Esto daría error
new RutModel(19101178, '5');
// ✔ Esto no
new RutModel(19101178, '3');
  • El número es menor a 1 millón.
import {RutModel} from "@ftapia/js-rut-utils";

// ❌ Esto daría error
new RutModel(111111, '6');
// ✔ Esto no
new RutModel(1111111, '4');
  • El número no puede ser un RUT falso con todos sus caracteres repetidos, como 11.111.111-1, 22.222.222-2, etc.
import {RutModel} from "@ftapia/js-rut-utils";

// ❌ Esto daría error
new RutModel(44444444, '4');
// ✔ Esto no
new RutModel(19101178, '3');

Funciones utils

Todas las características de RutModel se subdividieron en varias funciones en la carpeta utils:

Limpiar RUT

import {cleanRut} from "@ftapia/js-rut-utils";

// Limpia el RUT de puntos y guiones.
const rut = cleanRut('19.101.178-3'); // '191011783'

Formatear RUT

import {formatRut} from "@ftapia/js-rut-utils";

// Agrega puntos y guión al RUT
const rut = formatRut('191011783'); // '19.101.178-3'

Generar RUT aleatorio

import {generateRut} from "@ftapia/js-rut-utils";

const rut = generateRut(); // Generará un RUT con DV, SIN puntos ni guión.
const rut = generateRut(true); // Generará un RUT con DV, CON puntos y guión.

Generar DV a partir del número del RUT

import {generateDV} from "@ftapia/js-rut-utils";

const dv = generateDV(19101178); // '3'

Validación - Regex que comprueba que el RUT tenga puntos y guion y el largo de un RUT

import {rutForcedDotsAndDashRegex} from "@ftapia/js-rut-utils";

rutForcedDotsAndDashRegex.test('19.101.178-3'); // ✔ true
rutForcedDotsAndDashRegex.test('191011783'); // ❌ false
rutForcedDotsAndDashRegex.test('19101178-3'); // ❌ false
rutForcedDotsAndDashRegex.test('abcde'); // ❌ false

Validación - Regex que comprueba que el RUT tenga el largo de un RUT, independientemente de si tiene puntos y guiones

import {rutOptionalDotsAndDashRegex} from "@ftapia/js-rut-utils";

rutOptionalDotsAndDashRegex.test('19.101.178-3'); // ✔ true
rutOptionalDotsAndDashRegex.test('191011783'); // ✔ true
rutOptionalDotsAndDashRegex.test('19101178-3'); // ✔ true
rutOptionalDotsAndDashRegex.test('abcde'); // ❌ false

Validación - RUT es mayor o igual a 1 millón

import {isRutNumberOver1Million} from "@ftapia/js-rut-utils";

isRutNumberOver1Million(1000000); // ✔ true
isRutNumberOver1Million(11000000); // ✔ true
isRutNumberOver1Million(999999); // ❌ false

Validación - RUT tiene formato correcto, sobre 1 millón y su DV es válido

import {isRutValid} from "@ftapia/js-rut-utils";

isRutValid('10407355-7'); // ✔ true : Sin puntos con guión y DV válido.
isRutValid('19.982-6'); // ❌ false : Número menor a 1 millón.
isRutValid('44.444.444-4'); // ❌ false : Número repetido.

Validación - DV corresponde al calculado por el módulo 11

import {isRutValidInModule11} from "@ftapia/js-rut-utils";

isRutValidInModule11(19101178, '3'); // ✔ true  
isRutValidInModule11(19101178, '4'); // ❌ false  

Instalación

npm install --save @ftapiat/js-rut-utils

Testing

npm install
npm test