2015-10-09 17:13:05 +08:00
|
|
|
package strcase
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Converts a string to CamelCase
|
2017-06-15 10:36:32 +08:00
|
|
|
func toCamelInitCase(s string, initCase bool) string {
|
2015-10-09 17:13:05 +08:00
|
|
|
s = strings.Trim(s, " ")
|
|
|
|
n := ""
|
2017-06-15 02:26:32 +08:00
|
|
|
capNext := initCase
|
2015-10-09 17:13:05 +08:00
|
|
|
for _, v := range s {
|
|
|
|
if v >= 'A' && v <= 'Z' {
|
|
|
|
n += string(v)
|
|
|
|
}
|
|
|
|
if v >= 'a' && v <= 'z' {
|
|
|
|
if capNext {
|
|
|
|
n += strings.ToUpper(string(v))
|
|
|
|
} else {
|
|
|
|
n += string(v)
|
|
|
|
}
|
|
|
|
}
|
2017-06-15 02:26:32 +08:00
|
|
|
if v == '_' || v == ' ' || v == '-' {
|
2015-10-09 17:13:05 +08:00
|
|
|
capNext = true
|
|
|
|
} else {
|
|
|
|
capNext = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
2017-06-15 02:26:32 +08:00
|
|
|
|
|
|
|
func ToCamel(s string) string {
|
2017-06-15 10:36:32 +08:00
|
|
|
return toCamelInitCase(s, true);
|
2017-06-15 02:26:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func ToLowerCamel(s string) string {
|
2017-06-15 10:36:32 +08:00
|
|
|
return toCamelInitCase(s, false);
|
2017-06-15 02:26:32 +08:00
|
|
|
}
|