Last active
April 16, 2024 22:08
-
-
Save neiker/874c197cd0cbb06efb328f3cbc6753b3 to your computer and use it in GitHub Desktop.
Validación de CUIL (Argentina) en TypeScript / JavaScript
This file contains hidden or 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
export function cuilValidator(cuil: string): boolean { | |
if (cuil.length !== 11) { | |
return false; | |
} | |
const [checkDigit, ...rest] = cuil | |
.split('') | |
.map(Number) | |
.reverse(); | |
const total = rest.reduce( | |
(acc, cur, index) => acc + cur * (2 + (index % 6)), | |
0, | |
); | |
const mod11 = 11 - (total % 11); | |
if (mod11 === 11) { | |
return checkDigit === 0; | |
} | |
if (mod11 === 10) { | |
return false; | |
} | |
return checkDigit === mod11; | |
} |
@teebu elaborate "more correct"
I was looking for a validator, and stumbled on to this library https://github.com/brielov/cuit/tree/master/src and checked their tests.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is this correct?
This function seems to be more correct: