From 9e993ed86a0c5f718f1bdd209cee180ea45ce409 Mon Sep 17 00:00:00 2001 From: Maarten Huijsmans Date: Thu, 19 Dec 2024 19:33:39 +0000 Subject: [PATCH] Add dot_case --- src/orval/__init__.py | 3 ++- src/orval/strings.py | 23 +++++++++++++++++++++++ tests/test_strings.py | 33 ++++++++++++++++++++++++++++++++- 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/orval/__init__.py b/src/orval/__init__.py index f3ae007..e388ad8 100644 --- a/src/orval/__init__.py +++ b/src/orval/__init__.py @@ -6,13 +6,14 @@ from orval.byte_utils import pretty_bytes from orval.datetimes import utcnow from orval.hashing import hashify -from orval.strings import camel_case, kebab_case, pascal_case, slugify, snake_case, train_case, truncate +from orval.strings import camel_case, dot_case, kebab_case, pascal_case, slugify, snake_case, train_case, truncate from orval.utils import timing __version__ = metadata.version(__package__) __all__ = [ "camel_case", "chunkify", + "dot_case", "flatten", "hashify", "kebab_case", diff --git a/src/orval/strings.py b/src/orval/strings.py index 17ff85e..4e198f7 100644 --- a/src/orval/strings.py +++ b/src/orval/strings.py @@ -184,6 +184,29 @@ def snake_case(string: str, scream: bool = False, unicode: bool = True, compact_ return text.lower() if not scream else text.upper() +def dot_case(string: str, scream: bool = False, unicode: bool = True, compact_spaces: bool = True) -> str: + """Convert a string to dot.case. + + Parameters + ---------- + string + Input string to transform. + scream + Convert the output to uppercase. + unicode + If True, allows Unicode characters in the output string. If False, only ASCII characters are allowed (default is True). + compact_spaces + If True, multiple consecutive spaces are reduced to a single space (default is True). + + Returns + ------- + str + Returns a transformed string. + """ + text = _normalize(string, unicode=unicode, compact_spaces=compact_spaces).replace(" ", ".") + return text.lower() if not scream else text.upper() + + def truncate(string: str, number: int, /, suffix: str = "...") -> str: """Truncate a string to a certain number of characters.""" if number <= 0: diff --git a/tests/test_strings.py b/tests/test_strings.py index b472c8f..1008d32 100644 --- a/tests/test_strings.py +++ b/tests/test_strings.py @@ -2,7 +2,7 @@ import pytest -from orval import camel_case, kebab_case, pascal_case, slugify, snake_case, train_case, truncate +from orval import camel_case, dot_case, kebab_case, pascal_case, slugify, snake_case, train_case, truncate @pytest.mark.parametrize( @@ -177,6 +177,37 @@ def test_snake_case(string: str, scream: bool, expected: str) -> None: assert snake_case(string, scream) == expected +@pytest.mark.parametrize( + ("string", "scream", "expected"), + [ + ("great scott", False, "great.scott"), + ("Great Scott", False, "great.scott"), + ("great", False, "great"), + ("hello world again", False, "hello.world.again"), + (" great scott ", False, "great.scott"), + ("GREAT SCOTT", False, "great.scott"), + ("GREAT SCOTT", True, "GREAT.SCOTT"), + ("", False, ""), + ("", True, ""), + ("single", False, "single"), + ("multiple words in a string", False, "multiple.words.in.a.string"), + ("!!", False, ""), + ("great scott", True, "GREAT.SCOTT"), + ("Great Scott", True, "GREAT.SCOTT"), + ("hello", True, "HELLO"), + ("hello world again", True, "HELLO.WORLD.AGAIN"), + (" great scott ", True, "GREAT.SCOTT"), + ("single", True, "SINGLE"), + ("multiple words in a string", True, "MULTIPLE.WORDS.IN.A.STRING"), + ("!!öì 💩", True, "ÖÌ"), + ("!!ö ì 💩", True, "Ö.Ì"), + ], +) +def test_dot_case(string: str, scream: bool, expected: str) -> None: + """Should convert a string to dot.case.""" + assert dot_case(string, scream) == expected + + @pytest.mark.parametrize( ("string", "number", "suffix", "expected"), [