forked from okunishinishi/python-stringcase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringcase.py
More file actions
121 lines (84 loc) · 3.59 KB
/
stringcase.py
File metadata and controls
121 lines (84 loc) · 3.59 KB
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
"""Public string case conversion helpers."""
from __future__ import annotations
import re
# ---------------------------------------------------------------------------
# Internal helper
# ---------------------------------------------------------------------------
def _tokenize(string: object) -> list[str]:
"""Split an arbitrary string into lowercase word tokens.
Handles snake_case, kebab-case, space-separated, and PascalCase/camelCase
inputs uniformly.
"""
s: str = str(string)
# Insert a separator before each uppercase letter that follows a lowercase
# letter or digit (camelCase / PascalCase boundary).
s = re.sub(r"([a-z0-9])([A-Z])", r"\1_\2", s)
# Replace any run of non-alphanumeric characters with a single separator.
s = re.sub(r"[^a-zA-Z0-9]+", "_", s)
# Strip leading/trailing separators and split.
tokens: list[str] = [t for t in s.strip("_").split("_") if t]
return [t.lower() for t in tokens]
# ---------------------------------------------------------------------------
# Public API
# ---------------------------------------------------------------------------
def camelcase(string: object) -> str:
"""Convert *string* to camelCase."""
tokens: list[str] = _tokenize(string)
if not tokens:
return ""
return tokens[0] + "".join(t.capitalize() for t in tokens[1:])
def capitalcase(string: object) -> str:
"""Capitalise the first character of *string*, leaving the rest as-is."""
s: str = str(string)
if not s:
return ""
return s[0].upper() + s[1:]
def constcase(string: object) -> str:
"""Convert *string* to CONST_CASE (SCREAMING_SNAKE_CASE)."""
return "_".join(t.upper() for t in _tokenize(string))
def lowercase(string: object) -> str:
"""Return *string* converted to all lower-case characters."""
return str(string).lower()
def pascalcase(string: object) -> str:
"""Convert *string* to PascalCase."""
return "".join(t.capitalize() for t in _tokenize(string))
def pathcase(string: object) -> str:
"""Convert *string* to path/case (forward-slash separated, lower-case)."""
tokens: list[str] = _tokenize(string)
if not tokens:
return ""
return "/".join(tokens)
def backslashcase(string: object) -> str:
r"""Convert *string* to back\slash\case (backslash separated, lower-case)."""
tokens: list[str] = _tokenize(string)
if not tokens:
return ""
return "\\".join(tokens)
def sentencecase(string: object) -> str:
"""Convert *string* to Sentence case."""
tokens: list[str] = _tokenize(string)
if not tokens:
return ""
sentence: str = " ".join(tokens)
return sentence[0].upper() + sentence[1:]
def snakecase(string: object) -> str:
"""Convert *string* to snake_case."""
return "_".join(_tokenize(string))
def spinalcase(string: object) -> str:
"""Convert *string* to spinal-case (kebab-case)."""
return "-".join(_tokenize(string))
def dotcase(string: object) -> str:
"""Convert *string* to dot.case."""
return ".".join(_tokenize(string))
def titlecase(string: object) -> str:
"""Convert *string* to Title Case."""
return " ".join(t.capitalize() for t in _tokenize(string))
def trimcase(string: object) -> str:
"""Strip leading and trailing whitespace from *string*."""
return str(string).strip()
def uppercase(string: object) -> str:
"""Return *string* converted to all upper-case characters."""
return str(string).upper()
def alphanumcase(string: object) -> str:
"""Strip all non-alphanumeric characters from *string*."""
return re.sub(r"[^a-zA-Z0-9]", "", str(string))