Skip to content

Commit ae78a7d

Browse files
rodriguesmattpolzin
authored andcommitted
fix: camelize when prefix is camelized already
1 parent b071131 commit ae78a7d

1 file changed

Lines changed: 19 additions & 16 deletions

File tree

lib/jsonapi/utils/string.ex

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ defmodule JSONAPI.Utils.String do
109109
iex> camelize("alreadyCamelized")
110110
"alreadyCamelized"
111111
112+
iex> camelize("alreadyCamelized-id")
113+
"alreadyCamelizedId"
114+
115+
iex> camelize("alreadyCamelized_id")
116+
"alreadyCamelizedId"
117+
112118
"""
113119
@spec camelize(atom) :: String.t()
114120
def camelize(value) when is_atom(value) do
@@ -121,28 +127,25 @@ defmodule JSONAPI.Utils.String do
121127
def camelize(value) when value == "", do: value
122128

123129
def camelize(value) when is_binary(value) do
124-
with words <-
125-
Regex.split(
126-
~r{(?<=[a-zA-Z0-9])[-_](?=[a-zA-Z0-9])},
127-
to_string(value),
128-
trim: true
129-
) do
130-
case words do
131-
# If there is only one word, leave it as-is
132-
[word] ->
133-
word
134-
135-
# If there are multiple words, perform the camelizing
136-
[h | t] ->
137-
Enum.join([String.downcase(h) | camelize_list(t)])
138-
end
130+
case Regex.split(
131+
~r{(?<=[a-zA-Z0-9])[-_](?=[a-zA-Z0-9])},
132+
to_string(value),
133+
trim: true
134+
) do
135+
# If there is only one word, leave it as-is
136+
[word] ->
137+
word
138+
139+
# If there are multiple words, perform the camelizing
140+
[h | t] ->
141+
Enum.join([h | camelize_list(t)])
139142
end
140143
end
141144

142145
defp camelize_list([]), do: []
143146

144147
defp camelize_list([h | t]) do
145-
[String.capitalize(h)] ++ camelize_list(t)
148+
[String.capitalize(h) | camelize_list(t)]
146149
end
147150

148151
@doc """

0 commit comments

Comments
 (0)