strcase/camel.go
2017-11-29 12:02:53 +11:00

43 lines
712 B
Go

package strcase
import (
"strings"
)
// Converts a string to CamelCase
func toCamelInitCase(s string, initCase bool) string {
s = addWordBoundariesToNumbers(s)
s = strings.Trim(s, " ")
n := ""
capNext := initCase
for _, v := range s {
if v >= 'A' && v <= 'Z' {
n += string(v)
}
if v >= '0' && v <= '9' {
n += string(v)
}
if v >= 'a' && v <= 'z' {
if capNext {
n += strings.ToUpper(string(v))
} else {
n += string(v)
}
}
if v == '_' || v == ' ' || v == '-' {
capNext = true
} else {
capNext = false
}
}
return n
}
func ToCamel(s string) string {
return toCamelInitCase(s, true)
}
func ToLowerCamel(s string) string {
return toCamelInitCase(s, false)
}