Skip to content

Commit deb348b

Browse files
committed
feat(Replace): Implement replace
Replaces postcode in body of text, return any matches and updated corpus
1 parent 95f1d0e commit deb348b

File tree

3 files changed

+79
-6
lines changed

3 files changed

+79
-6
lines changed

README.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,10 @@ if (postcode.valid) {
127127
If you're just after a single value, you can import a single method.
128128

129129
```javascript
130-
# Validation
130+
// Validation
131131
isValid("Sw1A 2aa"); // => true
132132

133-
# Formatting
133+
// Formatting
134134
toNormalised("Sw1A 2aa"); // => "SW1A 2AA"
135135
toOutcode("Sw1A 2aa"); // => "SW1A"
136136
toIncode("Sw1A 2aa"); // => "2AA"
@@ -140,9 +140,25 @@ toSubDistrict("Sw1A 2aa"); // => "SW1A"
140140
toSector("Sw1A 2aa"); // => "SW1A 2"
141141
toUnit("Sw1A 2aa"); // => "AA"
142142

143-
# Replacement
144-
match("The PM and her no.2 live at SW1A2AA and SW1A 2AB"); // => ["SW1A2AA", "SW1A 2AB"]
143+
// Match
144+
// Retrieve valid postcodes in a body of text
145+
const matches = match("The PM and her no.2 live at SW1A2aa and SW1A 2AB"); // => ["SW1A2aa", "SW1A 2AB"]
146+
147+
// Perform transformations like normalisation postcodes using `.map` and `toNormalised`
148+
matches.map(toNormalised); // => ["SW1A 2AA", "SW1A 2AB"]
149+
150+
// No matches yields empty array
151+
match("Some London outward codes are SW1A, NW1 and E1"); // => []
152+
153+
// Replace
154+
// Replace postcodes in a body of text
145155
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 " }
156+
157+
// Add custom replacement
158+
replace("The PM lives at SW1A 2AA", "Downing Street"); // => { match: ["SW1A 2AA"], result: "The PM lives at Downing Street" };
159+
160+
// No match
161+
replace("Some London outward codes are SW1A, NW1 and E1"); // => { match: [], result: "Some London outward codes are SW1A, NW1 and E1" }
146162
```
147163

148164
## Version 5.0.0

lib/index.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,3 +296,24 @@ export const parse = (postcode: string): ValidPostcode | InvalidPostcode => {
296296
*/
297297
export const match = (corpus: string): string[] =>
298298
corpus.match(POSTCODE_CORPUS_REGEX) || [];
299+
300+
interface ReplaceResult {
301+
/**
302+
* List of matching postcodes found intext
303+
*/
304+
match: string[];
305+
/**
306+
* Body of text with postcodes replaced (with empty string by default)
307+
*/
308+
result: string;
309+
}
310+
311+
/**
312+
* Replaces postcodes in a body of text with a string
313+
*
314+
* By default the replacement string is empty string `""`
315+
*/
316+
export const replace = (corpus: string, replaceWith = ""): ReplaceResult => ({
317+
match: match(corpus),
318+
result: corpus.replace(POSTCODE_CORPUS_REGEX, replaceWith),
319+
});

test/unit.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { assert } from "chai";
22
import * as Postcode from "../lib/index";
33
import { loadFixtures, TestMethod } from "./util/helper";
44

5-
const { match } = Postcode;
5+
const { match, toNormalised, replace } = Postcode;
66

77
const testMethod: TestMethod = (options) => {
88
const { tests, method } = options;
@@ -129,11 +129,47 @@ describe("Unit parsing", () => {
129129
describe("match", () => {
130130
it("returns matching postcodes", () => {
131131
const corpus = `SW1A2Aa is the residence of the Prime Minister. SW1a 2AB is the residence of her no.2. SW1A 1AA is where the queen lives. They are located in the SW1A outcode`;
132-
assert.deepEqual(match(corpus), ["SW1A2Aa", "SW1a 2AB", "SW1A 1AA"]);
132+
const result = match(corpus);
133+
assert.deepEqual(result, ["SW1A2Aa", "SW1a 2AB", "SW1A 1AA"]);
134+
assert.deepEqual(result.map(toNormalised), [
135+
"SW1A 2AA",
136+
"SW1A 2AB",
137+
"SW1A 1AA",
138+
]);
133139
});
134140

135141
it("returns an empty array if no match", () => {
136142
const corpus = `SW1 NW1 E1 E2`;
137143
assert.deepEqual(match(corpus), []);
138144
});
139145
});
146+
147+
describe("replace", () => {
148+
it("replaces text and returns matching postcodes", () => {
149+
const corpus = `SW1A2Aa is the residence of the Prime Minister. SW1a 2AB is the residence of her no.2. SW1A 1AA is where the queen lives. They are located in the SW1A outcode`;
150+
const { match, result } = replace(corpus);
151+
assert.deepEqual(match, ["SW1A2Aa", "SW1a 2AB", "SW1A 1AA"]);
152+
assert.equal(
153+
result,
154+
` is the residence of the Prime Minister. is the residence of her no.2. is where the queen lives. They are located in the SW1A outcode`
155+
);
156+
});
157+
158+
it("replaces postcode with custom string", () => {
159+
const corpus = `SW1A2Aa is the residence of the Prime Minister. SW1a 2AB is the residence of her no.2. SW1A 1AA is where the queen lives. They are located in the SW1A outcode`;
160+
const replaceWith = "POSTCODE";
161+
const { match, result } = replace(corpus, replaceWith);
162+
assert.deepEqual(match, ["SW1A2Aa", "SW1a 2AB", "SW1A 1AA"]);
163+
assert.equal(
164+
result,
165+
`POSTCODE is the residence of the Prime Minister. POSTCODE is the residence of her no.2. POSTCODE is where the queen lives. They are located in the SW1A outcode`
166+
);
167+
});
168+
169+
it("returns an empty array if no match", () => {
170+
const corpus = `SW1 NW1 E1 E2`;
171+
const { match, result } = replace(corpus);
172+
assert.deepEqual(match, []);
173+
assert.deepEqual(result, corpus);
174+
});
175+
});

0 commit comments

Comments
 (0)