-
Notifications
You must be signed in to change notification settings - Fork 0
/
casestyle.py
74 lines (61 loc) · 2.04 KB
/
casestyle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# coding=utf-8
import re
def camelcase(string):
s_list = casepreprocess(string)
if s_list:
string = ''.join(s.title() for s in s_list)
string = string[0].lower() + string[1:]
return string
return ''
def pascalcase(string):
s_list = casepreprocess(string)
if s_list:
return ''.join(s.title() for s in s_list)
return ''
def snakecase(string):
s_list = casepreprocess(string)
if s_list:
return '_'.join(s_list)
return ''
def macrocase(string):
return snakecase(string).upper()
def kebabcase(string):
s_list = casepreprocess(string)
if s_list:
return '-'.join(s_list)
return ''
def casepreprocess(string):
if isinstance(string, str):
string = string.strip().replace('-', ' ')
if string != '_' * len(string):
underscore_at_start = ''
if string.startswith('_'):
j = 1
for i, _ in enumerate(string):
if i < len(string):
if string[i + 1] == string[i]:
j += 1
else:
break
underscore_at_start = '_' * j
underscore_at_end = ''
if string.endswith('_'):
j = 1
for i in range(len(string) - 1, -1, -1):
if i > 0:
if string[i - 1] == string[i]:
j += 1
else:
break
underscore_at_end = '_' * j
else:
return []
string = string.replace('_', ' ')
string = '%s%s%s' % (underscore_at_start, string, underscore_at_end)
# string = f'{underscore_at_start}{string}{underscore_at_end}'
string = re.sub('(.)([A-Z][a-z]+)', r'\1 \2', string)
string = re.sub('([a-z0-9])([A-Z])', r'\1 \2', string).lower()
s_list = string.split()
return s_list
else:
raise TypeError("casepreprocess() argument must be str")