UNPKG

rutility

Version:

A JavaScript library for validating and formatting Chilean RUTs (Rol Único Tributario). It provides functions to add/remove dots and dashes, validate formats, and calculate check digits

81 lines 3.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isFormat = exports.isValidRut = exports.calculateDv = void 0; var utils_1 = require("./Utils/utils"); /** * Calculates the verification digit of a Chilean RUT (Rol Único Tributario). * @param {string | number} rut - The RUT in one of the following formats: * - '12.345.678' * - '12345678' * - 12345678 (as a number) * @returns {string} The calculated verification digit or '0' if it's 11, 'K' if it's 10. * @throws {Error} If the RUT format is invalid. Acceptable formats are '26270086' or '26.270.086'. * @example * calculateDv('12.345.678'); // returns '5' * calculateDv('12345678'); // returns '5' * calculateDv(12345678); // returns '5' */ var calculateDv = function (rut) { var _a; if (typeof rut === 'number') { rut = rut.toString(); } if (!(0, utils_1.isValidFormatWithOutDash)(rut)) { throw new Error("Invalid RUT format. Acceptable formats are '12.345.678' or '12345678'."); } var rutBase = rut.replace(/\./g, ''); var series = [2, 3, 4, 5, 6, 7]; var sum = rutBase .split('') .reverse() .reduce(function (acc, digit, idx) { return acc + parseInt(digit) * series[idx % series.length]; }, 0); var dvCalc = 11 - (sum % 11); var finalDv = { 11: '0', 10: 'K' }; return (_a = finalDv[dvCalc]) !== null && _a !== void 0 ? _a : dvCalc.toString(); }; exports.calculateDv = calculateDv; /** * Validates if a Chilean RUT (Rol Único Tributario) is valid. * @param {string} rut - The RUT in one of the following formats: * - '12.345.678-9' * - '12345678-9' * @returns {boolean} true if the RUT is valid, false otherwise. * @throws {Error} If the RUT format is invalid. */ var isValidRut = function (rut) { (0, utils_1.formatValidations)(rut); var digitsAndVerification = rut.replace(/[.-]/g, ''); var digits = digitsAndVerification.slice(0, -1); var verificationDigit = digitsAndVerification.slice(-1); var calculatedVerificationDigit = (0, exports.calculateDv)(digits); return (verificationDigit.toLowerCase() === calculatedVerificationDigit.toLowerCase()); }; exports.isValidRut = isValidRut; exports.isFormat = { /** * Checks if a Chilean RUT has the correct format with dots and optional dash. * @param {string} rut - The RUT to be checked. Example: "12.345.678-9". * @returns {boolean} true if the RUT has the correct format, false otherwise. Example: true. */ dot: function (rut) { return /^(?!0)(\d{1,3}(?:\.\d{3})*)$/.test(rut); }, /** * Checks if a Chilean RUT has the correct format with dash at the end. * @param {string} rut - The RUT to be checked. Example: "12.345.678-9". * @returns {boolean} true if the RUT has the correct format, false otherwise. Example: true. */ dash: function (rut) { return /^(?!0)\d{1,9}-(\d|k|K)$/i.test(rut); }, /** * Checks if a Chilean RUT has the correct format with dots and dash, regardless of their order. * @param {string} rut - The RUT to be checked. Example: "12.345.678-9". * @returns {boolean} true if the RUT has the correct format, false otherwise. Example: true. */ dotDash: function (rut) { return /^(?!0)(\d{1,3}(?:\.\d{3})*)-(\d|k|K)$/i.test(rut); } }; //# sourceMappingURL=validations.js.map