flit-string-utils
Version:
Funciones utilitarias para strings y validaciones comunes en Colombia.
69 lines (68 loc) • 2.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.validDocumentType = exports.validEmail = exports.validPlate = void 0;
exports.calculateVD = calculateVD;
/**
* Calculates the verification digit (VD) of a NIT in Colombia.
* @param nit - NIT number without the verification digit.
* @returns Verification digit (0-9).
*/
function calculateVD(nit) {
const weights = [71, 67, 59, 53, 47, 43, 41, 37, 29, 23, 19, 17, 13, 7, 3];
const nitStr = nit.toString().replace(/\D/g, '');
if (nitStr.length === 0 || nitStr.length > weights.length)
return null;
let sum = 0;
let j = weights.length - nitStr.length;
for (let i = 0; i < nitStr.length; i++) {
sum += parseInt(nitStr[i], 10) * weights[i + j];
}
const remainder = sum % 11;
const vd = (remainder === 0 || remainder === 1) ? remainder : (11 - remainder);
return vd;
}
/**
* Validates if the provided string is a valid vehicle plate number in Colombia.
* @param value
* @returns
*/
const validPlate = (value) => {
const expression = /^[A-Z]{3}\d{3}$/i; //placa carro
const expression2 = /^[A-Z]{3}\d{2}[A-Z]{0,1}$/i; //placa moto
const expression3 = /^[A-Z]{2}\d{6}$/i; //plate machinery
const expression4 = /^[A-Z]{1}\d{5}$/i; //plate trailers
const expression5 = /^\d{3}[A-Z]{3}$/i; //placa motocarro
const result = expression.test(value || "") || expression2.test(value || "") || expression3.test(value || "") || expression4.test(value || "") || expression5.test(value || "");
return result;
};
exports.validPlate = validPlate;
/**
* Validates if the provided string is a valid email format.
* @param value
* @returns
*/
const validEmail = (value) => {
const expression = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i;
const result = expression.test(value || "");
return result;
};
exports.validEmail = validEmail;
/**
* Validates if the provided document type is one of the accepted types.
* @param value
* @returns
*/
const validDocumentType = (value) => {
const expression = /^CC/g;
const expression2 = /^NIT/g;
const expression3 = /^CE/g;
const expression4 = /^PA/g;
const expression5 = /^RC/g;
const result = expression.test(value || "") ||
expression2.test(value || "") ||
expression3.test(value || "") ||
expression4.test(value || "") ||
expression5.test(value || "");
return result;
};
exports.validDocumentType = validDocumentType;