|
| 1 | +import re |
| 2 | + |
| 3 | +""" |
| 4 | +general info: |
| 5 | +https://en.wikipedia.org/wiki/Naming_convention_(programming)#Python_and_Ruby |
| 6 | +
|
| 7 | +pascal case [ an upper Camel Case ]: https://en.wikipedia.org/wiki/Camel_case |
| 8 | +
|
| 9 | +camel case: https://en.wikipedia.org/wiki/Camel_case |
| 10 | +
|
| 11 | +kebab case [ can be found in general info ]: |
| 12 | +https://en.wikipedia.org/wiki/Naming_convention_(programming)#Python_and_Ruby |
| 13 | +
|
| 14 | +snake case: https://en.wikipedia.org/wiki/Snake_case |
| 15 | +""" |
| 16 | + |
| 17 | +# assistant functions |
| 18 | +def split_input(str_: str) -> str: |
| 19 | + return [char.split() for char in re.split(r"[^ a-z A-Z 0-9 \s]", str_)] |
| 20 | + |
| 21 | + |
| 22 | +def to_simple_case(str_: str) -> str: |
| 23 | + string_split = split_input(str_) |
| 24 | + return "".join( |
| 25 | + ["".join([char.capitalize() for char in sub_str]) for sub_str in string_split] |
| 26 | + ) |
| 27 | + |
| 28 | + |
| 29 | +def to_complex_case(text: str, upper: bool, separator: str) -> str: |
| 30 | + try: |
| 31 | + string_split = split_input(text) |
| 32 | + if upper: |
| 33 | + res_str = "".join( |
| 34 | + [ |
| 35 | + separator.join([char.upper() for char in sub_str]) |
| 36 | + for sub_str in string_split |
| 37 | + ] |
| 38 | + ) |
| 39 | + else: |
| 40 | + res_str = "".join( |
| 41 | + [ |
| 42 | + separator.join([char.lower() for char in sub_str]) |
| 43 | + for sub_str in string_split |
| 44 | + ] |
| 45 | + ) |
| 46 | + return res_str |
| 47 | + except IndexError: |
| 48 | + return "not valid string" |
| 49 | + |
| 50 | + |
| 51 | +# main content |
| 52 | +def to_pascal_case(text: str) -> str: |
| 53 | + """ |
| 54 | + >>> to_pascal_case("one two 31235three4four") |
| 55 | + 'OneTwo31235three4four' |
| 56 | + """ |
| 57 | + return to_simple_case(text) |
| 58 | + |
| 59 | + |
| 60 | +def to_camel_case(text: str) -> str: |
| 61 | + """ |
| 62 | + >>> to_camel_case("one two 31235three4four") |
| 63 | + 'oneTwo31235three4four' |
| 64 | + """ |
| 65 | + try: |
| 66 | + res_str = to_simple_case(text) |
| 67 | + return res_str[0].lower() + res_str[1:] |
| 68 | + except IndexError: |
| 69 | + return "not valid string" |
| 70 | + |
| 71 | + |
| 72 | +def to_snake_case(text: str, upper: bool) -> str: |
| 73 | + """ |
| 74 | + >>> to_snake_case("one two 31235three4four", True) |
| 75 | + 'ONE_TWO_31235THREE4FOUR' |
| 76 | + >>> to_snake_case("one two 31235three4four", False) |
| 77 | + 'one_two_31235three4four' |
| 78 | + """ |
| 79 | + return to_complex_case(text, upper, "_") |
| 80 | + |
| 81 | + |
| 82 | +def to_kebab_case(text: str, upper: bool) -> str: |
| 83 | + """ |
| 84 | + >>> to_kebab_case("one two 31235three4four", True) |
| 85 | + 'ONE-TWO-31235THREE4FOUR' |
| 86 | + >>> to_kebab_case("one two 31235three4four", False) |
| 87 | + 'one-two-31235three4four' |
| 88 | + """ |
| 89 | + return to_complex_case(text, upper, "-") |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + __import__("doctest").testmod() |
0 commit comments