go-tsviewer/stringer/stringer.go

50 lines
760 B
Go
Raw Permalink Normal View History

2019-01-14 20:07:11 +00:00
package stringer
import (
"fmt"
"strings"
"unicode"
)
func Build(ss ...string) (string, error) {
var builder strings.Builder
for _, s := range ss {
if _, err := builder.WriteString(s); err != nil {
return "", err
}
}
return builder.String(), nil
}
func ToCamel(str string) string {
if len(str) < 1 {
return str
}
for i, c := range str[1:] {
if c >= 'A' && c <= 'Z' {
str = fmt.Sprintf("%s_%c%s", str[:i+1], unicode.ToLower(c), str[i+2:])
return ToCamel(str)
}
}
return str
}
func ToSnake(str string) string {
if len(str) < 1 {
return str
}
for i, c := range str[1:] {
if c == '_' {
str = fmt.Sprintf("%s%c%s", str[:i+1], unicode.ToUpper(rune(str[i+2])), str[i+3:])
return ToSnake(str)
}
}
return str
}