Skip to content

Commit

Permalink
Merge pull request #41 from atakanozceviz/master
Browse files Browse the repository at this point in the history
Improve uppercase string to camel case conversion
  • Loading branch information
iancoleman committed Jul 13, 2023
2 parents a61ebb8 + d7993fd commit 6ce6fd7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 8 additions & 1 deletion camel.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ func toCamelInitCase(s string, initCase bool) string {
if s == "" {
return s
}
if a, ok := uppercaseAcronym[s]; ok {
a, hasAcronym := uppercaseAcronym[s]
if hasAcronym {
s = a
}

n := strings.Builder{}
n.Grow(len(s))
capNext := initCase
prevIsCap := false
for i, v := range []byte(s) {
vIsCap := v >= 'A' && v <= 'Z'
vIsLow := v >= 'a' && v <= 'z'
Expand All @@ -55,7 +57,12 @@ func toCamelInitCase(s string, initCase bool) string {
v += 'a'
v -= 'A'
}
} else if prevIsCap && vIsCap && !hasAcronym {
v += 'a'
v -= 'A'
}
prevIsCap = vIsCap

if vIsCap || vIsLow {
n.WriteByte(v)
capNext = false
Expand Down
2 changes: 2 additions & 0 deletions camel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func toCamel(tb testing.TB) {
{"odd-fix", "OddFix"},
{"numbers2And55with000", "Numbers2And55With000"},
{"ID", "Id"},
{"CONSTANT_CASE", "ConstantCase"},
}
for _, i := range cases {
in := i[0]
Expand Down Expand Up @@ -70,6 +71,7 @@ func toLowerCamel(tb testing.TB) {
{"ID", "id"},
{"some string", "someString"},
{" some string", "someString"},
{"CONSTANT_CASE", "constantCase"},
}
for _, i := range cases {
in := i[0]
Expand Down

0 comments on commit 6ce6fd7

Please sign in to comment.