-
Notifications
You must be signed in to change notification settings - Fork 0
/
caseit.go
311 lines (260 loc) · 7.02 KB
/
caseit.go
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package caseit
import (
"fmt"
"regexp"
"strings"
"unicode"
// titlecase "github.com/igorsobreira/titlecase"
// titlecase "github.com/KenjiTakahashi/tu/titlecase"
"github.com/runeimp/titlecase"
)
const Version = "1.0.0"
var (
spaceRE = regexp.MustCompile(`\s+`)
UseCodeBreak = false
)
// Idea modified from
// https://github.com/serenize/snaker/blob/master/snaker.go#L102
var commonInitialismsAndOddballs = map[string]string{
"ACL": "ACL",
"API": "API",
"ASCII": "ASCII",
"CIA": "CIA",
"CPU": "CPU",
"CSS": "CSS",
"DNS": "DNS",
"EOF": "EOF",
"ETA": "ETA",
"FBI": "FBI",
"GPU": "GPU",
"GRPC": "gRPC",
"GUID": "GUID",
"GRAPHQL": "GraphQL",
"HTML": "HTML",
"HTTP": "HTTP",
"HTTPS": "HTTPS",
"ID": "ID",
"IP": "IP",
"IRS": "IRS",
"JAVASCRIPT": "JavaScript",
"JSON": "JSON",
"LHS": "LHS",
"NSA": "NSA",
"OAUTH": "OAuth",
"OPENID": "OpenID",
"OS": "OS",
"QPS": "QPS",
"RAM": "RAM",
"RHS": "RHS",
"RPC": "RPC",
"SLA": "SLA",
"SMTP": "SMTP",
"SQL": "SQL",
"SSH": "SSH",
"TCP": "TCP",
"TLS": "TLS",
"TTL": "TTL",
"UDP": "UDP",
"UI": "UI",
"UID": "UID",
"URI": "URI",
"URL": "URL",
"UTF8": "UTF8",
"UTF16": "UTF16",
"UTF32": "UTF32",
"UUID": "UUID",
"VM": "VM",
"VR": "VR",
"XML": "XML",
"XMPP": "XMPP",
"XSRF": "XSRF",
"XSS": "XSS",
}
func CamelCase(s string) (r string) {
return DelimiterCase(s, "", "Force", true)
}
func CamelCoderCase(s string) (r string) {
return DelimiterCase(s, "", "Coder", true)
}
func capitalize(s string) (r string) {
for _, word := range strings.Fields(s) {
r = r + " " + capitalizeWord(word)
}
return r[1:]
}
func capitalizeWord(w string) string {
upper := strings.ToUpper(w)
if word, ok := commonInitialismsAndOddballs[upper]; ok == true {
return word
}
word := strings.ToLower(w) // NOTE: I shouldn't force lower after the first letter by default. v2.x should remove this.
word = strings.Title(word)
return word
}
func DelimiterCase(s, d, m string, f bool) (r string) {
r = PreprocessString(s)
switch strings.ToLower(m) {
case "coder":
r = capitalize(r)
case "force":
r = strings.ToLower(r)
r = strings.Title(r)
case "lower":
r = strings.ToLower(r)
case "smart":
r = titlecase.Convert(r)
case "title":
r = strings.Title(r)
case "upper":
r = strings.ToUpper(r)
default:
e := fmt.Errorf("Unknown mode: %q", m)
// fmt.Printf("ERROR: %s\n", e.Error())
panic(e.Error())
return r
}
if f {
r = fmt.Sprintf("%s%s", strings.ToLower(r[0:1]), r[1:])
}
r = strings.ReplaceAll(r, " ", d)
return r
}
func DotCoderCase(s string) (r string) {
return DelimiterCase(s, ".", "Coder", false)
}
func DotLowerCase(s string) (r string) {
return DelimiterCase(s, ".", "lower", false)
}
func DotForceCase(s string) (r string) {
return DelimiterCase(s, ".", "Force", false)
}
func DotTitleCase(s string) (r string) {
return DelimiterCase(s, ".", "Title", false)
}
func DotSmartCase(s string) (r string) {
return DelimiterCase(s, ".", "Smart", false)
}
func DotUpperCase(s string) (r string) {
return DelimiterCase(s, ".", "UPPER", false)
}
func KebabCoderCase(s string) (r string) {
return DelimiterCase(s, "-", "Coder", false)
}
func KebabCase(s string) (r string) {
return DelimiterCase(s, "-", "lower", false)
}
func KebabForceCase(s string) (r string) {
return DelimiterCase(s, "-", "Force", false)
}
func KebabTitleCase(s string) (r string) {
return DelimiterCase(s, "-", "Title", false)
}
func KebabSmartCase(s string) (r string) {
return DelimiterCase(s, "-", "Smart", false)
}
func KebabUpperCase(s string) (r string) {
return DelimiterCase(s, "-", "UPPER", false)
}
func NoDelimiterCamelCase(s string) (r string) {
return CamelCase(s)
}
func NoDelimiterCamelCoderCase(s string) (r string) {
return DelimiterCase(s, "", "Coder", true)
}
func NoDelimiterLowerCase(s string) (r string) {
return DelimiterCase(s, "", "lower", true)
}
func NoDelimiterPascalCase(s string) (r string) {
return PascalCase(s)
}
func NoDelimiterUpperCase(s string) (r string) {
return DelimiterCase(s, "", "UPPER", false)
}
func PascalCase(s string) (r string) {
return DelimiterCase(s, "", "Force", false)
}
func PascalCoderCase(s string) (r string) {
return DelimiterCase(s, "", "Coder", false)
}
func PreprocessString(s string) (r string) {
p := ' '
for _, c := range s {
r += Separatem(p, c)
p = c
}
r = strings.TrimSpace(r)
return spaceRE.ReplaceAllString(r, " ")
}
/*
MEETS CRITERIA to continue with Diagnostic Evaluation--Sufficient Emotional/Behavioral Needs have been Identified Above
*/
// Separatem takes p (previous), and c (current) and compares them to determine how to separate them, if necessary.
func Separatem(p, c rune) (r string) {
// If p and c are different cases return them concatenated with a space between
// If c is a dot, hyphen, underscore, slash, colon, or semicolon then convert it to a space
// NOTE: Consider scenarios with other delimiters and where to handle them. Probably not here.
switch c {
case '.', '-', '_', '/', ':', ';', '=', '+', '*':
// Replace with space
// fmt.Printf("%q is a delimiter\n", c)
r = " "
case 0x27, ',', '(', ')', '[', ']', '{', '}', '<', '>':
// Drop
r = ""
case '&':
// Symbol to word
r = "and"
default:
if UseCodeBreak && unicode.IsLetter(p) && unicode.IsLetter(c) {
// if unicode.IsLower(p) && unicode.IsUpper(c) || unicode.IsUpper(p) && unicode.IsLower(c) {
if unicode.IsLower(p) && unicode.IsUpper(c) {
// fmt.Printf("%q and %q is are opposite case\n", p, c)
r = fmt.Sprintf(" %s", string(c))
} else {
// fmt.Printf("%q is a letter\n", c)
r = string(c)
}
} else {
// fmt.Printf("%q or %q is not a letter\n", p, c)
r = string(c)
}
}
return r
}
func SnakeCase(s string) (r string) {
return DelimiterCase(s, "_", "lower", false)
}
func SnakeCoderCase(s string) (r string) {
return DelimiterCase(s, "_", "Coder", false)
}
func SnakeForceCase(s string) (r string) {
return DelimiterCase(s, "_", "Force", false)
}
func SnakeTitleCase(s string) (r string) {
return DelimiterCase(s, "_", "Title", false)
}
func SnakeSmartCase(s string) (r string) {
return DelimiterCase(s, "_", "Smart", false)
}
func SnakeUpperCase(s string) (r string) {
return DelimiterCase(s, "_", "UPPER", false)
}
func SpaceCoderCase(s string) (r string) {
return DelimiterCase(s, " ", "Coder", false)
}
func SpaceForceCase(s string) (r string) {
return DelimiterCase(s, " ", "Force", false)
}
func SpaceLowerCase(s string) (r string) {
return DelimiterCase(s, " ", "lower", false)
}
func SpaceTitleCase(s string) (r string) {
return DelimiterCase(s, " ", "Title", false)
}
func SpaceSmartCase(s string) (r string) {
return DelimiterCase(s, " ", "Smart", false)
}
func SpaceUpperCase(s string) (r string) {
r = PreprocessString(s)
return strings.ToUpper(r)
}