Skip to content

Commit

Permalink
Fix acronyms for camel case
Browse files Browse the repository at this point in the history
  • Loading branch information
iancoleman committed Nov 12, 2019
1 parent 21f80d9 commit 198ca93
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions acronyms.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package strcase

var uppercaseAcronym = map[string]bool{
"ID": true,
}
6 changes: 6 additions & 0 deletions camel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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:]
}
Expand Down
2 changes: 2 additions & 0 deletions camel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand Down

0 comments on commit 198ca93

Please sign in to comment.