forked from naoina/migu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
57 lines (53 loc) · 1.22 KB
/
util.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
package migu
import (
"bytes"
"unicode"
)
// toCamelCase returns a copy of the string s with all Unicode letters mapped to their camel case.
// It will convert to upper case previous letter of '_' and first letter, and remove letter of '_'.
func toCamelCase(s string) string {
if s == "" {
return ""
}
result := make([]rune, 0, len(s))
upper := false
for _, r := range s {
if r == '_' {
upper = true
continue
}
if upper {
result = append(result, unicode.ToUpper(r))
upper = false
continue
}
result = append(result, r)
}
result[0] = unicode.ToUpper(result[0])
return string(result)
}
// toSnakeCase returns a copy of the string s with all Unicode letters mapped to their snake case.
// It will insert letter of '_' at position of previous letter of uppercase and all
// letters convert to lower case.
func toSnakeCase(s string) string {
if s == "" {
return ""
}
var result bytes.Buffer
result.WriteRune(unicode.ToLower(rune(s[0])))
for _, c := range s[1:] {
if unicode.IsUpper(c) {
result.WriteRune('_')
}
result.WriteRune(unicode.ToLower(c))
}
return result.String()
}
func inStrings(a []string, s string) bool {
for _, v := range a {
if v == s {
return true
}
}
return false
}