-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.go
65 lines (61 loc) · 1.29 KB
/
utils.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
package utils
import (
"bytes"
"regexp"
)
// CamelCase converts strings to CamelCase
func CamelCase(src string, capFirst bool) string {
camelingRegex := regexp.MustCompile("[0-9A-Za-z]+")
// commonInitialisms, taken from
// https://github.com/golang/lint/blob/32a87160691b3c96046c0c678fe57c5bef761456/lint.go#L702
commonInitialisms := map[string]bool{
"API": true,
"ASCII": true,
"CPU": true,
"CSS": true,
"DNS": true,
"EOF": true,
"GUID": true,
"HTML": true,
"HTTP": true,
"HTTPS": true,
"ID": true,
"IP": true,
"JSON": true,
"LHS": true,
"QPS": true,
"RAM": true,
"RHS": true,
"RPC": true,
"SLA": true,
"SMTP": true,
"SQL": true,
"SSH": true,
"TCP": true,
"TLS": true,
"TTL": true,
"UDP": true,
"UI": true,
"UID": true,
"UUID": true,
"URI": true,
"URL": true,
"UTF8": true,
"VM": true,
"XML": true,
"XSRF": true,
"XSS": true,
}
byteSrc := []byte(src)
chunks := camelingRegex.FindAll(byteSrc, -1)
for idx, val := range chunks {
if idx == 0 && !capFirst {
chunks[idx] = val
} else if commonInitialisms[string(bytes.ToUpper(val)[:])] {
chunks[idx] = bytes.ToUpper(val)
} else {
chunks[idx] = bytes.Title(val)
}
}
return string(bytes.Join(chunks, nil))
}