strcase/camel.go

39 lines
636 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 {
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)
}
if v >= 'a' && v <= 'z' {
if capNext {
n += strings.ToUpper(string(v))
} else {
n += string(v)
}
}
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-06-15 10:36:32 +08:00
return toCamelInitCase(s, true);
}
func ToLowerCamel(s string) string {
2017-06-15 10:36:32 +08:00
return toCamelInitCase(s, false);
}