-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathutil.ts
112 lines (89 loc) · 2.76 KB
/
util.ts
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
interface CnpjDigits {
penultimateDigit: number;
ultimateDigit: number;
}
const removeSpecialCharsFromCnpjRegex = /[\.]|[-]|[\/]/g;
export const formatValueToCnpjPattern = (cnpj: string): string => {
const cnpjValue = removeSpecialCharsFromCnpj(cnpj);
let formattedValue: string = '';
let index: number = 0;
while (formattedValue.length < 18 && index < 17) {
if (index === 2 || index === 5) {
formattedValue += '.';
} else if (index === 8) {
formattedValue += '/';
} else if (index === 12) {
formattedValue += '-';
}
formattedValue += cnpjValue[index];
index++;
}
return formattedValue;
};
export const removeSpecialCharsFromCnpj = (cnpj: string): string => {
return cnpj.replace(removeSpecialCharsFromCnpjRegex, '');
};
const getCnpjDigitsNumbers = (cnpj: string): CnpjDigits => {
const lastTwoNumbers = cnpj.slice(cnpj.length - 2);
const penultimateDigit = parseInt(lastTwoNumbers[0]);
const ultimateDigit = parseInt(lastTwoNumbers[1]);
return {
penultimateDigit,
ultimateDigit,
};
};
const transformCnpjInArrNumber = (cnpj: string): number[] => {
var arr: number[] = [];
let index = 0;
while (index < 12) {
arr.push(parseInt(cnpj[index]));
index++;
}
return arr;
};
const calculateCnpjDigits = (cnpjNumbers: number[]): CnpjDigits => {
const factor = 11;
let index = cnpjNumbers.length - 1;
let startAuxValue = 2;
let totalForDigit = 0;
while (index >= 0) {
totalForDigit = totalForDigit + cnpjNumbers[index] * startAuxValue;
startAuxValue = startAuxValue === 9 ? 2 : startAuxValue + 1;
index--;
}
const calcPDigit = totalForDigit % factor;
const resultPDigit = factor - calcPDigit;
const zeroIfPGreaterThanNine = resultPDigit >= 9 ? 0 : resultPDigit;
const penultimateDigit = zeroIfPGreaterThanNine;
cnpjNumbers.push(penultimateDigit);
index = cnpjNumbers.length - 1;
startAuxValue = 2;
totalForDigit = 0;
while (index >= 0) {
totalForDigit = totalForDigit + cnpjNumbers[index] * startAuxValue;
startAuxValue = startAuxValue === 9 ? 2 : startAuxValue + 1;
index--;
}
const calcUDigit = totalForDigit % factor;
const resultUDigit = factor - calcUDigit;
const zeroIfGreaterThanNine = resultUDigit > 9 ? 0 : resultUDigit;
const ultimateDigit = zeroIfGreaterThanNine;
return {
penultimateDigit,
ultimateDigit,
};
};
export const isValidCnpjDigit = (cnpj: string): boolean => {
const onlyNumbers = removeSpecialCharsFromCnpj(cnpj);
if (onlyNumbers.length !== 14) {
return false;
}
const digits = getCnpjDigitsNumbers(onlyNumbers);
const arrNumbers = transformCnpjInArrNumber(onlyNumbers);
const validDigits = calculateCnpjDigits(arrNumbers);
return (
digits.penultimateDigit === validDigits.penultimateDigit &&
digits.ultimateDigit === validDigits.ultimateDigit
);
};
export default isValidCnpjDigit;