-
Notifications
You must be signed in to change notification settings - Fork 4
/
transform.go
178 lines (157 loc) · 3.91 KB
/
transform.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
package utils
import (
"strings"
)
/*
Creation Time: 2020 - Apr - 09
Created by: (ehsan)
Maintainers:
1. Ehsan N. Moosa (E2)
Auditor: Ehsan N. Moosa (E2)
*/
var uppercaseAcronym = map[string]string{
"ID": "id",
"HTML": "html",
"RPC": "rpc",
}
// Converts a string to CamelCase
//nolint
func toCamelInitCase(s string, initCase bool) string {
s = strings.TrimSpace(s)
if s == "" {
return s
}
if a, ok := uppercaseAcronym[s]; ok {
s = a
}
n := strings.Builder{}
n.Grow(len(s))
capNext := initCase
for i, v := range []byte(s) {
vIsCap := v >= 'A' && v <= 'Z'
vIsLow := v >= 'a' && v <= 'z'
if capNext {
if vIsLow {
v += 'A'
v -= 'a'
}
} else if i == 0 {
if vIsCap {
v += 'a'
v -= 'A'
}
}
if vIsCap || vIsLow {
n.WriteByte(v)
capNext = false
} else if vIsNum := v >= '0' && v <= '9'; vIsNum {
n.WriteByte(v)
capNext = true
} else {
capNext = v == '_' || v == ' ' || v == '-' || v == '.'
}
}
return n.String()
}
// ToCamel converts a string to CamelCase
func ToCamel(s string) string {
return toCamelInitCase(s, true)
}
// ToLowerCamel converts a string to lowerCamelCase
func ToLowerCamel(s string) string {
return toCamelInitCase(s, false)
}
// ToSnake converts a string to snake_case
func ToSnake(s string) string {
return ToDelimited(s, '_')
}
func ToSnakeWithIgnore(s string, ignore uint8) string {
return ToScreamingDelimited(s, '_', ignore, false)
}
// ToScreamingSnake converts a string to SCREAMING_SNAKE_CASE
func ToScreamingSnake(s string) string {
return ToScreamingDelimited(s, '_', 0, true)
}
// ToKebab converts a string to kebab-case
func ToKebab(s string) string {
return ToDelimited(s, '-')
}
// ToScreamingKebab converts a string to SCREAMING-KEBAB-CASE
func ToScreamingKebab(s string) string {
return ToScreamingDelimited(s, '-', 0, true)
}
// ToDelimited converts a string to delimited.snake.case
// (in this case `delimiter = '.'`)
func ToDelimited(s string, delimiter uint8) string {
return ToScreamingDelimited(s, delimiter, 0, false)
}
// ToScreamingDelimited converts a string to SCREAMING.DELIMITED.SNAKE.CASE
// (in this case `delimiter = '.'; screaming = true`)
// or delimited.snake.case
// (in this case `delimiter = '.'; screaming = false`)
//nolint
func ToScreamingDelimited(s string, delimiter uint8, ignore uint8, screaming bool) string {
n := strings.Builder{}
n.Grow(len(s) + 2) // nominal 2 bytes of extra space for inserted delimiters
start := true
spaces := 0
for i, v := range []byte(s) {
if v == ' ' {
spaces++
continue
} else if start {
start = false
spaces = 0
} else {
for ; spaces > 0; spaces-- {
if ignore == ' ' {
n.WriteByte(' ')
} else {
n.WriteByte(delimiter)
}
}
}
vIsCap := v >= 'A' && v <= 'Z'
vIsLow := v >= 'a' && v <= 'z'
if vIsLow && screaming {
v += 'A'
v -= 'a'
} else if vIsCap && !screaming {
v += 'a'
v -= 'A'
}
// treat acronyms as words, eg for JSONData -> JSON is a whole word
//nolint:nestif
if i+1 < len(s) {
next := s[i+1]
vIsNum := v >= '0' && v <= '9'
nextIsCap := next >= 'A' && next <= 'Z'
nextIsLow := next >= 'a' && next <= 'z'
nextIsNum := next >= '0' && next <= '9'
// add underscore if next letter case type is changed
if (vIsCap && (nextIsLow || nextIsNum)) ||
(vIsLow && (nextIsCap || nextIsNum)) ||
(vIsNum && (nextIsCap || nextIsLow)) {
if prevIgnore := ignore > 0 && i > 0 && s[i-1] == ignore; !prevIgnore {
if vIsCap && nextIsLow {
if prevIsCap := i > 0 && s[i-1] >= 'A' && s[i-1] <= 'Z'; prevIsCap {
n.WriteByte(delimiter)
}
}
n.WriteByte(v)
if vIsLow || vIsNum || nextIsNum {
n.WriteByte(delimiter)
}
continue
}
}
}
if (v == ' ' || v == '_' || v == '-') && v != ignore {
// replace space/underscore/hyphen with delimiter
n.WriteByte(delimiter)
} else {
n.WriteByte(v)
}
}
return n.String()
}