Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: cblanc/postcode.js
base: e0244856ac
...
head fork: cblanc/postcode.js
compare: 9e829086fc
Checking mergeability… Don't worry, you can still create the pull request.
  • 4 commits
  • 7 files changed
  • 0 commit comments
  • 1 contributor
View
1  .gitignore
@@ -1 +1,2 @@
node_modules
+.DS_Store
View
7 README.md
@@ -28,6 +28,13 @@ postcode.incode() // => "1QD"
postcode.normalise() // => "ID1 1QD"
```
+Misc. Class Methods include
+
+```
+Postcode.validOutcode(outcode)
+```
+
+
## Testing
```npm test```
View
17 index.js
@@ -1,18 +1,19 @@
"use strict";
var validationRegex = /^[a-z0-9]{1,4}\s*?\d[a-z]{2}$/i,
- outcodeRegex = /\d[a-z]{2}$/i;
+ incodeRegex = /\d[a-z]{2}$/i,
+ validOutcodeRegex = /[a-z0-9]{1,4}/i;
function isValidPostcode (postcode) {
return !!postcode.match(validationRegex);
}
-function parseIncode (postcode) {
- return postcode.replace(outcodeRegex, "").replace(/\s+/, "");
+function parseOutcode (postcode) {
+ return postcode.replace(incodeRegex, "").replace(/\s+/, "");
}
-function parseOutcode (postcode) {
- return postcode.match(outcodeRegex)[0];
+function parseIncode (postcode) {
+ return postcode.match(incodeRegex)[0];
}
function Postcode (rawPostcode) {
@@ -24,6 +25,10 @@ function Postcode (rawPostcode) {
}
}
+Postcode.validOutcode = function (outcode) {
+ return !!outcode.match(validOutcodeRegex);
+}
+
Postcode.prototype.valid = function () {
return this._valid;
}
@@ -45,7 +50,7 @@ Postcode.prototype.outcode = function () {
Postcode.prototype.normalise = function () {
if (!this._valid) return null;
if (this.postcode) return this.postcode;
- return [this.incode()," ", this.outcode()].join("");
+ return [this.outcode()," ", this.incode()].join("");
}
module.exports = Postcode;
View
2  package.json
@@ -1,6 +1,6 @@
{
"name": "postcode",
- "version": "0.1.3",
+ "version": "0.2.0",
"description": "UK Postcode helper methods",
"main": "index.js",
"directories": {
View
0  tests/data/outcode.json → tests/data/incodes.json
File renamed without changes
View
0  tests/data/incode.json → tests/data/outcodes.json
File renamed without changes
View
17 tests/unit.js
@@ -41,9 +41,22 @@ describe("Postcode normalisation", function () {
});
});
+describe("Postcode.validOutcode", function () {
+ it ("should return true for valid outcodes", function (done) {
+ testData = fs.readFile(path.join(dataDir, "outcodes.json"), function (error, data) {
+ if (error) throw error;
+ testData = JSON.parse(data);
+ testData.tests.forEach(function (test) {
+ assert.isTrue(Postcode.validOutcode(test.expected));
+ });
+ done();
+ });
+ });
+});
+
describe("Incode parsing", function () {
before(function (done) {
- testData = fs.readFile(path.join(dataDir, "incode.json"), function (error, data) {
+ testData = fs.readFile(path.join(dataDir, "incodes.json"), function (error, data) {
if (error) throw error;
testData = JSON.parse(data);
done();
@@ -63,7 +76,7 @@ describe("Incode parsing", function () {
describe("Outcode parsing", function () {
before(function (done) {
- testData = fs.readFile(path.join(dataDir, "outcode.json"), function (error, data) {
+ testData = fs.readFile(path.join(dataDir, "outcodes.json"), function (error, data) {
if (error) throw error;
testData = JSON.parse(data);
done();

No commit comments for this range

Something went wrong with that request. Please try again.