Last active
December 20, 2016 16:44
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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