From 8f5b1e721da832146fa8c9f193a421297291a24b Mon Sep 17 00:00:00 2001 From: Elijah Date: Thu, 7 Sep 2023 15:03:50 +0000 Subject: [PATCH] fix based on PR comments --- integration/test_event_chain.py | 12 ++++++------ reflex/utils/format.py | 18 +++++++++++++++++- tests/test_utils.py | 4 ++-- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/integration/test_event_chain.py b/integration/test_event_chain.py index c16bee4327..d562bc3408 100644 --- a/integration/test_event_chain.py +++ b/integration/test_event_chain.py @@ -97,11 +97,11 @@ def click_yield_nested(self): def redirect_return_chain(self): self.event_order.append("redirect_return_chain") - yield rx.redirect("/on_load_return_chain") + yield rx.redirect("/on-load-return-chain") def redirect_yield_chain(self): self.event_order.append("redirect_yield_chain") - yield rx.redirect("/on_load_yield_chain") + yield rx.redirect("/on-load-yield-chain") def click_return_int_type(self): self.event_order.append("click_return_int_type") @@ -367,7 +367,7 @@ def test_event_chain_click(event_chain, driver, button_id, exp_event_order): ("uri", "exp_event_order"), [ ( - "/on_load_return_chain", + "/on-load-return-chain", [ "on_load_return_chain", "event_arg:1", @@ -376,7 +376,7 @@ def test_event_chain_click(event_chain, driver, button_id, exp_event_order): ], ), ( - "/on_load_yield_chain", + "/on-load-yield-chain", [ "on_load_yield_chain", "event_arg:4", @@ -411,7 +411,7 @@ def test_event_chain_on_load(event_chain, driver, uri, exp_event_order): ("uri", "exp_event_order"), [ ( - "/on_mount_return_chain", + "/on-mount-return-chain", [ "on_load_return_chain", "event_arg:unmount", @@ -426,7 +426,7 @@ def test_event_chain_on_load(event_chain, driver, uri, exp_event_order): ], ), ( - "/on_mount_yield_chain", + "/on-mount-yield-chain", [ "on_load_yield_chain", "event_arg:mount", diff --git a/reflex/utils/format.py b/reflex/utils/format.py index b19e9f602d..e373331cf3 100644 --- a/reflex/utils/format.py +++ b/reflex/utils/format.py @@ -164,6 +164,21 @@ def to_title_case(text: str) -> str: return "".join(word.capitalize() for word in text.split("_")) +def to_kebab_case(text: str) -> str: + """Convert a string to kebab case. + + The words in the text are converted to lowercase and + separated by hyphens. + + Args: + text: The string to convert. + + Returns: + The title case string. + """ + return to_snake_case(text).replace("_", "-") + + def format_string(string: str) -> str: """Format the given string as a JS string literal.. @@ -207,6 +222,7 @@ def format_route(route: str, format_case=True) -> str: Args: route: The route to format. + format_case: whether to format case to kebab case. Returns: The formatted route. @@ -214,7 +230,7 @@ def format_route(route: str, format_case=True) -> str: route = route.strip("/") # Strip the route and format casing. if format_case: - route = to_snake_case(route) + route = to_kebab_case(route) # If the route is empty, return the index route. if route == "": diff --git a/tests/test_utils.py b/tests/test_utils.py index 931cf27eee..552b046e56 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -260,9 +260,9 @@ def test_is_generic_alias(cls: type, expected: bool): ("custom-route/", False, "custom-route"), ("/custom-route", True, "custom-route"), ("/custom-route", False, "custom-route"), - ("/custom_route", True, "custom_route"), + ("/custom_route", True, "custom-route"), ("/custom_route", False, "custom_route"), - ("/CUSTOM_route", True, "custom_route"), + ("/CUSTOM_route", True, "custom-route"), ("/CUSTOM_route", False, "CUSTOM_route"), ], )