jsons._key_transformers.snakecase()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
"""
2
PRIVATE MODULE: do not import (from) it directly.
3
4
This module contains functions that can be used to transform keys of
5
dictionaries.
6
"""
7
import re
8
9
10
def camelcase(str_: str) -> str:
11
    """
12
    Return ``s`` in camelCase.
13
    :param str_: the string that is to be transformed.
14
    :return: a string in camelCase.
15
    """
16
    str_ = str_.replace('-', '_')
17
    splitted = str_.split('_')
18
    if len(splitted) > 1:
19
        str_ = ''.join([x.title() for x in splitted])
20
    return str_[0].lower() + str_[1:]
21
22
23
def snakecase(str_: str) -> str:
24
    """
25
    Return ``s`` in snake_case.
26
    :param str_: the string that is to be transformed.
27
    :return: a string in snake_case.
28
    """
29
    str_ = str_.replace('-', '_')
30
    str_ = str_[0].lower() + str_[1:]
31
    return re.sub(r'([a-z])([A-Z])', '\\1_\\2', str_).lower()
32
33
34
def pascalcase(str_: str) -> str:
35
    """
36
    Return ``s`` in PascalCase.
37
    :param str_: the string that is to be transformed.
38
    :return: a string in PascalCase.
39
    """
40
    camelcase_str = camelcase(str_)
41
    return camelcase_str[0].upper() + camelcase_str[1:]
42
43
44
def lispcase(str_: str) -> str:
45
    """
46
    Return ``s`` in lisp-case.
47
    :param str_: the string that is to be transformed.
48
    :return: a string in lisp-case.
49
    """
50
    return snakecase(str_).replace('_', '-')
51