Skip to content

Instantly share code, notes, and snippets.

@mrexclamation
Last active December 20, 2016 16:44
Show Gist options
  • Save mrexclamation/792da3120b165d8dd18e2280f1407a0b to your computer and use it in GitHub Desktop.
Save mrexclamation/792da3120b165d8dd18e2280f1407a0b to your computer and use it in GitHub Desktop.
Script para generar el Digito de Verificacion de Nits o Cedulas de la DIAN Colombia
function GeneraDV(nit) {
var aPeso = [ 71, 67, 59, 53, 47, 43, 41, 37, 29, 23, 19, 17, 13, 7, 3 ];
if (typeof nit !== "string") { throw new Error("El NIT debe ser una cadena de texto"); }
// Limpiar Espacios, Comas, Puntos, Guiones y Comillas
nit = nit.replace( /\s/g,"").replace(/,/g,"").replace(/\./g,"").replace (/-/g,"").replace(/'/g,"");
if (isNaN(nit)) {
throw new Error("El NIT contiene caracteres especiales");
}
if (nit.length) {
var iAcum = aPeso.slice(aPeso.length - nit.length, aPeso.length).reduce(function(res, val, i) {
res += val * nit[i];
return res;
}, 0);
var iRes = iAcum % 11;
return iRes > 1 ? 11 - iRes : iRes;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment