Improve snake_case generation

This commit is contained in:
Igor Radovanovic
2023-08-29 11:36:06 +02:00
parent 1e19e45340
commit db859bdf4d

View File

@@ -99,5 +99,11 @@ def make_request(
def to_snake_case(string: str) -> str:
"""Convert a string to snake case."""
s1 = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", string)
return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower()
return (
re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1)
.lower()
.replace(" ", "_")
.replace("__", "_")
)