Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions algorithms/string/strip_url_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,16 @@ def strip_url_params2(url: str, param_to_strip: list[str] | None = None) -> str:
if "?" not in url:
return url

queries = (url.split("?")[1]).split("&")
query_keys = [query[0] for query in queries]
for index in range(len(query_keys) - 1, 0, -1):
if (
query_keys[index] in param_to_strip
or query_keys[index] in query_keys[0:index]
):
queries.pop(index)

return url.split("?")[0] + "?" + "&".join(queries)
domain, query_string = url.split("?", 1)
seen: set[str] = set()
queries: list[str] = []
for query in query_string.split("&"):
key = query.split("=", 1)[0]
if key not in param_to_strip and key not in seen:
seen.add(key)
queries.append(query)

return domain + "?" + "&".join(queries)


def strip_url_params3(url: str, strip: list[str] | None = None) -> str:
Expand Down
8 changes: 8 additions & 0 deletions tests/test_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,14 @@ def test_strip_url_params2(self):
strip_url_params2("www.saadbenn.com?a=1&b=2", ["b"]),
"www.saadbenn.com?a=1",
)
self.assertEqual(
strip_url_params2("https://x.test?ab=1&ac=2&ab=3"),
"https://x.test?ab=1&ac=2",
)
self.assertEqual(
strip_url_params2("https://x.test?ab=1&ac=2", ["ab"]),
"https://x.test?ac=2",
)

def test_strip_url_params3(self):
self.assertEqual(
Expand Down
Loading