diff --git a/flatisfy/tests.py b/flatisfy/tests.py index 1b7e34c..b74e3d9 100644 --- a/flatisfy/tests.py +++ b/flatisfy/tests.py @@ -114,6 +114,15 @@ class TestTexts(unittest.TestCase): tools.normalize_string("avec ascenseur") ) + def test_whitespace_trim(self): + """ + Checks that trailing and beginning whitespaces are trimmed. + """ + self.assertEqual( + "rennes 35000", + tools.normalize_string(" Rennes 35000 ") + ) + def test_accents(self): """ Checks accents are replaced. diff --git a/flatisfy/tools.py b/flatisfy/tools.py index 59cc8eb..3c500cb 100644 --- a/flatisfy/tools.py +++ b/flatisfy/tools.py @@ -225,6 +225,9 @@ def normalize_string(string, lowercase=True, convert_arabic_numerals=True): # Collapse multiple spaces, replace tabulations and newlines by space string = re.sub(r"\s+", " ", string) + # Trim whitespaces + string = string.strip() + return string