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 {
|
2017-11-29 08:52:26 +08:00
|
|
|
s = addWordBoundariesToNumbers(s)
|
2015-10-09 17:13:05 +08:00
|
|
|
s = strings.Trim(s, " ")
|
|
|
|
n := ""
|
2017-06-15 02:26:32 +08:00
|
|
|
capNext := initCase
|
2018-06-01 16:35:02 +08:00
|
|
|
for i, v := range s {
|
2015-10-09 17:13:05 +08:00
|
|
|
if v >= 'A' && v <= 'Z' {
|
2018-06-01 16:35:02 +08:00
|
|
|
if !initCase && i == 0 {
|
|
|
|
n += strings.ToLower(string(v))
|
|
|
|
} else {
|
|
|
|
n += string(v)
|
|
|
|
}
|
2015-10-09 17:13:05 +08:00
|
|
|
}
|
2017-11-29 08:52:26 +08:00
|
|
|
if v >= '0' && v <= '9' {
|
|
|
|
n += string(v)
|
|
|
|
}
|
2015-10-09 17:13:05 +08:00
|
|
|
if v >= 'a' && v <= 'z' {
|
|
|
|
if capNext {
|
|
|
|
n += strings.ToUpper(string(v))
|
|
|
|
} else {
|
|
|
|
n += string(v)
|
|
|
|
}
|
|
|
|
}
|
2017-11-29 09:02:53 +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-11-29 09:02:53 +08:00
|
|
|
return toCamelInitCase(s, true)
|
2017-06-15 02:26:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func ToLowerCamel(s string) string {
|
2017-11-29 09:02:53 +08:00
|
|
|
return toCamelInitCase(s, false)
|
2017-06-15 02:26:32 +08:00
|
|
|
}
|