Skip to content

Commit dfa7d89

Browse files
committed
refactor: ♻️ Code refactorization for improve code maintainability
1 parent 84580be commit dfa7d89

1 file changed

Lines changed: 38 additions & 42 deletions

File tree

src/index.ts

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,26 @@
11
/**
2-
* The function `documentValidator` in TypeScript validates a Dominican Republic
2+
* The function `documentValidator` validates a Dominican Republic
33
* identification document number.
4-
* @param {string} document - The code you provided is a function that validates a Dominican Republic
5-
* national identification document number (Cédula de Identidad y Electoral). The function takes a
4+
@param {string} document - The document number to validate (Cédula de Identidad y Electoral).
65
* string representing the document number as input and performs a series of calculations to validate
76
* it.
87
* @returns The function `documentValidator` is returning a boolean value - `true` if the
98
* provided Dominican document number is valid, and `false` if it is not valid.
109
*/
1110
export function documentValidator(document: string): boolean {
12-
const documentId: string = removeHyphens(document);
11+
try {
12+
const documentId: string = removeHyphens(document);
1313

14-
if (documentId.length != 11) {
15-
throw new Error('The provided document should have only 11 characters');
16-
}
17-
18-
const c = documentId.split('');
19-
const v = [1, 2, 1, 2, 1, 2, 1, 2, 1, 2];
20-
let result = 0;
21-
let oc;
22-
23-
for (let i = 0; i < 10; i++) {
24-
const up = parseInt(c[i]) * v[i];
25-
const ab = up;
26-
27-
if (ab >= 10) {
28-
oc = ab
29-
.toString()
30-
.split('')
31-
.map((x) => parseInt(x))
32-
.reduce((x, y) => x + y);
33-
} else {
34-
oc = ab;
14+
if (documentId.length !== 11) {
15+
throw new Error('The provided document must have 11 characters.');
3516
}
3617

37-
result += oc;
38-
}
39-
40-
let dp = result;
41-
let ac: any = dp.toString().split('')[0] + '0';
42-
ac = parseInt(ac);
43-
const uj = (ac / 10) * 10;
44-
45-
if (uj < dp) {
46-
// eslint-disable-next-line prettier/prettier
47-
dp = uj + 10 - dp;
48-
}
18+
const digits = documentId.split('').map(Number);
19+
const checkDigit = calculateDigits(digits.slice(0, 10));
4920

50-
const validationResult = c[10] == dp.toString();
51-
52-
if (validationResult) {
53-
return true;
54-
} else {
21+
return checkDigit === digits[10];
22+
} catch (error: any) {
23+
console.error(error.message);
5524
return false;
5625
}
5726
}
@@ -62,3 +31,30 @@ function removeHyphens(documentId: string): string {
6231
}
6332
return documentId;
6433
}
34+
35+
/**
36+
* Calculates the check digit for a document number.
37+
* @param {number[]} digits - The first 10 digits of the document.
38+
* @returns {number} - The calculated check digit.
39+
*/
40+
function calculateDigits(digits: number[]): number {
41+
const weights = [1, 2, 1, 2, 1, 2, 1, 2, 1, 2];
42+
let sum = 0;
43+
44+
for (let i = 0; i < weights.length; i++) {
45+
const product = digits[i] * weights[i];
46+
sum += sumDigits(product);
47+
}
48+
49+
const nextTen = Math.ceil(sum / 10) * 10;
50+
return nextTen - sum;
51+
}
52+
53+
/**
54+
* Sums the digits of a number.
55+
* @param {number} num - The number whose digits will be summed.
56+
* @returns {number} - The sum of the digits of the number.
57+
*/
58+
function sumDigits(num: number): number {
59+
return num.toString().split('').reduce((acc, digit) => acc + parseInt(digit), 0);
60+
}

0 commit comments

Comments
 (0)