-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathcase.go
144 lines (132 loc) · 2.79 KB
/
case.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
package format
import (
"fmt"
"strings"
"unicode"
)
type Case int
const (
//CaseUpper represents case format
CaseUpper = Case(iota)
CaseLower
CaseUpperCamel
CaseLowerCamel
CaseUpperUnderscore
CaseLowerUnderscore
)
// NewCase create a new case for supplied name or error, supported in case insensitive form "upperCase", "upper", "u"
func NewCase(name string) (Case, error) {
switch strings.ToLower(name) {
case "upper", "u":
return CaseUpper, nil
case "lower", "l":
return CaseLower, nil
case "lowercamel", "lc":
return CaseLowerCamel, nil
case "uppercamel", "uc":
return CaseUpperCamel, nil
case "lowerunderscore", "lu":
return CaseLowerUnderscore, nil
case "upperunderscore", "uu":
return CaseUpperUnderscore, nil
}
return -1, fmt.Errorf("unsupported case format: %s", name)
}
// String return case format name
func (from Case) String() string {
switch from {
case CaseUpper:
return "Upper"
case CaseLower:
return "Lower"
case CaseUpperCamel:
return "UpperCamel"
case CaseLowerCamel:
return "LowerCamel"
case CaseUpperUnderscore:
return "UpperUnderscore"
case CaseLowerUnderscore:
return "LowerUnderscore"
}
return "UnsupportedCase"
}
// Format converts supplied text from Case to
func (from Case) Format(text string, to Case) string {
toUpper := false
toLower := false
toCamel := false
toUnserscore := false
fromCamel := false
fromUnserscore := false
switch to {
case CaseUpper, CaseUpperUnderscore:
toUpper = true
case CaseLower, CaseLowerUnderscore:
toLower = true
case CaseUpperCamel, CaseLowerCamel:
toCamel = true
}
switch to {
case CaseUpperUnderscore, CaseLowerUnderscore:
toUnserscore = true
}
switch from {
case CaseUpperCamel, CaseLowerCamel:
fromCamel = true
case CaseUpperUnderscore, CaseLowerUnderscore:
fromUnserscore = true
}
underscore := rune('_')
var result = make([]rune, 0)
makeLower := false
makeUpper := false
hasUnderscore := false
for i, r := range text {
first := i == 0
if toUpper {
makeUpper = true
} else if toLower {
makeLower = true
}
if first {
if to == CaseLowerCamel {
r = unicode.ToLower(r)
} else if to == CaseUpperCamel {
r = unicode.ToUpper(r)
}
} else {
if fromUnserscore {
if toCamel {
if r == underscore {
hasUnderscore = true
continue
}
if hasUnderscore {
makeUpper = true
hasUnderscore = false
} else {
makeLower = true
}
}
}
if unicode.IsUpper(r) {
if fromCamel {
if toUnserscore {
if !(i > 1 && result[len(result)-2] == '_') {
result = append(result, underscore)
}
}
}
}
}
if makeLower {
r = unicode.ToLower(r)
} else if makeUpper {
r = unicode.ToUpper(r)
}
result = append(result, r)
makeUpper = false
makeLower = false
}
return string(result)
}