-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcase.go
More file actions
173 lines (156 loc) · 4.6 KB
/
case.go
File metadata and controls
173 lines (156 loc) · 4.6 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
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
package strings
import (
"strings"
"unicode"
)
// KebabToCamel convert string s from kebab-case to CamelCase. The initial case of s does not affect the output.
func KebabToCamel(s string) string {
// This depends on Map walking the string from start to end, which
// really needs to happen anyway in unicode strings.
capitalize := true
return strings.Map(func(r rune) rune {
if r == '-' {
capitalize = true
return -1
} else if capitalize {
capitalize = false
return unicode.ToTitle(r)
} else {
return unicode.ToLower(r)
}
}, s)
}
// SnakeToKebab converts s from snake_case to kebab-case. Only the underscores are impacted, the case of the rest of s
// is passed through unchanged.
func SnakeToKebab(s string) string {
return strings.Replace(s, "_", "-", -1)
}
// SnakeToCamel converts s from snake_case to CamelCase.
func SnakeToCamel(s string) string {
k := SnakeToKebab(s)
return KebabToCamel(k)
}
// CamelToKebab converts capitalize from CamelCase to kebab-case.
// If it encounters a character that is not legitimate camel case,
// it ignores it (like numbers, spaces, etc.).
// Runs of upper case letters are treated as one word.
func CamelToKebab(camelCase string) string {
return camelToKebabOrSnake(camelCase, '-')
}
// CamelToSnake converts camelCase from CamelCase to snake_case.
// If it encounters a character that is not legitimate camel case,
// it ignores it (like numbers, spaces, etc.).
// Runs of upper case letters are treated as one word.
// A run of upper case, followed by lower case letters will be treated
// as if the final character in the upper case run belongs with the lower case
// letters.
func CamelToSnake(camelCase string) string {
return camelToKebabOrSnake(camelCase, '_')
}
func camelToKebabOrSnake(camelCase string, replacement rune) string {
var kebabCase []rune
var inUpper bool
for i, r := range camelCase {
if unicode.IsLetter(r) {
if unicode.IsUpper(r) {
if i > 0 && !inUpper {
kebabCase = append(kebabCase, replacement)
}
kebabCase = append(kebabCase, unicode.ToLower(r))
inUpper = true
} else {
if inUpper {
// switching from upper to lower, if we were in an upper run
// we need to add a hyphen in front of the last rune
if len(kebabCase) > 1 && kebabCase[len(kebabCase)-2] != replacement {
r2 := kebabCase[len(kebabCase)-1]
kebabCase[len(kebabCase)-1] = replacement
kebabCase = append(kebabCase, r2)
}
}
kebabCase = append(kebabCase, r)
inUpper = false
}
}
}
return string(kebabCase)
}
// Decap returns a new string with the first character in the string set to its lower case equivalent,
// and subsequent characters that are capitalized also set to lower case until it encounters a lower case letter.
func Decap(s string) string {
if s == "" {
return ""
}
var b strings.Builder
var foundLower bool
for i, r := range s {
l := unicode.ToLower(r)
if i == 0 {
b.WriteRune(l)
} else if l != r && !foundLower {
b.WriteRune(l)
} else {
foundLower = true
b.WriteRune(r)
}
}
return b.String()
}
// Title is a more advanced titling operation.
// It will convert underscores to spaces, and add spaces to CamelCase words.
func Title(s string) string {
s = strings.TrimSpace(strings.Title(strings.Replace(s, "_", " ", -1)))
if len(s) <= 1 {
return s
}
newString := s[0:1]
l := strings.ToLower(s)
i := 1
for i < len(s) {
if l[i] != s[i] && s[i-1:i] != " " {
// is a capital.
newString += " "
// Group capitals until we get a lower case
for i < len(s)-1 && l[i] != s[i] && l[i+1] != s[i+1] {
newString += s[i : i+1]
i++
}
}
newString += s[i : i+1]
i++
}
return newString
}
// Camel converts a string separated by spaces, hyphens and other breaks to CamelCase,
// also known as PascalCase. None alpha-numerics are removed.
func Camel(s string) string {
var b strings.Builder
wordBoundary := true
for _, r := range s {
if !(unicode.IsLetter(r) || unicode.IsNumber(r)) {
wordBoundary = true
continue
}
if wordBoundary {
b.WriteRune(unicode.ToTitle(r))
wordBoundary = false
} else {
b.WriteRune(r)
}
}
return b.String()
}
// EqualCaseInsensitive is a synonym for the strings package EqualFold which provides unicode compliant case-insensitive
// comparison.
func EqualCaseInsensitive(s1, s2 string) bool {
return strings.EqualFold(s1, s2)
}
// IsSnake returns true if the string only contains lower case letters, numbers and underscores with no spaces.
func IsSnake(s string) bool {
for _, r := range s {
if !((unicode.IsLetter(r) && unicode.IsLower(r)) || r == '_' || unicode.IsNumber(r)) {
return false
}
}
return true
}