strcase/camel.go

43 lines
712 B
Go
Raw Normal View History

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 := ""
capNext := initCase
2015-10-09 17:13:05 +08:00
for _, v := range s {
if v >= 'A' && v <= 'Z' {
n += string(v)
}
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
}
func ToCamel(s string) string {
2017-11-29 09:02:53 +08:00
return toCamelInitCase(s, true)
}
func ToLowerCamel(s string) string {
2017-11-29 09:02:53 +08:00
return toCamelInitCase(s, false)
}