Skip to content

Commit 7aab86b

Browse files
committed
Merge branch 'dev'
* dev: Updating Readme file. National code validation has been added into the library. Updating main variable name to match the library name. Postal code validation has been added into the library.
2 parents aca1b1c + dd2708e commit 7aab86b

File tree

7 files changed

+104
-5
lines changed

7 files changed

+104
-5
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ Then you can use the available methods for different available validators:
1616
```javascript
1717
IRCheck.Phone.checkType('02191001848'); //returns 'LANDLINE'
1818
IRCheck.Phone.normalizeMobile('+989121234567'); // returns '9121234567'
19+
IRCheck.Postal.isPostalCodeValid('7634598734'); // returns 'TRUE'
20+
IRCheck.National.isNationalCodeValid('4721016352'); // returns 'TRUE'
1921
```
2022

2123
## Validators Availability
2224
Currently, the following validators are available:
2325
* [Iran Phone Numbers](#phone)
26+
* [Iran Postal](#postal)
27+
* [Iran National Codes](#national)
2428

2529
### Phone
2630
Here’s a list of available methods:
@@ -29,10 +33,25 @@ Here’s a list of available methods:
2933
* `isLandline(number)` Verify the input number is a type of landline
3034
* `normalizeMobile(number)` Normalize a mobile number as 9121234567
3135
* `normalizeLandline(number)` Normalize a landline number as 2191001848
32-
* `getProvinceData(number)` Get province info from its area code or code name
36+
* `getProvinceData(province {String|Number})` Get province info from its area code or code name
3337
* `getProvincesFromLandline(number)` Extract province data from a landline numbers
3438

39+
### Postal
40+
Here’s a list of available methods:
41+
* `isPostalCodeValid(postalCode {String})` Check if the entered postal code is valid
42+
43+
### National
44+
Here’s a list of available methods:
45+
* `isNationalCodeValid(nationalCode {String})` Check if the entered Iranian national code is valid
46+
47+
3548
## History
49+
### 0.3.0
50+
* Postal code validation has been added into the library.
51+
* National code validation has been added into the library.
52+
* Updating README.md file.
53+
* A few changes in the main file.
54+
3655
### 0.1.1
3756
* Fixing typo errors in README.md file.
3857

index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
const Phone = require('./phone');
2+
const Postal = require('./postal');
3+
const National = require('./national');
24

3-
const Verifier = function () {};
5+
const IRCheck = function () {};
46

5-
Verifier.prototype.Phone = Phone;
7+
IRCheck.prototype.Phone = Phone;
8+
IRCheck.prototype.Postal = Postal;
9+
IRCheck.prototype.National = National;
610

7-
module.exports = new Verifier();
11+
module.exports = new IRCheck();

national/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const National = function () {};
2+
3+
/**
4+
* Check if the entered Iranian national code is valid.
5+
* @param {string} nationalCode
6+
* @returns {boolean}
7+
*/
8+
National.prototype.isNationalCodeValid = function (nationalCode) {
9+
if (nationalCode.length !== 10) {
10+
return false;
11+
}
12+
let sumOfMultipliedNumber = 0;
13+
for (i = 10; i > 1; i -= 1) {
14+
sumOfMultipliedNumber += nationalCode.charAt(10 - i) * i;
15+
}
16+
17+
const r = sumOfMultipliedNumber % 11;
18+
19+
if (r < 2) {
20+
return Number(nationalCode.charAt(9)) === r;
21+
}
22+
23+
return Number(nationalCode.charAt(9)) === (11 - r);
24+
}
25+
26+
module.exports = new National();

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.1.1",
3+
"version": "0.3.0",
44
"description": "Node.js validation library for Iranian developers",
55
"main": "index.js",
66
"scripts": {

postal/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const PostalCodeRegex = /^[13-9]{10}$/;
2+
3+
const Postal = function () {};
4+
5+
/**
6+
* Check if the entered postal code is valid.
7+
* @param {string} postalCode
8+
* @returns {boolean}
9+
*/
10+
Postal.prototype.isPostalCodeValid = function (postalCode) {
11+
return new RegExp(PostalCodeRegex).test(postalCode);
12+
};
13+
14+
module.exports = new Postal();

test/national-test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const { describe, it } = require('mocha');
2+
const { expect } = require('chai');
3+
const { National } = require('../index');
4+
5+
describe('National Tools', () => {
6+
describe('isNationalCodeValid', () => {
7+
it('should be valid national code', () => {
8+
expect(National.isNationalCodeValid('0939092001')).to.be.equal(true);
9+
expect(National.isNationalCodeValid('4721016352')).to.be.equal(true);
10+
expect(National.isNationalCodeValid('4120128431')).to.be.equal(true);
11+
});
12+
it('should not be a valid national code', () => {
13+
expect(National.isNationalCodeValid('0939092002')).to.be.equal(false);
14+
expect(National.isNationalCodeValid('472016352')).to.be.equal(false);
15+
expect(National.isNationalCodeValid('04120128431')).to.be.equal(false);
16+
});
17+
});
18+
});

test/postal-test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const { describe, it } = require('mocha');
2+
const { expect } = require('chai');
3+
const { Postal } = require('../index');
4+
5+
describe('Postal Tools', () => {
6+
describe('isPostalCodeValid', () => {
7+
it('should be valid postal codes', () => {
8+
expect(Postal.isPostalCodeValid('1834556784')).to.equal(true);
9+
expect(Postal.isPostalCodeValid('7634598734')).to.equal(true);
10+
expect(Postal.isPostalCodeValid('3813187764')).to.equal(true);
11+
});
12+
it('should be not a valid postal code', () => {
13+
expect(Postal.isPostalCodeValid('76654543')).to.equal(false);
14+
expect(Postal.isPostalCodeValid('1898875423')).to.equal(false);
15+
expect(Postal.isPostalCodeValid('1890875413')).to.equal(false);
16+
});
17+
});
18+
});

0 commit comments

Comments
 (0)