|
1 | | -[](https://circleci.com/gh/ideal-postcodes/postcode) [](https://coveralls.io/github/ideal-postcodes/postcode?branch=master)   |
| 1 | +<h1 align="center"> |
| 2 | + < img src= "https://img.ideal-postcodes.co.uk/Postcode.js%[email protected]" alt= "Postcode.js"> |
| 3 | +</h1> |
2 | 4 |
|
3 | | -# Postcode |
| 5 | +> Validate & parse UK postcodes |
4 | 6 |
|
| 7 | +[](https://circleci.com/gh/ideal-postcodes/postcode) |
| 8 | +[](https://coveralls.io/github/ideal-postcodes/postcode?branch=master) |
| 9 | + |
| 10 | + |
| 11 | + |
5 | 12 | [](https://npm.runkit.com/postcode) |
6 | 13 |
|
7 | | -Utility methods for UK Postcodes. |
| 14 | +Utility methods for UK Postcodes, including validating the shape of a postcode, extracting postcode elements (like incodes, outcodes, areas and [more](#Definitions)). |
8 | 15 |
|
9 | | -Included is a test suite that tests against all postcodes listed in the Ordnance Survey's postcode dataset as of January 2014. |
| 16 | +Tested against ~1.7 million postcodes on ONSPD. |
| 17 | + |
| 18 | +## Features |
| 19 | + |
| 20 | +- [Check](#validate) whether a postcode conforms to the [correct format](https://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom#Formatting) |
| 21 | +- [Extract](#parse) useful elements of a postcode like incode, outcode, sector |
| 22 | +- [Single purpose static methods](#static-methods) |
| 23 | +- Tested against a list of ~1.7 million postcodes listed on ONS Postcode Directory |
| 24 | + |
| 25 | +## Links |
| 26 | + |
| 27 | +- [GitHub Repository](https://github.com/ideal-postcodes/openapi) |
| 28 | +- [API Documentation](https://postcodejs.ideal-postcodes.dev) |
| 29 | +- [Try postcode.js on RunKit](https://npm.runkit.com/postcode) |
| 30 | +- [Postcode element definitions](#definitions) |
| 31 | +- [Caveat on postcode validation](#note-on-postcode-Validation) |
| 32 | +- [NPM Package](https://www.npmjs.com/package/postcode) |
10 | 33 |
|
11 | 34 | ## Getting Started |
12 | 35 |
|
13 | | -Install with `npm install postcode` |
| 36 | +### Installation |
| 37 | + |
| 38 | +```bash |
| 39 | +npm install postcode |
| 40 | +``` |
| 41 | + |
| 42 | +### Wield |
| 43 | + |
| 44 | +```javascript |
| 45 | +import Postcode from "postcode"; |
| 46 | +``` |
| 47 | + |
| 48 | +### Validate |
| 49 | + |
| 50 | +```javascript |
| 51 | +Postcode.isValid("AA1 1AB"); // => true |
| 52 | +``` |
| 53 | + |
| 54 | +### Parse |
| 55 | + |
| 56 | +Pass a string to `Postcode.parse`. This will return a valid or invalid postcode instance which can be easily destructured. |
| 57 | + |
| 58 | +#### Valid Postcode |
| 59 | + |
| 60 | +``` |
| 61 | +const { |
| 62 | + normalised, // => "SW1A 2AA" |
| 63 | + outcode, // => "SW1A" |
| 64 | + incode, // => "2AA" |
| 65 | + area, // => "SW" |
| 66 | + district, // => "SW1" |
| 67 | + unit, // => "AA" |
| 68 | + sector, // => "SW1A 2" |
| 69 | + subDistrict, // => "SW1A" |
| 70 | + valid, // => true |
| 71 | +} = Postcode.parse("Sw1A 2aa"); |
| 72 | +``` |
| 73 | + |
| 74 | +#### Invalid postcode |
14 | 75 |
|
15 | | -Create an instance of Postcode to perform utility methods, like so |
| 76 | +``` |
| 77 | +const { |
| 78 | + normalised, // => null |
| 79 | + outcode, // => null |
| 80 | + incode, // => null |
| 81 | + area, // => null |
| 82 | + district, // => null |
| 83 | + unit, // => null |
| 84 | + sector, // => null |
| 85 | + subDistrict, // => null |
| 86 | + valid, // => false |
| 87 | +} = Postcode.parse(" Oh no, ): "); |
| 88 | +``` |
| 89 | + |
| 90 | +#### Accessor Overview |
| 91 | + |
| 92 | +| Postcode | .outcode | .incode | .area | .district | .subDistrict | .sector | .unit | |
| 93 | +|----------|----------|---------|-------|-----------|--------------|---------|-------| |
| 94 | +| AA9A 9AA | AA9A | 9AA | AA | AA9 | AA9A | AA9A 9 | AA | |
| 95 | +| A9A 9AA | A9A | 9AA | A | A9 | A9A | A9A 9 | AA | |
| 96 | +| A9 9AA | A9 | 9AA | A | A9 | `null` | A9 9 | AA | |
| 97 | +| A99 9AA | A99 | 9AA | A | A99 | `null` | A99 9 | AA | |
| 98 | +| AA9 9AA | AA9 | 9AA | AA | AA9 | `null` | AA9 9 | AA | |
| 99 | +| AA99 9AA | AA99 | 9AA | AA | AA99 | `null` | AA99 9 | AA | |
| 100 | + |
| 101 | +### Static Methods |
| 102 | + |
| 103 | +If you're just after a single value, you would be better served by calling a static method on `Postcode`. |
| 104 | + |
| 105 | +```javascript |
| 106 | +Postcode.isValid("Sw1A 2aa"); // => true |
| 107 | + |
| 108 | +Postcode.toNormalised("Sw1A 2aa"); // => "SW1A 2AA" |
| 109 | +Postcode.toOutcode("Sw1A 2aa"); // => "SW1A" |
| 110 | +Postcode.toIncode("Sw1A 2aa"); // => "2AA" |
| 111 | +Postcode.toArea("Sw1A 2aa"); // => "AA" |
| 112 | +Postcode.toDistrict("Sw1A 2aa"); // => "SW1" |
| 113 | +Postcode.toSubDistrict("Sw1A 2aa"); // => "SW1A" |
| 114 | +Postcode.toSector("Sw1A 2aa"); // => "SW1A 2" |
| 115 | +Postcode.toUnit("Sw1A 2aa"); // => "AA" |
| 116 | +``` |
| 117 | + |
| 118 | +### Older API |
| 119 | + |
| 120 | +Below documents the old validation API, which continues to be supported. |
| 121 | + |
| 122 | +Create an instance of Postcode to perform utility methods, like so: |
16 | 123 |
|
17 | 124 | ```javascript |
18 | 125 | const Postcode = require("postcode"); |
@@ -91,23 +198,20 @@ The postcode sector is made up of the postcode district, the single space, and t |
91 | 198 |
|
92 | 199 | The postcode unit is two characters added to the end of the postcode sector. Each postcode unit generally represents a street, part of a street, a single address, a group of properties, a single property, a sub-section of the property, an individual organisation or (for instance Driver and Vehicle Licensing Agency) a subsection of the organisation. The level of discrimination is often based on the amount of mail received by the premises or business. Examples of postcode units include "NY" (from "SW1W 0NY"), "GZ" (from "PO16 7GZ"), "HF" (from "GU16 7HF"), or "JQ" (from "L1 8JQ"). |
93 | 200 |
|
94 | | -Sources: |
95 | | - |
96 | | -- https://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom#Formatting |
97 | | -- https://en.wikipedia.org/wiki/London_postal_district#Numbered_divisions |
98 | | - |
99 | | -## Testing |
100 | | - |
101 | | -```npm test``` |
102 | | - |
103 | 201 | ## Note on Postcode Validation |
104 | 202 |
|
105 | 203 | Postcodes cannot be validated just with a regular expression. Proper postcode validation requires having a full list of postcodes to check against. Relying on a regex will produce false postives/negatives. |
106 | 204 |
|
107 | 205 | A complete list of Postcodes can be obtained from the ONS Postcode Directory, which is updated every 3 months. |
108 | 206 |
|
| 207 | +## Testing |
| 208 | + |
| 209 | +```bash |
| 210 | +npm test |
| 211 | +``` |
| 212 | + |
109 | 213 | ## License |
110 | 214 |
|
111 | 215 | MIT |
112 | 216 |
|
113 | | -Contains Ordnance Survey Data © Crown Copyright & Database Right 2014 |
| 217 | +Contains Ordnance Survey Data © Crown Copyright & Database Right |
0 commit comments