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

rut-chile

v1.0.2

Published

Comprehensive Chilean RUT validation and verification toolkit for JavaScript and TypeScript. Easily validate RUT format, calculate check digits, and ensure data accuracy in your applications.

Downloads

44

Readme

Rut-Chile

Comprehensive RUT Validation and Utilities for JavaScript and TypeScript

Node.js CI

Rut-Chile is a comprehensive package for working with Chilean RUTs (Rol Único Tributario) in JavaScript and TypeScript. It provides a robust validation mechanism that goes beyond the standard verification digit check, ensuring the RUT adheres to all structural and format requirements. Additionally, it offers versatile formatting and de-formatting functionalities with control over case sensitivity and hyphen.

Key Features:

  • Rigorous Validation:

    • Validates the verification digit using the Chilean RUT algorithm.
    • Checks for invalid characters.
    • Ensures proper spacing and dash placement.
    • Verifies valid RUT lenght.
    • validateWithResponse function provides a detailed response object including validation status, message, and validity flag (boolean).
  • Flexible Formatting:

    • Formats RUTs to various standard formats with configurable separators (dot or comma).
    • Case Sensitivity Control: Formats the RUT output (including the verification digit 'K') in uppercase or lowercase as desired.
  • De-formatting Options:

    • Removes formatting characters and dashes if desired (123456789 or 12345678-9).
    • Extracts RUT from formatted strings.
  • TypeScript Support:

    • Seamless integration with TypeScript projects.

Installation:

npm install rut-chile

Usage:

validate(rut: string): boolean and validateWithResponse(rut: string): RUTResult

These methods validate a Chilean RUT (Rol Único Tributario). Checks for invalid characters, spaces or improper length. And finally check if the verification digit is valid. All-in-One RUT validation!

import RUT from 'rut-chile';

const rut = '10766555-2';

// Rut-Chile also accepts RUTs in these formats:
// 10.766.555-2 (Dotted format)
// 10,766,555-2 (Comma format)
// 107665552 (Unformatted, without separators and without hyphen)
// Accepts lowercase or uppercase for RUTs with 'k'.

console.log(RUT.validate(rut)) // Output: true
console.log(RUT.validateWithResponse(rut)) // RUTResult Object Output: { status: success, message: 'Valid RUT', payload: true}

const badRut = '10766555-k'; // Wrong verification digit

console.log(RUT.validate(badRut)) // Output: false
console.log(RUT.validateWithResponse(badRut)) // RUTResult Object Output: { status: error, message: 'Invalid check digit', payload: false}

getDigit(rutWithoutDigit: string, toUppercase = false): string

Returns a RUT verification digit. Can be returned with uppercase in case of 'k' if desired. Note: it does not check for invalid characters.

import RUT from 'rut-chile';

const rutWithoutDigit = '16591919';

// Rut-Chile also accepts RUTs in these formats:
// 16.591.919 (Dotted format)
// 16,591,919 (Comma format)
// 16591919 (Unformatted)

console.log(RUT.getDigit(rutWithoutDigit)) // Output: k
console.log(RUT.getDigit(rutWithoutDigit, true)) // Output: K

checkDigit(rut: string): boolean

Returns a boolean indicating whether the check digit is valid or not. Note: it does not check for invalid characters.

import RUT from 'rut-chile';

const rut = '10766555-2';

// Rut-Chile also accepts RUTs in these formats:
// 10.766.555-2 (Dotted format)
// 10,766,555-2 (Comma format)
// 107665552 (Unformatted)
// Accepts lower and uppercase in case of 'k'

console.log(RUT.checkDigit(rut)) // Output: true

const badRut = '10766555-3'; // bad verification digit
console.log(RUT.checkDigit(badRut)) // Output: false

format(rut: string, withComma = false, toUpperCase = false): string

Returns the formatted RUT number with dots and dash. Can be returned with comma and uppercase if desired too. Note: it does not check for invalid characters.

import RUT from 'rut-chile';

const rut = '16591919k';

// Rut-Chile also accepts RUTs in these formats:
// 16591919-k (Unformatted, only with dash)
// Accepts lowercase and uppercase in case of 'k'

console.log(RUT.format(rut)) // Output: 16.591.919-k
console.log(RUT.format(rut, true)) // Output: 16,591,919-k
console.log(RUT.format(rut, true, true)) // Output: 16,591,919-K

deformat(rut: string, noDash = false, toUpperCase = false): string

Returns the RUT number without dots or commas. Can be returned without dash and with uppercase if desired too. Note: it does not check for invalid characters.

import RUT from 'rut-chile';

const rut = '16.591.919-k';

// Rut-Chile also accepts RUTs in these formats:
// 16,591,919-k (Comma format)
// Accepts lowercase or uppercase in case of 'k'.

console.log(RUT.deformat(rut)) // Output: 16591919-k
console.log(RUT.deformat(rut, true)) // Output: 16591919k
console.log(RUT.deformat(rut, true, true)) // Output: 16591919K

Individual Validations:

hasTooFewChars(rut: string): boolean

Returns true if the RUT has too few characters, false otherwise.

import RUT from 'rut-chile';

const rut = '46555-5';

// Rut-Chile also accepts RUTs in these formats:
// 46.555-5 (Dotted format)
// 46,555-5 (Comma format)
// 465555 (Unformatted, without separators and without hyphen)
// Accepts lowercase or uppercase for RUTs with 'k'.

console.log(RUT.hasTooFewChars(rut)) // Output: true

hasTooManyChars(rut: string): boolean

Returns true if the RUT has too many characters, false otherwise.

import RUT from 'rut-chile';

const rut = '107665555-5';

// Rut-Chile also accepts RUTs in these formats:
// 107.665.555-5 (Dotted format)
// 107,665,555-5 (Comma format)
// 1076655555 (Unformatted, without separators and without hyphen)
// Accepts lowercase or uppercase for RUTs with 'k'.

console.log(RUT.hasTooManyChars(rut)) // Output: true

hasInvalidChars(rut: string): boolean

Returns true if the RUT has invalid characters, false otherwise.

import RUT from 'rut-chile';

const rut = '17665555-5';

// Rut-Chile also accepts RUTs in these formats:
// 17.665.555-5 (Dotted format)
// 17,665,555-5 (Comma format)
// 176655555 (Unformatted, without separators and without hyphen)
// Accepts lowercase or uppercase for RUTs with 'k'.

console.log(RUT.hasInvalidChars(rut)) // Output: false

const badRut = '1766555a-5';
console.log(RUT.hasInvalidChars(badRut)) // Output: true

hasInvalidDash(rut: string): boolean

Returns true if the RUT has invalid dash, false otherwise.

import RUT from 'rut-chile';

const rut = '17665555-5';

// Rut-Chile also accepts RUTs in these formats:
// 17.665.555-5 (Dotted format)
// 17,665,555-5 (Comma format)
// Accepts lowercase or uppercase for RUTs with 'k'.

console.log(RUT.hasInvalidDash(rut)) // Output: false

const badRut = '1766555-55';
console.log(RUT.hasInvalidChars(badRut)) // Output: true

hasSpaces(rut: string): boolean

Returns true if the RUT has invalid spaces, false otherwise.

import RUT from 'rut-chile';

const rut = '17665555-5';

// Rut-Chile also accepts RUTs in these formats:
// 17.665.555-5 (Dotted format)
// 17,665,555-5 (Comma format)
// 176655555 (Unformatted, without separators and without hyphen)
// Accepts lowercase or uppercase for RUTs with 'k'.

console.log(RUT.hasSpaces(rut)) // Output: false

const badRut = '17 665555-5';
console.log(RUT.hasSpaces(badRut)) // Output: true