Skip to content

Instantly share code, notes, and snippets.

@anrras
Last active July 18, 2021 20:50
Show Gist options
  • Save anrras/ba9052af0a85acfd6df5bda792f87688 to your computer and use it in GitHub Desktop.
Save anrras/ba9052af0a85acfd6df5bda792f87688 to your computer and use it in GitHub Desktop.
Class for calculate digit verification of the NIT in Colombia in TypeScript
export class NitValidations {
static returnVerificationDigit(myNit: string) {
// Se limpia el Nit
let validNit = this.cleanNit(myNit);
// Si el Nit es válido, devuelve el digito
if (validNit) {
return this.calculateVerificationDigit(validNit);
}
}
static calculateVerificationDigit(nit: string) {
let vpri = new Array(16);
let totalSum: number = 0;
let cedulaDigit: number = 0;
let result: number = null;
vpri = [null, 3, 7, 13, 17, 19, 23, 29, 37, 41, 43, 47, 53, 59, 67, 71];
for (var i = 0; i < nit.length; i++) {
cedulaDigit = Number(nit.substr(i, 1));
totalSum += cedulaDigit * vpri[nit.length - i];
}
result = totalSum % 11;
return result > 1 ? 11 - result : result;
}
static cleanNit(nit: any) {
nit = nit.replace(/\s/g, ''); // spaces
nit = nit.replace(/,/g, ''); // commas
nit = nit.replace(/\./g, ''); // dots
nit = nit.replace(/-/g, ''); // dashes
if (isNaN(nit)) {
return null;
}
return nit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment