Add prepare_url_slash to string_helpers.py and tests

Function cleans up url paths (without domain) by ensuring they start with a single slash and removing double slashes.
This commit is contained in:
Clemens Schwaighofer
2025-10-23 15:47:19 +09:00
parent fe69530b38
commit 53cf2a6f48
3 changed files with 121 additions and 2 deletions

View File

@@ -2,6 +2,7 @@
String helpers
"""
import re
from decimal import Decimal, getcontext
from textwrap import shorten
@@ -101,4 +102,21 @@ def format_number(number: float, precision: int = 0) -> str:
"f}"
).format(_number)
def prepare_url_slash(url: str) -> str:
"""
if the URL does not start with /, add slash
strip all double slashes in URL
Arguments:
url {str} -- _description_
Returns:
str -- _description_
"""
url = re.sub(r'\/+', '/', url)
if not url.startswith("/"):
url = "/" + url
return url
# __END__