Skip to content

Commit c27b1fa

Browse files
committed
Sketch out 5.0.0 changes
1 parent 9bcabc5 commit c27b1fa

2 files changed

Lines changed: 77 additions & 74 deletions

File tree

LEGACY.md

Lines changed: 0 additions & 43 deletions
This file was deleted.

README.md

Lines changed: 77 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,25 @@ Tested against ~1.7 million postcodes on ONSPD.
4646
npm install postcode
4747
```
4848

49-
### Wield
50-
51-
```javascript
52-
import Postcode from "postcode";
53-
```
54-
5549
### Validate
5650

5751
```javascript
58-
Postcode.isValid("AA1 1AB"); // => true
52+
import { isValid } from "postcode";
53+
54+
isValid("AA1 1AB"); // => true
5955
```
6056

6157
### Parse
6258

63-
Pass a string to `Postcode.parse`. This will return a valid or invalid postcode instance which can be easily destructured.
59+
Pass a string to `parse`. This will return a valid or invalid postcode instance which can be easily destructured.
6460

65-
#### Valid Postcode
61+
#### Valid Postcode `ValidPostcode`
62+
63+
```javascript
64+
import { parse } from "postcode";
6665

67-
```
6866
const {
69-
normalised, // => "SW1A 2AA"
67+
postcode, // => "SW1A 2AA"
7068
outcode, // => "SW1A"
7169
incode, // => "2AA"
7270
area, // => "SW"
@@ -75,14 +73,14 @@ const {
7573
sector, // => "SW1A 2"
7674
subDistrict, // => "SW1A"
7775
valid, // => true
78-
} = Postcode.parse("Sw1A 2aa");
76+
} = parse("Sw1A 2aa");
7977
```
8078

81-
#### Invalid postcode
79+
#### Invalid Postcode `InvalidPostcode`
8280

83-
```
81+
```javascript
8482
const {
85-
normalised, // => null
83+
postcode, // => null
8684
outcode, // => null
8785
incode, // => null
8886
area, // => null
@@ -91,10 +89,29 @@ const {
9189
sector, // => null
9290
subDistrict, // => null
9391
valid, // => false
94-
} = Postcode.parse(" Oh no, ): ");
92+
} = parse(" Oh no, ): ");
9593
```
9694

97-
#### Accessor Overview
95+
#### Type Guard
96+
97+
The TypeScript compiler can infer if you have a valid postcode type from `parse` by checking the `valid` attribute
98+
99+
```
100+
import { parse } from "postcode";
101+
102+
const postcode = parse("SW1A 2AA");
103+
104+
if (postcode.valid) {
105+
// `postcode` adheres to the `ValidPostcode` interface
106+
processString(postcode.outcode.toLowerCase()); // TypeScript compiler knows `outcode` to be a string
107+
processString(postcode.subDistrict.toLowerCase()); // And it will throw errors on common gotchas (e.g. subdistrict can be `null` on a valid postcode)
108+
} else {
109+
// `postcode` adheres to the `InvalidPostcode` interface
110+
processInvalidPostcode(postcode);
111+
}
112+
```
113+
114+
#### Valid Postcode Object
98115

99116
| Postcode | .outcode | .incode | .area | .district | .subDistrict | .sector | .unit |
100117
|----------|----------|---------|-------|-----------|--------------|---------|-------|
@@ -105,26 +122,55 @@ const {
105122
| AA9 9AA | AA9 | 9AA | AA | AA9 | `null` | AA9 9 | AA |
106123
| AA99 9AA | AA99 | 9AA | AA | AA99 | `null` | AA99 9 | AA |
107124

108-
### Static Methods
125+
### Exported Methods
126+
127+
If you're just after a single value, you can import a single method.
128+
129+
```javascript
130+
# Validation
131+
isValid("Sw1A 2aa"); // => true
132+
133+
# Formatting
134+
toNormalised("Sw1A 2aa"); // => "SW1A 2AA"
135+
toOutcode("Sw1A 2aa"); // => "SW1A"
136+
toIncode("Sw1A 2aa"); // => "2AA"
137+
toArea("Sw1A 2aa"); // => "AA"
138+
toDistrict("Sw1A 2aa"); // => "SW1"
139+
toSubDistrict("Sw1A 2aa"); // => "SW1A"
140+
toSector("Sw1A 2aa"); // => "SW1A 2"
141+
toUnit("Sw1A 2aa"); // => "AA"
142+
143+
# Replacement
144+
match("The PM and her no.2 live at SW1A2AA and SW1A 2AB"); // => ["SW1A2AA", "SW1A 2AB"]
145+
replace("The PM and her no.2 live at SW1A2AA and SW1A 2AB"); // => { match: ["SW1A2AA", "SW1A 2AB"], result: "The PM and her no.2 live at and " }
146+
```
147+
148+
## Version 5.0.0
149+
150+
5.0.0 brings changes which allows for better treeshaking and interopability with ES Modules. It also deprecates legacy class based APIs in favour of single purpose methods.
109151

110-
If you're just after a single value, you would be better served by calling a static method on `Postcode`.
152+
### Breaking Changes
111153

154+
- `postcode` no longer exports a class. Legacy `new Postcode()` functionality has been removed. Methods attached to `Postcode` are all available as named exports.
155+
- `postcode` no longer uses default exports. All exports are named. E.g.
112156
```javascript
113-
Postcode.isValid("Sw1A 2aa"); // => true
114-
115-
Postcode.toNormalised("Sw1A 2aa"); // => "SW1A 2AA"
116-
Postcode.toOutcode("Sw1A 2aa"); // => "SW1A"
117-
Postcode.toIncode("Sw1A 2aa"); // => "2AA"
118-
Postcode.toArea("Sw1A 2aa"); // => "AA"
119-
Postcode.toDistrict("Sw1A 2aa"); // => "SW1"
120-
Postcode.toSubDistrict("Sw1A 2aa"); // => "SW1A"
121-
Postcode.toSector("Sw1A 2aa"); // => "SW1A 2"
122-
Postcode.toUnit("Sw1A 2aa"); // => "AA"
157+
// In <= 4.0.0
158+
import Postcode from "postcode";
159+
Postcode.parse("SW1A 2AA");
160+
161+
// In >= 5.0.0
162+
import { parse } from "postcode";
163+
parse("SW1A 2AA");
123164
```
124165

125-
### Legacy API
166+
In many cases, migration can be achieved by changing `import Postcode from "postcode"` to `import * as Postcode from "postcode"`, however this gives up treeshaking advantages.
167+
168+
### New Features
126169

127-
The legacy object based API is documented in [LEGACY.md](LEGACY.md)
170+
- `postcode` now exports a ES Module build
171+
- Exports regular expressions
172+
- `match` accepts a string and returns all valid postcodes
173+
- `replace` accepts a string and replaces valid postcodes with an optional second argument. Default replacement text is empty string `""`
128174

129175
## Definitions
130176

0 commit comments

Comments
 (0)