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
37 lines • 1.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isValidFormatWithOutDash = exports.formatValidations = void 0;
/**
* Basic validations of the format of a Chilean RUT (Rol Único Tributario).
* Checks if the RUT is a string, does not start with zero, and follows the correct numeric format.
* Throws an error if the RUT is invalid.
*
* @param {string} rut - The RUT to validate.
* @throws {Error} If the RUT is not a string.
* @throws {Error} If the RUT starts with zero.
* @throws {Error} If the RUT does not match the expected format.
*/
var formatValidations = function (rut) {
if (typeof rut !== 'string') {
throw new Error('Invalid RUT format. RUT must be a string.');
}
if (rut.startsWith('0')) {
throw new Error('Invalid RUT format. RUT cannot start with zero.');
}
var cleanRut = rut.replace(/[.-]/g, '');
var rutRegex = /^\d{1,9}[0-9kK]?$/i;
if (!rutRegex.test(cleanRut)) {
throw new Error('Invalid RUT format. RUT must be numeric and have between 1 and 10 digits.');
}
};
exports.formatValidations = formatValidations;
/**
* Validates the format of a Chilean RUT (Rol Único Tributario).
* @param {string} rut - The RUT to validate.
* @returns {boolean} True if the format is valid, false otherwise.
*/
var isValidFormatWithOutDash = function (rut) {
return /^\d{1,9}$/.test(rut) || /^\d{1,3}(\.\d{3}){0,2}$/.test(rut);
};
exports.isValidFormatWithOutDash = isValidFormatWithOutDash;
//# sourceMappingURL=utils.js.map