Created
February 26, 2024 00:25
-
-
Save edjeordjian/25dac767d6c24f1d2ec248ad3503fe8f to your computer and use it in GitHub Desktop.
Código para comprobar un número de CUIT
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
// https://es.wikipedia.org/wiki/Clave_%C3%9Anica_de_Identificaci%C3%B3n_Tributaria | |
// Expects the digits of the cuit except the last one (verifiying digit). | |
const getVerifiyingCUITDigit = (incompleteCUIT) => { | |
const coefficients = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2]; | |
if (incompleteCUIT.length !== coefficients.length) { | |
return -1; | |
} | |
let totalSum = 0; | |
for (let i = 0; | |
i < coefficients.length; | |
i += 1) { | |
totalSum += parseInt(incompleteCUIT[i]) * coefficients[i]; | |
} | |
const remainder = 11 - totalSum % 11; | |
if (remainder === 11) { | |
return 0; | |
} | |
if (remainder === 10) { | |
return 1; | |
} | |
return remainder; | |
} | |
// Expects a CUIT with the format XX-XXXXXXXX-X | |
const isCUITValid = (cuit) => { | |
if (!cuit) { | |
return false; | |
} | |
const components = cuit.split("-"); | |
const types = ["20", "23", "24", "25", "26", "27", "30", "33", "34"]; | |
if (components.length != 3 | |
|| !parseInt(components[0]) | |
|| !parseInt(components[1]) | |
|| !parseInt(components[2]) | |
|| !types.includes(components[0])) { | |
return false; | |
} | |
const expectedLastDigit = getVerifiyingCUITDigit(components[0] + components[1]); | |
const lastDigit = parseInt(cuit[cuit.length - 1], 10); | |
return expectedLastDigit === lastDigit; | |
} | |
module.exports = { | |
isCUITValid, getVerifiyingCUITDigit | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment