Skip to content

Commit 939a415

Browse files
committed
Update docs
1 parent f8c83f0 commit 939a415

2 files changed

Lines changed: 62 additions & 9 deletions

File tree

README.md

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,27 @@ if (postcode.valid) {
126126

127127
If you're just after a single value, you can import a single method.
128128

129+
130+
#### Validation
131+
129132
```javascript
130-
// Validation
131133
isValid("Sw1A 2aa"); // => true
134+
```
135+
136+
#### Formatting
137+
138+
```
139+
import {
140+
toNormalised,
141+
toOutcode,
142+
toIncode,
143+
toArea,
144+
toDistrict,
145+
toSubDistrict,
146+
toSector,
147+
toUnit,
148+
} from "postcode";
132149
133-
// Formatting
134150
toNormalised("Sw1A 2aa"); // => "SW1A 2AA"
135151
toOutcode("Sw1A 2aa"); // => "SW1A"
136152
toIncode("Sw1A 2aa"); // => "2AA"
@@ -140,25 +156,33 @@ toSubDistrict("Sw1A 2aa"); // => "SW1A"
140156
toSector("Sw1A 2aa"); // => "SW1A 2"
141157
toUnit("Sw1A 2aa"); // => "AA"
142158
143-
// Match
144-
// Retrieve valid postcodes in a body of text
159+
#### Extract & Replace
160+
161+
`match`. Retrieve valid postcodes in a body of text
162+
163+
```javascript
145164
const matches = match("The PM and her no.2 live at SW1A2aa and SW1A 2AB"); // => ["SW1A2aa", "SW1A 2AB"]
146165
147166
// Perform transformations like normalisation postcodes using `.map` and `toNormalised`
148167
matches.map(toNormalised); // => ["SW1A 2AA", "SW1A 2AB"]
149168
150169
// No matches yields empty array
151170
match("Some London outward codes are SW1A, NW1 and E1"); // => []
171+
```
172+
173+
`replace`. Replace postcodes in a body of text, returning the updated corpus and any matching postcodes
152174

153-
// Replace
154-
// Replace postcodes in a body of text
155-
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 " }
175+
```javascript
176+
replace("The PM and her no.2 live at SW1A2AA and SW1A 2AB");
177+
// => { match: ["SW1A2AA", "SW1A 2AB"], result: "The PM and her no.2 live at and " }
156178

157179
// Add custom replacement
158-
replace("The PM lives at SW1A 2AA", "Downing Street"); // => { match: ["SW1A 2AA"], result: "The PM lives at Downing Street" };
180+
replace("The PM lives at SW1A 2AA", "Downing Street");
181+
// => { match: ["SW1A 2AA"], result: "The PM lives at Downing Street" };
159182

160183
// No match
161-
replace("Some London outward codes are SW1A, NW1 and E1"); // => { match: [], result: "Some London outward codes are SW1A, NW1 and E1" }
184+
replace("Some London outward codes are SW1A, NW1 and E1");
185+
// => { match: [], result: "Some London outward codes are SW1A, NW1 and E1" }
162186
```
163187

164188
## Version 5.0.0

lib/index.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,19 @@ export const parse = (postcode: string): ValidPostcode | InvalidPostcode => {
301301
* Searches a body of text for postcode matches
302302
*
303303
* Returns an empty array if no match
304+
*
305+
* @example
306+
*
307+
* ```
308+
* // Retrieve valid postcodes in a body of text
309+
* const matches = match("The PM and her no.2 live at SW1A2aa and SW1A 2AB"); // => ["SW1A2aa", "SW1A 2AB"]
310+
*
311+
* // Perform transformations like normalisation postcodes using `.map` and `toNormalised`
312+
* matches.map(toNormalised); // => ["SW1A 2AA", "SW1A 2AB"]
313+
*
314+
* // No matches yields empty array
315+
* match("Some London outward codes are SW1A, NW1 and E1"); // => []
316+
* ```
304317
*/
305318
export const match = (corpus: string): string[] =>
306319
corpus.match(POSTCODE_CORPUS_REGEX) || [];
@@ -323,6 +336,22 @@ interface ReplaceResult {
323336
* Replaces postcodes in a body of text with a string
324337
*
325338
* By default the replacement string is empty string `""`
339+
*
340+
* @example
341+
*
342+
* ```
343+
* // Replace postcodes in a body of text
344+
* replace("The PM and her no.2 live at SW1A2AA and SW1A 2AB");
345+
* // => { match: ["SW1A2AA", "SW1A 2AB"], result: "The PM and her no.2 live at and " }
346+
*
347+
* // Add custom replacement
348+
* replace("The PM lives at SW1A 2AA", "Downing Street");
349+
* // => { match: ["SW1A 2AA"], result: "The PM lives at Downing Street" };
350+
*
351+
* // No match
352+
* replace("Some London outward codes are SW1A, NW1 and E1");
353+
* // => { match: [], result: "Some London outward codes are SW1A, NW1 and E1" }
354+
* ```
326355
*/
327356
export const replace = (corpus: string, replaceWith = ""): ReplaceResult => ({
328357
match: match(corpus),

0 commit comments

Comments
 (0)