Skip to content

Commit ca6bc8a

Browse files
committed
Company national code validation is added.
2 parents 7aab86b + 7a3c4c7 commit ca6bc8a

File tree

4 files changed

+254
-3
lines changed

4 files changed

+254
-3
lines changed

national/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,25 @@ National.prototype.isNationalCodeValid = function (nationalCode) {
2323
return Number(nationalCode.charAt(9)) === (11 - r);
2424
}
2525

26+
/**
27+
* Check if the entered Iranian company national code is valid.
28+
* @param {string} companyNationalCode
29+
* @returns {boolean}
30+
*/
31+
National.prototype.isCompanyNationalCodeValid = function (companyNationalCode) {
32+
if ((companyNationalCode.length !== 11) || (isNaN(Number(companyNationalCode)))) {
33+
return false;
34+
}
35+
36+
const ratios = [29, 27, 23, 19, 17, 29, 27, 23, 19, 17];
37+
const baseNumber = Number(companyNationalCode.charAt(9)) + 2;
38+
let sumOfMultipliedNumber = 0;
39+
for (i = 0; i < 10; i += 1) {
40+
sumOfMultipliedNumber += (Number(companyNationalCode.charAt(i)) + baseNumber) * ratios[i];
41+
}
42+
43+
const r = (sumOfMultipliedNumber % 11) === 10 ? 0 : (sumOfMultipliedNumber % 11);
44+
return (r === Number(companyNationalCode.charAt(10)));
45+
}
46+
2647
module.exports = new National();

package-lock.json

Lines changed: 219 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ircheck",
3-
"version": "0.3.0",
3+
"version": "0.3.5",
44
"description": "Node.js validation library for Iranian developers",
55
"main": "index.js",
66
"scripts": {

test/national-test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,17 @@ describe('National Tools', () => {
1515
expect(National.isNationalCodeValid('04120128431')).to.be.equal(false);
1616
});
1717
});
18+
19+
describe('isCompanyNationalCodeValid', () => {
20+
it('should be valid company national code', () => {
21+
expect(National.isCompanyNationalCodeValid('10380284790')).to.be.equal(true);
22+
expect(National.isCompanyNationalCodeValid('14007259837')).to.be.equal(true);
23+
expect(National.isCompanyNationalCodeValid('14007256420')).to.be.equal(true);
24+
});
25+
it('should not be valid company national code', () => {
26+
expect(National.isCompanyNationalCodeValid('1038028479')).to.be.equal(false);
27+
expect(National.isCompanyNationalCodeValid('14006259837')).to.be.equal(false);
28+
expect(National.isCompanyNationalCodeValid('14007256429')).to.be.equal(false);
29+
});
30+
});
1831
});

0 commit comments

Comments
 (0)