Skip to content

Commit 46dcf82

Browse files
committed
Fixed incodes and outcodes
1 parent 5c47737 commit 46dcf82

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"use strict";
22

33
var validationRegex = /^[a-z0-9]{1,4}\s*?\d[a-z]{2}$/i,
4-
incodeRegex = /\d[a-z]{2}$/i;
4+
incodeRegex = /\d[a-z]{2}$/i,
5+
validOutcodeRegex = /[a-z0-9]{1,4}/i;
56

67
function isValidPostcode (postcode) {
78
return !!postcode.match(validationRegex);
@@ -24,6 +25,10 @@ function Postcode (rawPostcode) {
2425
}
2526
}
2627

28+
Postcode.validOutcode = function (outcode) {
29+
return !!outcode.match(validationRegex);
30+
}
31+
2732
Postcode.prototype.valid = function () {
2833
return this._valid;
2934
}

tests/unit.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,29 @@ describe("Postcode normalisation", function () {
4141
});
4242
});
4343

44+
describe("Postcode.validOutcode", function () {
45+
it ("should return true for valid outcodes", function (done) {
46+
testData = fs.readFile(path.join(dataDir, "outcodes.json"), function (error, data) {
47+
if (error) throw error;
48+
testData = JSON.parse(data);
49+
testData.forEach(function (test) {
50+
assert.isTrue(Postcode.validOutcode(test.expected));
51+
});
52+
done();
53+
});
54+
});
55+
it ("should return false for invalid outcodes", function (done) {
56+
testData = fs.readFile(path.join(dataDir, "incodes.json"), function (error, data) {
57+
if (error) throw error;
58+
testData = JSON.parse(data);
59+
testData.forEach(function (test) {
60+
assert.isFalse(Postcode.validOutcode(test.expected));
61+
});
62+
done();
63+
});
64+
});
65+
})
66+
4467
describe("Incode parsing", function () {
4568
before(function (done) {
4669
testData = fs.readFile(path.join(dataDir, "incodes.json"), function (error, data) {

0 commit comments

Comments
 (0)