|
1 | 1 | "use strict"; |
2 | 2 |
|
3 | 3 | var validationRegex = /^[a-z]{1,2}\d[a-z\d]?\s*\d[a-z]{2}$/i, |
4 | | - incodeRegex = /\d[a-z]{2}$/i, |
5 | | - validOutcodeRegex = /^[a-z]{1,2}\d[a-z\d]?$/i, |
6 | | - areaRegex = /^[a-z]{1,2}/i, |
7 | | - districtSplitRegex = /^([a-z]{1,2}\d)([a-z])$/i, |
8 | | - sectorRegex = /^[a-z]{1,2}\d[a-z\d]?\s*\d/i, |
9 | | - unitRegex = /[a-z]{2}$/i; |
10 | | - |
11 | | -function isValidPostcode (postcode) { |
12 | | - return !!postcode.match(validationRegex); |
13 | | -} |
14 | | - |
15 | | -function parseOutcode (postcode) { |
16 | | - return postcode.replace(incodeRegex, "").replace(/\s+/, ""); |
17 | | -} |
18 | | - |
19 | | -function parseIncode (postcode) { |
20 | | - return postcode.match(incodeRegex)[0]; |
21 | | -} |
22 | | - |
23 | | -function parseArea (postcode) { |
24 | | - return postcode.match(areaRegex)[0]; |
25 | | -} |
26 | | - |
27 | | -function parseSector (postcode) { |
28 | | - return postcode.match(sectorRegex)[0]; |
29 | | -} |
30 | | - |
31 | | -function parseUnit (postcode) { |
32 | | - return postcode.match(unitRegex)[0]; |
33 | | -} |
34 | | - |
35 | | -function Postcode (rawPostcode) { |
36 | | - this._raw = rawPostcode; |
37 | | - this._valid = isValidPostcode(rawPostcode); |
38 | | -} |
39 | | - |
40 | | -Postcode.validOutcode = function (outcode) { |
41 | | - return !!outcode.match(validOutcodeRegex); |
42 | | -} |
43 | | - |
44 | | -Postcode.prototype.valid = function () { |
45 | | - return this._valid; |
46 | | -} |
47 | | - |
48 | | -Postcode.prototype.incode = function () { |
49 | | - if (!this._valid) return null; |
50 | | - if (this._incode) return this._incode; |
51 | | - this._incode = parseIncode(this._raw).toUpperCase(); |
52 | | - return this._incode; |
53 | | -} |
54 | | - |
55 | | -Postcode.prototype.outcode = function () { |
56 | | - if (!this._valid) return null; |
57 | | - if (this._outcode) return this._outcode; |
58 | | - this._outcode = parseOutcode(this._raw).toUpperCase(); |
59 | | - return this._outcode; |
60 | | -} |
61 | | - |
62 | | -Postcode.prototype.area = function () { |
63 | | - if (!this._valid) return null; |
64 | | - if (this._area) return this._area; |
65 | | - this._area = parseArea(this._raw).toUpperCase(); |
66 | | - return this._area; |
67 | | -} |
68 | | - |
69 | | -Postcode.prototype.district = function () { |
70 | | - if (!this._valid) return null; |
71 | | - if (this._district) return this._district; |
72 | | - var outcode = this.outcode(); |
73 | | - var split = outcode.match(districtSplitRegex); |
74 | | - this._district = split ? split[1] : outcode; |
75 | | - return this._district; |
76 | | -} |
77 | | - |
78 | | -Postcode.prototype.subDistrict = function () { |
79 | | - if (!this._valid) return null; |
80 | | - if (this._subDistrict) return this._subDistrict; |
81 | | - var outcode = this.outcode(); |
82 | | - var split = outcode.match(districtSplitRegex); |
83 | | - this._subDistrict = split ? outcode : null; |
84 | | - return this._subDistrict; |
85 | | -} |
86 | | - |
87 | | -Postcode.prototype.sector = function () { |
88 | | - if (!this._valid) return null; |
89 | | - if (this._sector) return this._sector; |
90 | | - this._sector = parseSector(this.normalise()).toUpperCase(); |
91 | | - return this._sector; |
92 | | -} |
93 | | - |
94 | | -Postcode.prototype.unit = function () { |
95 | | - if (!this._valid) return null; |
96 | | - if (this._unit) return this._unit; |
97 | | - this._unit = parseUnit(this._raw).toUpperCase(); |
98 | | - return this._unit; |
99 | | -} |
100 | | - |
101 | | -Postcode.prototype.normalise = function () { |
102 | | - if (!this._valid) return null; |
103 | | - if (this.postcode) return this.postcode; |
104 | | - return [this.outcode()," ", this.incode()].join(""); |
105 | | -} |
| 4 | + incodeRegex = /\d[a-z]{2}$/i, |
| 5 | + validOutcodeRegex = /^[a-z]{1,2}\d[a-z\d]?$/i, |
| 6 | + areaRegex = /^[a-z]{1,2}/i, |
| 7 | + districtSplitRegex = /^([a-z]{1,2}\d)([a-z])$/i, |
| 8 | + sectorRegex = /^[a-z]{1,2}\d[a-z\d]?\s*\d/i, |
| 9 | + unitRegex = /[a-z]{2}$/i; |
| 10 | + |
| 11 | +function isValidPostcode(postcode) { |
| 12 | + return !!postcode.match(validationRegex); |
| 13 | +} |
| 14 | + |
| 15 | +function parseOutcode(postcode) { |
| 16 | + return postcode.replace(incodeRegex, "").replace(/\s+/, ""); |
| 17 | +} |
| 18 | + |
| 19 | +function parseIncode(postcode) { |
| 20 | + return postcode.match(incodeRegex)[0]; |
| 21 | +} |
| 22 | + |
| 23 | +function parseArea(postcode) { |
| 24 | + return postcode.match(areaRegex)[0]; |
| 25 | +} |
| 26 | + |
| 27 | +function parseSector(postcode) { |
| 28 | + return postcode.match(sectorRegex)[0]; |
| 29 | +} |
| 30 | + |
| 31 | +function parseUnit(postcode) { |
| 32 | + return postcode.match(unitRegex)[0]; |
| 33 | +} |
| 34 | + |
| 35 | +function Postcode(rawPostcode) { |
| 36 | + this._raw = rawPostcode; |
| 37 | + this._valid = isValidPostcode(rawPostcode); |
| 38 | +} |
| 39 | + |
| 40 | +Postcode.validOutcode = function(outcode) { |
| 41 | + return !!outcode.match(validOutcodeRegex); |
| 42 | +}; |
| 43 | + |
| 44 | +Postcode.prototype.valid = function() { |
| 45 | + return this._valid; |
| 46 | +}; |
| 47 | + |
| 48 | +Postcode.prototype.incode = function() { |
| 49 | + if (!this._valid) return null; |
| 50 | + if (this._incode) return this._incode; |
| 51 | + this._incode = parseIncode(this._raw).toUpperCase(); |
| 52 | + return this._incode; |
| 53 | +}; |
| 54 | + |
| 55 | +Postcode.prototype.outcode = function() { |
| 56 | + if (!this._valid) return null; |
| 57 | + if (this._outcode) return this._outcode; |
| 58 | + this._outcode = parseOutcode(this._raw).toUpperCase(); |
| 59 | + return this._outcode; |
| 60 | +}; |
| 61 | + |
| 62 | +Postcode.prototype.area = function() { |
| 63 | + if (!this._valid) return null; |
| 64 | + if (this._area) return this._area; |
| 65 | + this._area = parseArea(this._raw).toUpperCase(); |
| 66 | + return this._area; |
| 67 | +}; |
| 68 | + |
| 69 | +Postcode.prototype.district = function() { |
| 70 | + if (!this._valid) return null; |
| 71 | + if (this._district) return this._district; |
| 72 | + var outcode = this.outcode(); |
| 73 | + var split = outcode.match(districtSplitRegex); |
| 74 | + this._district = split ? split[1] : outcode; |
| 75 | + return this._district; |
| 76 | +}; |
| 77 | + |
| 78 | +Postcode.prototype.subDistrict = function() { |
| 79 | + if (!this._valid) return null; |
| 80 | + if (this._subDistrict) return this._subDistrict; |
| 81 | + var outcode = this.outcode(); |
| 82 | + var split = outcode.match(districtSplitRegex); |
| 83 | + this._subDistrict = split ? outcode : null; |
| 84 | + return this._subDistrict; |
| 85 | +}; |
| 86 | + |
| 87 | +Postcode.prototype.sector = function() { |
| 88 | + if (!this._valid) return null; |
| 89 | + if (this._sector) return this._sector; |
| 90 | + this._sector = parseSector(this.normalise()).toUpperCase(); |
| 91 | + return this._sector; |
| 92 | +}; |
| 93 | + |
| 94 | +Postcode.prototype.unit = function() { |
| 95 | + if (!this._valid) return null; |
| 96 | + if (this._unit) return this._unit; |
| 97 | + this._unit = parseUnit(this._raw).toUpperCase(); |
| 98 | + return this._unit; |
| 99 | +}; |
| 100 | + |
| 101 | +Postcode.prototype.normalise = function() { |
| 102 | + if (!this._valid) return null; |
| 103 | + return [this.outcode(), " ", this.incode()].join(""); |
| 104 | +}; |
106 | 105 |
|
107 | 106 | module.exports = Postcode; |
0 commit comments