Fix acronyms for camel case

This commit is contained in:
Ian Coleman 2019-11-13 09:25:14 +11:00
parent 21f80d9b9a
commit 198ca931c7
3 changed files with 13 additions and 0 deletions

5
acronyms.go Normal file
View File

@ -0,0 +1,5 @@
package strcase
var uppercaseAcronym = map[string]bool{
"ID": true,
}

View File

@ -60,6 +60,9 @@ func toCamelInitCase(s string, initCase bool) string {
// ToCamel converts a string to CamelCase
func ToCamel(s string) string {
if uppercaseAcronym[s] {
s = strings.ToLower(s)
}
return toCamelInitCase(s, true)
}
@ -68,6 +71,9 @@ func ToLowerCamel(s string) string {
if s == "" {
return s
}
if uppercaseAcronym[s] {
s = strings.ToLower(s)
}
if r := rune(s[0]); r >= 'A' && r <= 'Z' {
s = strings.ToLower(string(r)) + s[1:]
}

View File

@ -40,6 +40,7 @@ func TestToCamel(t *testing.T) {
{"AnyKind of_string", "AnyKindOfString"},
{"odd-fix", "OddFix"},
{"numbers2And55with000", "Numbers2And55With000"},
{"ID", "Id"},
}
for _, i := range cases {
in := i[0]
@ -58,6 +59,7 @@ func TestToLowerCamel(t *testing.T) {
{"", ""},
{"AnyKind of_string", "anyKindOfString"},
{"AnyKind.of-string", "anyKindOfString"},
{"ID", "id"},
}
for _, i := range cases {
in := i[0]