Case conversion: the case functions of Effect's String module.
The word-based converters split their input into words at case changes
("camelCase", "CAMELCase"), letter/digit boundaries and any run of
characters other than ASCII letters and digits, then join the words again::
case.snake_case("helloWorld") # "hello_world"
case.camel_case("hello world") # "helloWorld"
case.constant_case("api-v2 xml") # "API_V_2_XML"
case.config_case("api-v2 xml") # "API_V2_XML" (digit groups kept)The snake_to_*, *_to_snake and kebab_to_snake functions only
swap separators or split at capitals, for input known to be in that case.
Functions#
no_case#
no_case(text, *, split=_SPLIT, strip=_STRIP, delimiter=' ', transform=str.lower) -> strThe words of text, each through transform, joined by
delimiter (no_case("helloWorld") is "hello world").
Each split pattern has two groups, and a word boundary goes between
them; each match of a strip pattern becomes a boundary. Unlike a
JavaScript regex without the g flag, a pattern replaces every match.
| Parameter | Type | Description |
|---|---|---|
text | str | |
split | Patterns = _SPLIT | |
strip | Patterns = _STRIP | |
delimiter | str = ' ' | |
transform | Callable[[str], str] = str.lower |
Returns str
def no_case(
text: str,
*,
split: Patterns = _SPLIT,
strip: Patterns = _STRIP,
delimiter: str = " ",
transform: Callable[[str], str] = str.lower,
) -> str:
"""The words of ``text``, each through ``transform``, joined by
``delimiter`` (``no_case("helloWorld")`` is ``"hello world"``).
Each ``split`` pattern has two groups, and a word boundary goes between
them; each match of a ``strip`` pattern becomes a boundary. Unlike a
JavaScript regex without the ``g`` flag, a pattern replaces every match.
"""
words = _words(text, _patterns(split), _patterns(strip))
return delimiter.join(transform(word) for word in words)pascal_case#
pascal_case(text) -> str"hello world" -> "HelloWorld".
| Parameter | Type | Description |
|---|---|---|
text | str |
Returns str
def pascal_case(text: str) -> str:
"""``"hello world"`` -> ``"HelloWorld"``."""
return no_case(text, delimiter="", transform=_capitalized)camel_case#
camel_case(text) -> str"hello world" -> "helloWorld".
| Parameter | Type | Description |
|---|---|---|
text | str |
Returns str
def camel_case(text: str) -> str:
"""``"hello world"`` -> ``"helloWorld"``."""
words = _words(text, _SPLIT, (_STRIP,))
return "".join(w.lower() if i == 0 else _capitalized(w) for i, w in enumerate(words))constant_case#
constant_case(text) -> str"hello world" -> "HELLO_WORLD"; digits split off
("api-v2" -> "API_V_2").
| Parameter | Type | Description |
|---|---|---|
text | str |
Returns str
def constant_case(text: str) -> str:
"""``"hello world"`` -> ``"HELLO_WORLD"``; digits split off
(``"api-v2"`` -> ``"API_V_2"``)."""
return no_case(text, delimiter="_", transform=str.upper)config_case#
config_case(text) -> strCONSTANT_CASE that keeps digit groups ("api-v2 xml" ->
"API_V2_XML"), as configuration keys are written. Not idempotent:
"field2value" gives "FIELD2VALUE", which gives "FIELD2_VALUE".
| Parameter | Type | Description |
|---|---|---|
text | str |
Returns str
def config_case(text: str) -> str:
"""CONSTANT_CASE that keeps digit groups (``"api-v2 xml"`` ->
``"API_V2_XML"``), as configuration keys are written. Not idempotent:
``"field2value"`` gives ``"FIELD2VALUE"``, which gives ``"FIELD2_VALUE"``."""
return "_".join(word.upper() for word in _words(text, _CONFIG_SPLIT, (_STRIP,)))kebab_case#
kebab_case(text) -> str"helloWorld" -> "hello-world".
| Parameter | Type | Description |
|---|---|---|
text | str |
Returns str
def kebab_case(text: str) -> str:
"""``"helloWorld"`` -> ``"hello-world"``."""
return no_case(text, delimiter="-")snake_case#
snake_case(text) -> str"helloWorld" -> "hello_world".
| Parameter | Type | Description |
|---|---|---|
text | str |
Returns str
def snake_case(text: str) -> str:
"""``"helloWorld"`` -> ``"hello_world"``."""
return no_case(text, delimiter="_")snake_to_camel#
snake_to_camel(text) -> str"hello_world" -> "helloWorld": each _ is dropped and
the next character upper-cased.
| Parameter | Type | Description |
|---|---|---|
text | str |
Returns str
def snake_to_camel(text: str) -> str:
"""``"hello_world"`` -> ``"helloWorld"``: each ``_`` is dropped and
the next character upper-cased."""
return _from_snake(text, lambda c: c)snake_to_pascal#
snake_to_pascal(text) -> str"hello_world" -> "HelloWorld".
| Parameter | Type | Description |
|---|---|---|
text | str |
Returns str
def snake_to_pascal(text: str) -> str:
"""``"hello_world"`` -> ``"HelloWorld"``."""
return _from_snake(text, str.upper)snake_to_kebab#
snake_to_kebab(text) -> strEvery _ becomes -.
| Parameter | Type | Description |
|---|---|---|
text | str |
Returns str
def snake_to_kebab(text: str) -> str:
"""Every ``_`` becomes ``-``."""
return text.replace("_", "-")camel_to_snake#
camel_to_snake(text) -> str_ before every capital, then lower case:
"XMLHttpRequest" -> "_x_m_l_http_request".
| Parameter | Type | Description |
|---|---|---|
text | str |
Returns str
def camel_to_snake(text: str) -> str:
"""``_`` before every capital, then lower case:
``"XMLHttpRequest"`` -> ``"_x_m_l_http_request"``."""
return _CAPITAL.sub("_\\1", text).lower()pascal_to_snake#
pascal_to_snake(text) -> strcamel_to_snake that leaves the first character alone:
"XMLHttpRequest" -> "x_m_l_http_request".
| Parameter | Type | Description |
|---|---|---|
text | str |
Returns str
def pascal_to_snake(text: str) -> str:
"""``camel_to_snake`` that leaves the first character alone:
``"XMLHttpRequest"`` -> ``"x_m_l_http_request"``."""
return (text[:1] + _CAPITAL.sub("_\\1", text[1:])).lower()kebab_to_snake#
kebab_to_snake(text) -> strEvery - becomes _.
| Parameter | Type | Description |
|---|---|---|
text | str |
Returns str
def kebab_to_snake(text: str) -> str:
"""Every ``-`` becomes ``_``."""
return text.replace("-", "_")