-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace underscores with dashes in relationships
- Loading branch information
1 parent
31c4ae6
commit 0c50bc6
Showing
5 changed files
with
83 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
defmodule JSONAPI.Utils.Underscore do | ||
@moduledoc """ | ||
Helpers for replacing underscores with dashes. | ||
""" | ||
|
||
def underscore?, do: Application.get_env(:jsonapi, :underscore_to_dash, false) | ||
|
||
def underscore(value) when is_atom(value) do | ||
value | ||
|> to_string | ||
|> underscore | ||
end | ||
|
||
def underscore(value) when is_binary(value) do | ||
String.replace(value, "_", "-") | ||
end | ||
|
||
def underscore(value) when is_map(value) do | ||
value | ||
|> Enum.map(&underscore/1) | ||
|> Enum.into(%{}) | ||
end | ||
|
||
def underscore({key, value}) do | ||
if is_map(value) do | ||
{underscore(key), underscore(value)} | ||
else | ||
{underscore(key), value} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters