@devoxs/ati-doc-validator
Version:
Libreria de validacion de documentos oficiales del Ecuador
97 lines (96 loc) • 3.47 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AtiDocumentValidator = void 0;
var AtiDocumentValidatorClass = /** @class */ (function () {
function AtiDocumentValidatorClass() {
var _this = this;
this.provinceCodes = [
'01',
'02',
'03',
'04',
'05',
'06',
'07',
'08',
'09',
'10',
'11',
'12',
'13',
'14',
'15',
'16',
'17',
'18',
'19',
'20',
'21',
'22',
'23',
'24',
];
this._regexOnlyTenNumbers = new RegExp('^[0-9]{10}$');
this._regexOnly13Numbers = new RegExp('^([0-9]{10})001$');
this._regexRucPrvtSocNoResident = new RegExp("^(" + this.provinceCodes.join('|') + ")9([0-9]{7})001$");
this._regexPublicSociety = new RegExp("^(" + this.provinceCodes.join('|') + ")6([0-9]{6})0001$");
this.cedulaValidator = function (cedula) {
var validationResult = {
result: false,
message: '',
};
if (_this.isNullOrUndefined(cedula)) {
validationResult.message = 'Parámetro de entrada incorrecto';
return validationResult;
}
if (!_this._regexOnlyTenNumbers.test(cedula)) {
validationResult.message = 'Documento no cumple formato. Solo se permiten 10 dígitos';
return validationResult;
}
var total = 0;
for (var i = 0; i < cedula.length - 1; i++) {
if (i % 2 === 0) {
var acumulator = Number(cedula.charAt(i)) * 2;
if (acumulator > 9) {
acumulator -= 9;
}
total += acumulator;
}
else {
total += Number(cedula.charAt(i));
}
}
total = total % 10 ? 10 - (total % 10) : 0;
if (Number(cedula.charAt(cedula.length - 1)) === total) {
validationResult.result = true;
}
else {
validationResult.message = 'Número de documento incorrecto';
}
return validationResult;
};
this.rucValidator = function (ruc) {
var validationResult = {
result: false,
message: '',
};
if (!_this._regexOnly13Numbers.test(ruc)) {
validationResult.message = 'Documento no cumple formato. Solo se permiten 13 dígitos.';
return validationResult;
}
validationResult.result =
_this.cedulaValidator(ruc.substring(0, 10)).result ||
_this._regexRucPrvtSocNoResident.test(ruc) ||
_this._regexPublicSociety.test(ruc);
if (!validationResult.result) {
validationResult.message = 'Número de RUC incorrecto';
}
return validationResult;
};
}
AtiDocumentValidatorClass.prototype.isNullOrUndefined = function (value) {
return value === undefined || value === null;
};
return AtiDocumentValidatorClass;
}());
exports.AtiDocumentValidator = AtiDocumentValidatorClass;