-
Notifications
You must be signed in to change notification settings - Fork 4
/
stringcase.py
244 lines (157 loc) · 4.44 KB
/
stringcase.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
"""
String convert functions
"""
import re
def camelcase(string):
""" Convert string into camel case.
Args:
string: String to convert.
Returns:
string: Camel case string.
"""
string = re.sub(r"^[\-_\.]", '', str(string))
if not string:
return string
return lowercase(string[0]) + re.sub(r"[\-_\.\s]([a-z])", lambda matched: uppercase(matched.group(1)), string[1:])
def capitalcase(string):
"""Convert string into capital case.
First letters will be uppercase.
Args:
string: String to convert.
Returns:
string: Capital case string.
"""
string = str(string)
if not string:
return string
return uppercase(string[0]) + string[1:]
def constcase(string):
"""Convert string into upper snake case.
Join punctuation with underscore and convert letters into uppercase.
Args:
string: String to convert.
Returns:
string: Const cased string.
"""
return uppercase(snakecase(string))
def lowercase(string):
"""Convert string into lower case.
Args:
string: String to convert.
Returns:
string: Lowercase case string.
"""
return str(string).lower()
def pascalcase(string):
"""Convert string into pascal case.
Args:
string: String to convert.
Returns:
string: Pascal case string.
"""
return capitalcase(camelcase(string))
def pathcase(string):
"""Convert string into path case.
Join punctuation with slash.
Args:
string: String to convert.
Returns:
string: Path cased string.
"""
string = snakecase(string)
if not string:
return string
return re.sub(r"_", "/", string)
def backslashcase(string):
"""Convert string into spinal case.
Join punctuation with backslash.
Args:
string: String to convert.
Returns:
string: Spinal cased string.
"""
str1 = re.sub(r"_", r"\\", snakecase(string))
return str1
def sentencecase(string):
"""Convert string into sentence case.
First letter capped and each punctuations are joined with space.
Args:
string: String to convert.
Returns:
string: Sentence cased string.
"""
joiner = ' '
string = re.sub(r"[\-_\.\s]", joiner, str(string))
if not string:
return string
return capitalcase(trimcase(
re.sub(r"[A-Z]", lambda matched: joiner +
lowercase(matched.group(0)), string)
))
def snakecase(string):
"""Convert string into snake case.
Join punctuation with underscore
Args:
string: String to convert.
Returns:
string: Snake cased string.
"""
string = re.sub(r"[\-\.\s]", '_', str(string))
if not string:
return string
return lowercase(string[0]) + re.sub(r"[A-Z]", lambda matched: '_' + lowercase(matched.group(0)), string[1:])
def spinalcase(string):
"""Convert string into spinal case.
Join punctuation with hyphen.
Args:
string: String to convert.
Returns:
string: Spinal cased string.
"""
return re.sub(r"_", "-", snakecase(string))
def dotcase(string):
"""Convert string into dot case.
Join punctuation with dot.
Args:
string: String to convert.
Returns:
string: Dot cased string.
"""
return re.sub(r"_", ".", snakecase(string))
def titlecase(string):
"""Convert string into sentence case.
First letter capped while each punctuations is capitalsed
and joined with space.
Args:
string: String to convert.
Returns:
string: Title cased string.
"""
return ' '.join(
[capitalcase(word) for word in snakecase(string).split("_")]
)
def trimcase(string):
"""Convert string into trimmed string.
Args:
string: String to convert.
Returns:
string: Trimmed case string
"""
return str(string).strip()
def uppercase(string):
"""Convert string into upper case.
Args:
string: String to convert.
Returns:
string: Uppercase case string.
"""
return str(string).upper()
def alphanumcase(string):
"""Cuts all non-alphanumeric symbols,
i.e. cuts all expect except 0-9, a-z and A-Z.
Args:
string: String to convert.
Returns:
string: String with cutted non-alphanumeric symbols.
"""
return ''.join(filter(str.isalnum, str(string)))