Skip to content

Commit

Permalink
feat: add Utils.snake_to_pascal() & Utils.pascal_to_snake()
Browse files Browse the repository at this point in the history
  • Loading branch information
linkfrg committed Feb 11, 2025
1 parent cba319f commit a9ddcb7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/api/utils/str_cases.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
String Cases
=============

.. autofunction:: ignis.utils.Utils.snake_to_pascal

.. autofunction:: ignis.utils.Utils.pascal_to_snake
3 changes: 3 additions & 0 deletions ignis/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from .socket import send_socket, listen_socket
from .debounce import DebounceTask, debounce
from .get_monitors import get_monitors
from .str_cases import snake_to_pascal, pascal_to_snake


class Utils:
Expand Down Expand Up @@ -54,3 +55,5 @@ class Utils:
DebounceTask = DebounceTask
debounce = debounce
get_monitors = get_monitors
snake_to_pascal = snake_to_pascal
pascal_to_snake = pascal_to_snake
21 changes: 21 additions & 0 deletions ignis/utils/str_cases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import re


def snake_to_pascal(string: str) -> str:
"""
Convert a `snake_case` string to `PascalCase`.
Args:
string: the string to convert.
"""
return string.replace("_", " ").title().replace(" ", "")


def pascal_to_snake(string: str) -> str:
"""
Convert a `PascalCase` string to `snake_case`.
Args:
string: the string to convert.
"""
return re.sub(r"(?<!^)(?=[A-Z])", "_", string).lower()

0 comments on commit a9ddcb7

Please sign in to comment.