Total Complexity | 5 |
Total Lines | 38 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | import re |
||
2 | |||
3 | |||
4 | def snake(string: str): |
||
5 | """ |
||
6 | :param string: |
||
7 | :return: |
||
8 | """ |
||
9 | return re.sub("([A-Z])", "_\\1", string).lower().lstrip("_") |
||
10 | |||
11 | |||
12 | def camel(string: str): |
||
13 | """ |
||
14 | :param string: |
||
15 | :return: |
||
16 | """ |
||
17 | return "".join(map(str.capitalize, string.split("_"))) |
||
18 | |||
19 | |||
20 | class Conversion: |
||
21 | cases = { |
||
22 | 'snake': snake, |
||
23 | 'camel': camel |
||
24 | } |
||
25 | |||
26 | def __init__(self, case: str = None): |
||
27 | """ |
||
28 | :param case: |
||
29 | """ |
||
30 | self.case = self.cases['snake' if not case or case not in self.cases else case] |
||
31 | |||
32 | def encode(self, data: str): |
||
33 | """ |
||
34 | :param data: |
||
35 | :return: |
||
36 | """ |
||
37 | return self.case(data) |
||
38 |