-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
66 lines (51 loc) · 1.1 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
66
package utils
import (
"strings"
"unicode"
"github.com/gedex/inflector"
)
func toSnakeCase(s string) string {
var result []rune
for _, acronym := range acronyms {
s = strings.ReplaceAll(s, strings.ToUpper(acronym), acronym)
}
for i, r := range s {
if i > 0 && unicode.IsUpper(r) {
result = append(result, '_')
}
result = append(result, unicode.ToLower(r))
}
return string(result)
}
func snakeCaseToCamelCase(s string) string {
var result []rune
words := strings.Split(s, "_")
for i, word := range words {
if i == 0 {
result = append(result, []rune(word)...)
} else {
result = append(result, []rune(titleize(word))...)
}
}
return string(result)
}
func toCamelCase(s string) string {
var result []rune
for i, r := range s {
if i == 0 {
result = append(result, unicode.ToLower(r))
} else {
result = append(result, r)
}
}
return string(result)
}
func pluralize(s string) string {
return inflector.Pluralize(s)
}
func singularize(s string) string {
return inflector.Singularize(s)
}
func titleize(s string) string {
return strings.ToUpper(s[:1]) + s[1:]
}