-
Notifications
You must be signed in to change notification settings - Fork 0
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
Expand fields to simplify string functions #1
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 |
---|---|---|
|
@@ -17,56 +17,26 @@ defmodule JSONAPI.Utils.String do | |
iex> underscore("top-posts") | ||
"top_posts" | ||
|
||
iex> underscore(:top_posts) | ||
"top_posts" | ||
|
||
iex> underscore("-top-posts") | ||
"-top_posts" | ||
|
||
iex> underscore("-top--posts-") | ||
"-top--posts-" | ||
|
||
iex> underscore(%{"foo-bar" => "baz"}) | ||
%{"foo_bar" => "baz"} | ||
|
||
iex> underscore({"foo-bar", "dollar-sol"}) | ||
{"foo_bar", "dollar-sol"} | ||
|
||
iex> underscore({"foo-bar", %{"a-d" => "z-8"}}) | ||
{"foo_bar", %{"a_d" => "z-8"}} | ||
|
||
iex> underscore(%{"f-b" => %{"a-d" => "z"}, "c-d" => "e"}) | ||
%{"f_b" => %{"a_d" => "z"}, "c_d" => "e"} | ||
|
||
iex> underscore(:"foo-bar") | ||
:foo_bar | ||
|
||
iex> underscore(%{"f-b" => "a-d"}) | ||
%{"f_b" => "a-d"} | ||
|
||
""" | ||
@spec underscore(String.t()) :: String.t() | ||
def underscore(value) when is_binary(value) do | ||
String.replace(value, ~r/([a-zA-Z0-9])-([a-zA-Z0-9])/, "\\1_\\2") | ||
end | ||
|
||
def underscore(map) when is_map(map) do | ||
Enum.into(map, %{}, &underscore/1) | ||
end | ||
|
||
def underscore({key, value}) when is_map(value) do | ||
{underscore(key), underscore(value)} | ||
end | ||
|
||
def underscore({key, value}) do | ||
{underscore(key), value} | ||
end | ||
|
||
@spec underscore(atom) :: atom | ||
def underscore(value) when is_atom(value) do | ||
value | ||
|> to_string() | ||
|> underscore() | ||
|> String.to_atom() | ||
end | ||
|
||
def underscore(value) do | ||
value | ||
end | ||
|
||
@doc """ | ||
|
@@ -86,32 +56,18 @@ defmodule JSONAPI.Utils.String do | |
"_top__posts_" | ||
|
||
""" | ||
@spec dasherize(atom) :: atom | ||
def dasherize(value) when is_atom(value) do | ||
value | ||
|> to_string() | ||
|> dasherize() | ||
end | ||
|
||
@spec dasherize(String.t()) :: String.t() | ||
def dasherize(value) when is_binary(value) do | ||
String.replace(value, ~r/([a-zA-Z0-9])_([a-zA-Z0-9])/, "\\1-\\2") | ||
end | ||
|
||
def dasherize(%{__struct__: _} = value) when is_map(value) do | ||
value | ||
end | ||
|
||
def dasherize(value) when is_map(value) do | ||
Enum.into(value, %{}, &dasherize/1) | ||
end | ||
|
||
def dasherize({key, value}) do | ||
if is_map(value) do | ||
{dasherize(key), dasherize(value)} | ||
else | ||
{dasherize(key), value} | ||
end | ||
end | ||
|
||
@doc """ | ||
Replace underscores or dashes between words in `value` with camelCasing | ||
|
||
|
@@ -122,19 +78,24 @@ defmodule JSONAPI.Utils.String do | |
iex> camelize("top_posts") | ||
"topPosts" | ||
|
||
iex> camelize(:top_posts) | ||
"topPosts" | ||
|
||
iex> camelize("_top_posts") | ||
"_topPosts" | ||
|
||
iex> camelize("_top__posts_") | ||
"_top__posts_" | ||
|
||
""" | ||
@spec camelize(atom) :: atom | ||
def camelize(value) when is_atom(value) do | ||
value | ||
|> to_string() | ||
|> camelize() | ||
end | ||
|
||
@spec camelize(String.t()) :: String.t() | ||
def camelize(value) when is_binary(value) do | ||
with words <- | ||
Regex.split( | ||
|
@@ -154,20 +115,68 @@ defmodule JSONAPI.Utils.String do | |
[String.capitalize(h)] ++ camelize_list(t) | ||
end | ||
|
||
def camelize(%{__struct__: _} = value) when is_map(value) do | ||
value | ||
@doc """ | ||
|
||
## Examples | ||
|
||
iex> expand_fields(%{"foo-bar" => "baz"}, &underscore/1) | ||
%{"foo_bar" => "baz"} | ||
|
||
iex> expand_fields(%{"foo_bar" => "baz"}, &dasherize/1) | ||
%{"foo-bar" => "baz"} | ||
|
||
iex> expand_fields(%{"foo-bar" => "baz"}, &camelize/1) | ||
%{"fooBar" => "baz"} | ||
|
||
iex> expand_fields({"foo-bar", "dollar-sol"}, &underscore/1) | ||
{"foo_bar", "dollar-sol"} | ||
|
||
iex> expand_fields({"foo-bar", %{"a-d" => "z-8"}}, &underscore/1) | ||
{"foo_bar", %{"a_d" => "z-8"}} | ||
|
||
iex> expand_fields(%{"f-b" => %{"a-d" => "z"}, "c-d" => "e"}, &underscore/1) | ||
%{"f_b" => %{"a_d" => "z"}, "c_d" => "e"} | ||
|
||
iex> expand_fields(%{"f-b" => %{"a-d" => %{"z-w" => "z"}}, "c-d" => "e"}, &underscore/1) | ||
%{"f_b" => %{"a_d" => %{"z_w" => "z"}}, "c_d" => "e"} | ||
|
||
iex> expand_fields(:"foo-bar", &underscore/1) | ||
"foo_bar" | ||
|
||
iex> expand_fields(:foo_bar, &dasherize/1) | ||
"foo-bar" | ||
|
||
iex> expand_fields(:"foo-bar", &camelize/1) | ||
"fooBar" | ||
|
||
iex> expand_fields(%{"f-b" => "a-d"}, &underscore/1) | ||
%{"f_b" => "a-d"} | ||
|
||
iex> expand_fields(%{"inserted-at" => ~N[2019-01-17 03:27:24.776957]}, &underscore/1) | ||
%{"inserted_at" => ~N[2019-01-17 03:27:24.776957]} | ||
|
||
""" | ||
@spec expand_fields(map, function) :: map | ||
def expand_fields(%{__struct__: _} = value, _fun), do: value | ||
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. I confess that I'm not really sure what's happening with this 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. Pattern matching a naive datetime and other data types - |
||
|
||
@spec expand_fields(map, function) :: map | ||
def expand_fields(map, fun) when is_map(map) do | ||
Enum.into(map, %{}, &expand_fields(&1, fun)) | ||
end | ||
|
||
def camelize(value) when is_map(value) do | ||
Enum.into(value, %{}, &camelize/1) | ||
@spec expand_fields(tuple, function) :: tuple | ||
def expand_fields({key, value}, fun) when is_map(value) do | ||
{fun.(key), expand_fields(value, fun)} | ||
end | ||
|
||
def camelize({key, value}) do | ||
if is_map(value) do | ||
{camelize(key), camelize(value)} | ||
else | ||
{camelize(key), value} | ||
end | ||
@spec expand_fields(tuple, function) :: tuple | ||
def expand_fields({key, value}, fun) do | ||
{fun.(key), value} | ||
end | ||
|
||
@spec expand_fields(any, function) :: any | ||
def expand_fields(value, fun) do | ||
fun.(value) | ||
end | ||
|
||
defp normalized_underscore_to_dash_config(value) when is_boolean(value) do | ||
|
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.
❤️