From 7f810ece217f7328f8dbde2d8cd69adcaaa548ac Mon Sep 17 00:00:00 2001 From: Elijah Ahianyo Date: Mon, 20 Nov 2023 19:33:48 +0000 Subject: [PATCH] To camel case fix (#2205) --- reflex/utils/format.py | 6 ++++-- tests/utils/test_format.py | 5 +++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/reflex/utils/format.py b/reflex/utils/format.py index 8b7eaaf6b..03ee5c9c7 100644 --- a/reflex/utils/format.py +++ b/reflex/utils/format.py @@ -137,9 +137,11 @@ def to_camel_case(text: str) -> str: Returns: The camel case string. """ - words = re.split("[_-]", text) + words = re.split("[_-]", text.lstrip("-_")) + leading_underscores_or_hyphens = "".join(re.findall(r"^[_-]+", text)) # Capitalize the first letter of each word except the first one - return words[0] + "".join(x.capitalize() for x in words[1:]) + converted_word = words[0] + "".join(x.capitalize() for x in words[1:]) + return leading_underscores_or_hyphens + converted_word def to_title_case(text: str) -> str: diff --git a/tests/utils/test_format.py b/tests/utils/test_format.py index 00a14c89b..c83fd8c79 100644 --- a/tests/utils/test_format.py +++ b/tests/utils/test_format.py @@ -142,6 +142,11 @@ def test_to_snake_case(input: str, output: str): ("kebab-case", "kebabCase"), ("kebab-case-two", "kebabCaseTwo"), ("snake_kebab-case", "snakeKebabCase"), + ("_hover", "_hover"), + ("-starts-with-hyphen", "-startsWithHyphen"), + ("--starts-with-double-hyphen", "--startsWithDoubleHyphen"), + ("_starts_with_underscore", "_startsWithUnderscore"), + ("__starts_with_double_underscore", "__startsWithDoubleUnderscore"), ], ) def test_to_camel_case(input: str, output: str):