added inflector.Camelize helper

This commit is contained in:
Gani Georgiev
2025-01-24 14:49:28 +02:00
parent 65440314ce
commit c101798516
4 changed files with 60 additions and 1 deletions

View File

@@ -83,3 +83,30 @@ func Snakecase(str string) string {
return strings.ToLower(result.String())
}
// Camelize convers a string to its "CamelCased" version (non alphanumeric characters are removed).
//
// For example:
//
// inflector.Camelize("send_email") // "SendEmail"
func Camelize(str string) string {
var result strings.Builder
var isPrevSpecial bool
for _, c := range str {
if !unicode.IsLetter(c) && !unicode.IsNumber(c) {
isPrevSpecial = true
continue
}
if isPrevSpecial || result.Len() == 0 {
isPrevSpecial = false
result.WriteRune(unicode.ToUpper(c))
} else {
result.WriteRune(c)
}
}
return result.String()
}