Skip to content

Commit

Permalink
Improve uppercase string conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Atakan Ozceviz committed Jul 18, 2022
1 parent a61ebb8 commit d7993fd
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 d7993fd

Please sign in to comment.