postcodes: add support for optional spaces

This commit is contained in:
Sarah Hoffmann
2022-05-23 14:04:22 +02:00
parent 49626ba709
commit 9172696324
3 changed files with 30 additions and 4 deletions

View File

@@ -31,18 +31,24 @@ class _PostcodeMatcher:
pc_pattern = config['pattern'].replace('d', '[0-9]').replace('l', '[A-Z]') pc_pattern = config['pattern'].replace('d', '[0-9]').replace('l', '[A-Z]')
self.pattern = re.compile(f'(?:{country_code.upper()}[ -]?)?({pc_pattern})') self.norm_pattern = re.compile(f'\\s*(?:{country_code.upper()}[ -]?)?(.*)\\s*')
self.pattern = re.compile(pc_pattern)
self.output = config.get('output', r'\g<0>')
def normalize(self, postcode): def normalize(self, postcode):
""" Return the normalized version of the postcode. If the given postcode """ Return the normalized version of the postcode. If the given postcode
does not correspond to the usage-pattern, return null. does not correspond to the usage-pattern, return null.
""" """
normalized = postcode.strip().upper() # Upper-case, strip spaces and leading country code.
normalized = self.norm_pattern.fullmatch(postcode.upper())
match = self.pattern.fullmatch(normalized) if normalized:
match = self.pattern.fullmatch(normalized.group(1))
return match.expand(self.output) if match else None
return match.group(1) if match else None return None
class _PostcodeSanitizer: class _PostcodeSanitizer:

View File

@@ -456,6 +456,9 @@ cz:
partition: 124 partition: 124
languages: cs languages: cs
names: !include country-names/cz.yaml names: !include country-names/cz.yaml
postcode:
pattern: "(ddd) ?(dd)"
output: \1 \2
# Germany (Deutschland) # Germany (Deutschland)
@@ -1618,6 +1621,9 @@ se:
partition: 112 partition: 112
languages: sv languages: sv
names: !include country-names/se.yaml names: !include country-names/se.yaml
postcode:
pattern: "(ddd) ?(dd)"
output: \1 \2
# Singapore (Singapore) # Singapore (Singapore)
@@ -1657,6 +1663,9 @@ sk:
partition: 172 partition: 172
languages: sk languages: sk
names: !include country-names/sk.yaml names: !include country-names/sk.yaml
postcode:
pattern: "(ddd) ?(dd)"
output: \1 \2
# Sierra Leone (Sierra Leone) # Sierra Leone (Sierra Leone)

View File

@@ -77,3 +77,14 @@ def test_postcode_kazakhstan_pass(sanitize, postcode):
def test_postcode_kazakhstan_fail(sanitize, postcode): def test_postcode_kazakhstan_fail(sanitize, postcode):
assert sanitize(country='kz', postcode=postcode) == [] assert sanitize(country='kz', postcode=postcode) == []
@pytest.mark.parametrize("postcode", ('675 34', '67534', 'SE-675 34', 'SE67534'))
def test_postcode_sweden_pass(sanitize, postcode):
assert sanitize(country='se', postcode=postcode) == [('postcode', '675 34')]
@pytest.mark.parametrize("postcode", ('67 345', '671123'))
@pytest.mark.sanitizer_params(convert_to_address=False)
def test_postcode_sweden_fail(sanitize, postcode):
assert sanitize(country='se', postcode=postcode) == []