-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathEIKValidator.tsx
85 lines (72 loc) · 2.18 KB
/
EIKValidator.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Documentation for the EIK algorithm: https://www.nsi.bg/sites/default/files/konkursi/RGP_OPAK_2014_Annex_6.pdf
export function validateEIK9(eik: string): boolean {
const digits = checkInput(eik, 9)
const ninthDigit = calculateNinthDigitInEIK(digits)
return ninthDigit === digits[8]
}
export function validateEIK13(eik: string): boolean {
const digits = checkInput(eik, 13)
const thirteenDigit = calculateThirteenthDigitInEIK(digits)
return thirteenDigit === digits[12] && thirteenDigit !== -1
}
function calculateNinthDigitInEIK(digits: number[]): number {
let sum = 0
for (let i = 0; i < 8; i++) {
sum += digits[i] * FIRST_SUM_9DIGIT_WEIGHTS[i]
}
const remainder = sum % 11
if (remainder !== 10) {
return remainder
}
let secondSum = 0
for (let i = 0; i < 8; i++) {
secondSum += digits[i] * SECOND_SUM_9DIGIT_WEIGHTS[i]
}
const secondRem = secondSum % 11
if (secondRem !== 10) {
return secondRem
}
return 0
}
function calculateThirteenthDigitInEIK(digits: number[]): number {
const ninthDigit = calculateNinthDigitInEIK(digits)
if (ninthDigit !== digits[8]) {
return -1 // Invalid 9th digit in EIK-13
}
let sum = 0
for (let i = 8, j = 0; j < 4; i++, j++) {
sum += digits[i] * FIRST_SUM_13DIGIT_WEIGHTS[j]
}
const remainder = sum % 11
if (remainder !== 10) {
return remainder
}
let secondSum = 0
for (let i = 8, j = 0; j < 4; i++, j++) {
secondSum += digits[i] * SECOND_SUM_13DIGIT_WEIGHTS[j]
}
const secondRem = secondSum % 11
if (secondRem !== 10) {
return secondRem
}
return 0
}
function checkInput(eik: string, eikLength: number): number[] {
if (eik && eik.length !== eikLength) {
return []
}
const charDigits = eik.split('')
const digits: number[] = []
for (let i = 0; i < charDigits.length; i++) {
if (/\d/.test(charDigits[i])) {
digits[i] = parseInt(charDigits[i], 10)
} else {
return []
}
}
return digits
}
const FIRST_SUM_9DIGIT_WEIGHTS: number[] = [1, 2, 3, 4, 5, 6, 7, 8]
const SECOND_SUM_9DIGIT_WEIGHTS: number[] = [3, 4, 5, 6, 7, 8, 9, 10]
const FIRST_SUM_13DIGIT_WEIGHTS: number[] = [2, 7, 3, 5]
const SECOND_SUM_13DIGIT_WEIGHTS: number[] = [4, 9, 5, 7]