export const NUMBER_LENGTH = 13 export const KEY_LENGTH = 2 export function checkNIR(nir: string, nirType: string): boolean { nir = nir.replace(/\s+/g, '').toUpperCase() const nirRegexComplex = new RegExp( '^' + '(?[1-4]|7|8)' + '(?\\d{2})' + '(?0[1-9]|1[0-2]|2[0-9]|3[0-9]|4[0-2])' + '(?\\d{2}|2A|2B|96|97\\d|98\\d|99\\d)' + '(?\\d{3})' + '(?\\d{3})' + '(?[0-9]{2})?' + '$', 'i', ) const nirSimpleRegex = new RegExp( '^' + '(?[12])' + '(?\\d{2})' + '(?0[1-9]|1[0-2]|[2-9][0-9])' + '(?\\d{2}|2A|2B)' + '(?\\d{3})' + '(?\\d{3})' + '(?9[0-7]|[0-8]\\d)?' + '$', 'i', ) const nirRegex = nirType === 'simple' ? nirSimpleRegex : nirRegexComplex return nirRegex.test(nir) } export function computeNIRKey(nir: string): string { nir = nir.replace(/\s+/g, '').toUpperCase() let nirNumberPart = nir.substring(0, 13) nirNumberPart = nirNumberPart.replace('2A', '19').replace('2B', '18') const nirNumber = BigInt(nirNumberPart) const key = 97n - (nirNumber % 97n) return key.toString().padStart(2, '0') } export function isNIRKeyValid(nir: string): boolean { nir = nir.replace(/\s+/g, '').toUpperCase() const providedKey = nir.substring(13, 15) const computedKey = computeNIRKey(nir) return providedKey === computedKey }