Skip to content

Commit 1464661

Browse files
committed
Postal code validation has been added into the library.
1 parent aca1b1c commit 1464661

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

index.js

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

34
const Verifier = function () {};
45

56
Verifier.prototype.Phone = Phone;
7+
Verifier.prototype.Postal = Postal;
68

79
module.exports = new Verifier();

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/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)