-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathukPostcode.js
More file actions
68 lines (62 loc) · 1.73 KB
/
ukPostcode.js
File metadata and controls
68 lines (62 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* uk-postcode
* Parse and validate UK postcodes
* @author Lovell Fuller
*
* This code is distributed under the Apache License Version 2.0, the terms of
* which may be found at http://www.apache.org/licenses/LICENSE-2.0.html
*/
/**
* Postcode object
*/
var Postcode = function() {
this.outward = null;
this.inward = null;
};
/**
* Does this postcode have valid outward part?
*/
Postcode.prototype.isValid = function() {
return this.outward !== null;
};
/**
* Does this postcode have valid outward and inward parts?
*/
Postcode.prototype.isComplete = function() {
return this.outward !== null && this.inward !== null;
};
/**
* Does this postcode have a valid outward and no inward part?
*/
Postcode.prototype.isPartial = function() {
return this.outward !== null && this.inward === null;
};
/**
* Format postcode with/without a space between the outward and inward portions
*/
Postcode.prototype.toString = function() {
var formatted = "";
if (this.isValid()) {
formatted = this.outward;
if (this.isComplete()) {
formatted = formatted + " " + this.inward;
}
}
return formatted;
};
/**
* Export factory method
*/
exports.fromString = function(postcodeAsString) {
var postcode = new Postcode();
if (typeof postcodeAsString === "string") {
var clean = postcodeAsString.toUpperCase().replace(/[^A-Z0-9]/g, '');
if (clean.match(/^[A-Z]{1,2}[0-9R][0-9A-Z]?[0-9][ABD-HJLNP-UW-Z]{2}$/)) {
postcode.outward = clean.substring(0, clean.length - 3);
postcode.inward = clean.substring(clean.length - 3);
} else if (clean.match(/^[A-Z]{1,2}[0-9][0-9A-Z]?$/)) {
postcode.outward = clean;
}
}
return postcode;
};