Skip to content
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

Merged
merged 3 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions lib/jsonapi/utils/string.ex
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ defmodule JSONAPI.Utils.String do
iex> camelize("")
""

iex> camelize("alreadyCamelized")
"alreadyCamelized"
Comment on lines +97 to +98
Copy link
Contributor Author

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.


"""
@spec camelize(atom) :: String.t()
def camelize(value) when is_atom(value) do
Expand All @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Annotation: Using trim: true means we can remove the need to manually filter out the empty strings.

) 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
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 - or _ delimiters) then return the single word without downcasing it.


# If there are multiple words, perform the camelizing
[h | t] ->
[String.downcase(h) | camelize_list(t)]
|> Enum.join()
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule JSONAPI.Mixfile do
def project do
[
app: :jsonapi,
version: "1.5.0",
version: "1.5.1",
package: package(),
compilers: compilers(Mix.env()),
description: description(),
Expand Down