-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change camelize behavior #293
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,6 +94,9 @@ defmodule JSONAPI.Utils.String do | |
iex> camelize("") | ||
"" | ||
|
||
iex> camelize("alreadyCamelized") | ||
"alreadyCamelized" | ||
|
||
""" | ||
@spec camelize(atom) :: String.t() | ||
def camelize(value) when is_atom(value) do | ||
|
@@ -109,12 +112,19 @@ defmodule JSONAPI.Utils.String do | |
with words <- | ||
Regex.split( | ||
~r{(?<=[a-zA-Z0-9])[-_](?=[a-zA-Z0-9])}, | ||
to_string(value) | ||
to_string(value), | ||
trim: true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Annotation: Using |
||
) do | ||
[h | t] = words |> Enum.filter(&(&1 != "")) | ||
|
||
[String.downcase(h) | camelize_list(t)] | ||
|> Enum.join() | ||
case words do | ||
# If there is only one word, leave it as-is | ||
[word] -> | ||
word | ||
Comment on lines
+119
to
+121
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Annotation: This is the main change. Like the comment says, if we could not split the value into multiple words (i.e. there were no |
||
|
||
# If there are multiple words, perform the camelizing | ||
[h | t] -> | ||
[String.downcase(h) | camelize_list(t)] | ||
|> Enum.join() | ||
end | ||
end | ||
end | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Annotation: New test to prove an already-camelized value will remain camelized.