Skip to content

Commit e9af444

Browse files
committed
collapse white space when using string format, remove trailing comma if there is one
1 parent 0ef36ad commit e9af444

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

nameparser/parser.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def __next__(self):
121121
def __unicode__(self):
122122
if self.string_format:
123123
# string_format = "{title} {first} {middle} {last} {suffix} ({nickname})"
124-
return self.string_format.format(**self.as_dict())
124+
return self.collapse_whitespace(self.string_format.format(**self.as_dict())).strip(', ')
125125
return " ".join(self)
126126

127127
def __str__(self):
@@ -293,7 +293,10 @@ def full_name(self):
293293
def full_name(self, value):
294294
self._full_name = value
295295
self.parse_full_name()
296-
296+
297+
def collapse_whitespace(self, string):
298+
# collapse multiple spaces into single space
299+
return self.C.regexes.spaces.sub(" ", string.strip())
297300

298301
def pre_process(self):
299302
"""
@@ -360,8 +363,7 @@ def parse_full_name(self):
360363

361364
self.pre_process()
362365

363-
# collapse multiple spaces
364-
self._full_name = self.C.regexes.spaces.sub(" ", self._full_name.strip())
366+
self._full_name = self.collapse_whitespace(self._full_name)
365367

366368
# break up full_name by commas
367369
parts = [x.strip() for x in self._full_name.split(",")]

0 commit comments

Comments
 (0)