forked from ForeverEnjoy/easy2code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringconvertor.py
59 lines (39 loc) · 1.25 KB
/
stringconvertor.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
import re
def camelcase(string):
"""
Demo:
foo_bar_baz => fooBarBaz
FooBarBaz => fooBarBaz
"""
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 pascalcase(string):
return capitalcase(camelcase(string))
def capitalcase(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.
"""
return uppercase(snakecase(string))
def pathcase(string):
string = snakecase(string)
if not string:
return string
return re.sub(r"_", "/", string)
def snakecase(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):
return re.sub(r"_", "-", snakecase(string))
def uppercase(string):
return str(string).upper()
def lowercase(string):
return str(string).lower()