Last active
September 22, 2021 12:42
-
-
Save lavary/af6e0fbb7498965ee1252cb027a7fa05 to your computer and use it in GitHub Desktop.
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 const validateBI = function (NBI, checkDigit) { | |
return { | |
valid: validateBI(NBI, checkDigit), | |
errorMessage: 'BI não válido' | |
} | |
} | |
const NBI_LENGTH = 9 | |
function calculateChecksum (number) { | |
// Make the number into an array | |
let numbers = [...number.toString()].map(Number) | |
let sum = 0 | |
for (let i = 0; i < numbers.length - 1; i++) { | |
sum += numbers[i] * (9 - i) | |
} | |
// check checksum | |
return (sum % 11 && (11 - sum % 11) % 10) === numbers[numbers.length - 1] | |
} | |
function validateBI ( NBI , checkDigit ) { | |
// Join NBI and the heck digit | |
NBI = `${NBI}${checkDigit}` | |
// Add zero to the left of NBI if it is of length 7 | |
if ( NBI.length === 8 ) { | |
NBI = '0' + NBI | |
} | |
// If still not valid size, returh false | |
if (NBI.length != NBI_LENGTH) { | |
return false | |
} | |
return calculateChecksum(NBI) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment