Add helpers.collapse_whitespace.

This commit is contained in:
voussoir 2020-09-19 03:50:55 -07:00
parent 1013fb81c1
commit b223691107
2 changed files with 8 additions and 7 deletions

View file

@ -104,6 +104,11 @@ def chunk_sequence(sequence, chunk_length, allow_incomplete=True):
if len(chunk) == chunk_length or allow_incomplete: if len(chunk) == chunk_length or allow_incomplete:
yield chunk yield chunk
def collapse_whitespace(s):
s = s.strip()
s = re.sub(r'\s+', ' ', s)
return s
def comma_space_split(s): def comma_space_split(s):
''' '''
Split the string apart by commas and spaces, discarding all extra Split the string apart by commas and spaces, discarding all extra

View file

@ -268,9 +268,7 @@ class Album(ObjectBase, GroupableMixin):
if not isinstance(title, str): if not isinstance(title, str):
raise TypeError(f'Title must be {str}, not {type(title)}.') raise TypeError(f'Title must be {str}, not {type(title)}.')
title = title.strip() title = helpers.collapse_whitespace(title)
for whitespace in string.whitespace:
title = title.replace(whitespace, ' ')
return title return title
@ -565,9 +563,7 @@ class Bookmark(ObjectBase):
if not isinstance(title, str): if not isinstance(title, str):
raise TypeError(f'Title must be {str}, not {type(title)}.') raise TypeError(f'Title must be {str}, not {type(title)}.')
title = title.strip() title = helpers.collapse_whitespace(title)
for whitespace in string.whitespace:
title = title.replace(whitespace, ' ')
return title return title
@ -1490,7 +1486,7 @@ class User(ObjectBase):
if not isinstance(display_name, str): if not isinstance(display_name, str):
raise TypeError(f'Display name must be string, not {type(display_name)}.') raise TypeError(f'Display name must be string, not {type(display_name)}.')
display_name = display_name.strip() display_name = helpers.collapse_whitespace(display_name)
if display_name == '': if display_name == '':
return None return None