[text] add 'sanitize_whitespace()'

This commit is contained in:
Mike Fährmann
2025-07-19 20:49:03 +02:00
parent 923e27f4fd
commit 7bb4053396
2 changed files with 16 additions and 0 deletions

View File

@@ -66,6 +66,11 @@ def slugify(value):
return re(r"[-\s]+").sub("-", value).strip("-_")
def sanitize_whitespace(value):
"""Replace all whitespace characters with a single space"""
return re(r"\s+").sub(" ", value.strip())
def ensure_http_scheme(url, scheme="https://"):
"""Prepend 'scheme' to 'url' if it doesn't have one"""
if url and not url.startswith(("https://", "http://")):

View File

@@ -106,6 +106,17 @@ class TestText(unittest.TestCase):
self.assertEqual(f(1), "1")
self.assertEqual(f(2.3), "23")
def test_sanitize_whitespace(self, f=text.sanitize_whitespace):
self.assertEqual(f("Hello World"), "Hello World")
self.assertEqual(f("Hello\tWorld"), "Hello World")
self.assertEqual(f(" Hello World "), "Hello World")
self.assertEqual(f("\tHello \n\tWorld "), "Hello World")
self.assertEqual(f(""), "")
self.assertEqual(f(" "), "")
self.assertEqual(f(" "), "")
self.assertEqual(f(" \t\n "), "")
def test_ensure_http_scheme(self, f=text.ensure_http_scheme):
result = "https://example.org/filename.ext"