Skip to content

Commit be73bba

Browse files
derek73claude
andcommitted
fix: drop whitespace before punctuation when format field is empty (closes #139)
When string_format contains a field like "{last} {suffix}, {first}" and suffix is absent, the rendered string was "Smith , John" due to the space left between the empty field and the following comma. Added a post-format substitution step that strips whitespace before punctuation characters (,;:), consistent with the existing progressive cleanup pattern in __str__. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent c8ea8cf commit be73bba

3 files changed

Lines changed: 18 additions & 0 deletions

File tree

nameparser/config/regexes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
("period_not_at_end",re.compile(r'.*\..+$', re.I | re.U)),
2323
("emoji",re_emoji),
2424
("phd", re.compile(r'\s(ph\.?\s+d\.?)', re.I | re.U)),
25+
("space_before_punct", re.compile(r'\s+([,;:])', re.U)),
2526
])
2627
"""
2728
All regular expressions used by the parser are precompiled and stored in the config.

nameparser/parser.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ def __str__(self) -> str:
189189
_s = self.string_format.format(**self.as_dict())
190190
# remove trailing punctuation from missing nicknames
191191
_s = _s.replace(str(self.C.empty_attribute_default), '').replace(" ()", "").replace(" ''", "").replace(' ""', "")
192+
_s = self.C.regexes.space_before_punct.sub(r'\1', _s)
192193
return self.collapse_whitespace(_s).strip(', ')
193194
return " ".join(self)
194195

tests/test_output_format.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,22 @@ def test_formating_of_nicknames_in_middle(self) -> None:
103103
hn.nickname = ''
104104
self.assertEqual(str(hn), "Rev John A. Kenneth Doe III")
105105

106+
def test_empty_field_drops_surrounding_whitespace(self) -> None:
107+
# issue #139: adjacent whitespace/punctuation should be dropped when a field is empty
108+
hn = HumanName("John Smith")
109+
hn.string_format = "{last} {suffix}, {first}"
110+
self.assertEqual(str(hn), "Smith, John")
111+
112+
def test_empty_field_drops_surrounding_punctuation(self) -> None:
113+
hn = HumanName("John Smith")
114+
hn.string_format = "{title} {first} {last}"
115+
self.assertEqual(str(hn), "John Smith")
116+
117+
def test_empty_field_interior_comma_dropped(self) -> None:
118+
hn = HumanName("John Smith")
119+
hn.string_format = "{last}, {suffix} {first}"
120+
self.assertEqual(str(hn), "Smith, John")
121+
106122
def test_remove_emojis(self) -> None:
107123
hn = HumanName("Sam Smith 😊")
108124
self.m(hn.first, "Sam", hn)

0 commit comments

Comments
 (0)