Skip to content

Commit

Permalink
Add dot_case
Browse files Browse the repository at this point in the history
  • Loading branch information
lukin0110 committed Dec 19, 2024
1 parent 46795ce commit 9e993ed
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/orval/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
23 changes: 23 additions & 0 deletions src/orval/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
33 changes: 32 additions & 1 deletion tests/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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"),
[
Expand Down

0 comments on commit 9e993ed

Please sign in to comment.