Skip to content

Commit f2a6fb1

Browse files
authored
Merge pull request #665 from alphagov/sanitize-postcode-dashes
Strip hyphens and en/em dashes from the postcode before validating
2 parents 2679c68 + fbd9ad3 commit f2a6fb1

2 files changed

Lines changed: 19 additions & 8 deletions

File tree

lib/postcode_sanitizer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module PostcodeSanitizer
22
def self.call(postcode)
3-
postcode.to_s.gsub(/\s+/, "").upcase
3+
postcode.to_s.gsub(/\s+|-+|–+|—+/, "").upcase
44
end
55
end

spec/lib/postcode_sanitizer_spec.rb

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,29 @@
22
require 'postcode_sanitizer'
33

44
RSpec.describe PostcodeSanitizer do
5-
describe '.call' do
5+
describe ".call" do
66
it "removes all whitespace" do
7-
postcode = " N1 1TY "
8-
expect(described_class.call(postcode)).to eq "N11TY"
7+
expect(described_class.call(" N1 1TY ")).to eq "N11TY"
98
end
9+
1010
it "upcases the postcode" do
11-
postcode = "n11ty "
12-
expect(described_class.call(postcode)).to eq "N11TY"
11+
expect(described_class.call("n11ty ")).to eq "N11TY"
1312
end
13+
1414
it "removes whitespaces and upcase the postcode" do
15-
postcode = " N1 1ty "
16-
expect(described_class.call(postcode)).to eq "N11TY"
15+
expect(described_class.call(" N1 1ty ")).to eq "N11TY"
16+
end
17+
18+
it "removes hypens and upcases the postcode" do
19+
expect(described_class.call(" N1-1ty ")).to eq "N11TY"
20+
end
21+
22+
it "removes en dashes and upcases the postcode" do
23+
expect(described_class.call(" N1–1ty ")).to eq "N11TY"
24+
end
25+
26+
it "removes em dashes and upcases the postcode" do
27+
expect(described_class.call(" N1—1ty ")).to eq "N11TY"
1728
end
1829
end
1930
end

0 commit comments

Comments
 (0)