-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
47 lines (39 loc) · 1.33 KB
/
index.js
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
const National = function () {};
/**
* Check if the entered Iranian national code is valid.
* @param {string} nationalCode
* @returns {boolean}
*/
National.prototype.isNationalCodeValid = function (nationalCode) {
if (nationalCode.length !== 10) {
return false;
}
let sumOfMultipliedNumber = 0;
for (i = 10; i > 1; i -= 1) {
sumOfMultipliedNumber += nationalCode.charAt(10 - i) * i;
}
const r = sumOfMultipliedNumber % 11;
if (r < 2) {
return Number(nationalCode.charAt(9)) === r;
}
return Number(nationalCode.charAt(9)) === (11 - r);
}
/**
* Check if the entered Iranian company national code is valid.
* @param {string} companyNationalCode
* @returns {boolean}
*/
National.prototype.isCompanyNationalCodeValid = function (companyNationalCode) {
if ((companyNationalCode.length !== 11) || (isNaN(Number(companyNationalCode)))) {
return false;
}
const ratios = [29, 27, 23, 19, 17, 29, 27, 23, 19, 17];
const baseNumber = Number(companyNationalCode.charAt(9)) + 2;
let sumOfMultipliedNumber = 0;
for (i = 0; i < 10; i += 1) {
sumOfMultipliedNumber += (Number(companyNationalCode.charAt(i)) + baseNumber) * ratios[i];
}
const r = (sumOfMultipliedNumber % 11) === 10 ? 0 : (sumOfMultipliedNumber % 11);
return (r === Number(companyNationalCode.charAt(10)));
}
module.exports = new National();