🇨🇱 Chilean Rut Utilities 🇨🇱
🇪🇸 Versión en Español
Table of Contents
💬 Intro
Set of utility functions to parse, validate and generate a Chilean R.U.T.
Meant for developers who want to interact, manipulate and validate Chilean R.U.T.
🚀 Getting started
Npm & Yarn install:
$ npm install @fdograph/rut-utilities
$ yarn add @fdograph/rut-utilities
🔧 Usage
validateRut(rut: string) => boolean
Returns true
if the passed string
corresponds to a fully valid R.U.T.
import { validateRut } from '@fdograph/rut-utilities';
validateRut('18585543-0');
> true
validateRut('18.585.543-0');
> true
validateRut('9.999.999-9');
> false
validateRutList(ruts: string[]) => Map<string, boolean>
Returns a results Map in which each entry has a key
corresponding to the input and the value
corresponding to its validation result.
import { validateRutList } from '@fdograph/rut-utilities';
const validRuts = ['7775735-k', '18585543-0', '18348353-6'];
const result = validateRutList(validRuts);
result.get('7775735-k');
> true
result.get(validRuts[1]);
> true
formatRut(rut: string, format?: RutFormat = RutFormat.DASH) => string
Formats a rut-like
string according to the format
parameter or returns the intact string if this doesn't match a rut-like
string pattern.
enum RutFormat {
DOTS,
DASH,
DOTS_DASH
}
import { formatRut, RutFormat } from '@fdograph/rut-utilities';
formatRut('44.333.222-1');
> '44333222-1'
formatRut('44333222-1', RutFormat.DOTS_DASH);
> '44.333.222-1'
formatRut('44333222-1', RutFormat.DOTS);
> '44.333.2221'
formatRut('jg7gk-1', RutFormat.DOTS);
> 'jg7gk-1'
deconstructRut(rut: string) => DeconstructedRut
Returns an object containing the RUT's digits
and verifier
.
You can use Destructuring to access each.
type DeconstructedRut = {
digits: string;
verifier: string;
}
import { deconstructRut } from '@fdograph/rut-utilities';
const { digits, verifier } = deconstructRut('7775735-k');
console.log(digits);
> '7775735'
console.log(verifier);
> 'k'
You can see the full set of utility functions in the Tests
📄 License
This project is licensed under the Apache 2.0 License - see the LICENSE file for details.