elvish/pkg/strutil/camel_to_dashed.go
Qi Xiao e134ef51e3 Handle all-cap abbreviations in strutil.CamelToDashed.
Also remove support for the "name" field tag from eval.scanOptions - it's not
used anywhere and the use case it was intended for is handled by CamelToDashed.
2021-12-31 16:54:32 +00:00

25 lines
628 B
Go

package strutil
import (
"strings"
"unicode"
)
// CamelToDashed converts a CamelCaseIdentifier to a dash-separated-identifier,
// or a camelCaseIdentifier to a -dash-separated-identifier. All-cap words
// are converted to lower case; HTTP becomes http and HTTPRequest becomes
// http-request.
func CamelToDashed(camel string) string {
var sb strings.Builder
runes := []rune(camel)
for i, r := range runes {
if (i == 0 && unicode.IsLower(r)) ||
(0 < i && i < len(runes)-1 &&
unicode.IsUpper(r) && unicode.IsLower(runes[i+1])) {
sb.WriteRune('-')
}
sb.WriteRune(unicode.ToLower(r))
}
return sb.String()
}