-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add enumerable util * fix type spec of the do_split function. * fix code format * improve docs and tests * improve docs * fix README
- Loading branch information
1 parent
c076d75
commit e161a1a
Showing
4 changed files
with
79 additions
and
1 deletion.
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
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,27 @@ | ||
defmodule Recase.Enumerable do | ||
@moduledoc """ | ||
Helper module to convert enumerable keys recursively. | ||
""" | ||
|
||
@doc """ | ||
Invoke fun for each keys of the enumerable. | ||
""" | ||
@spec convert_keys(Enumerable.t(), fun) :: Enumerable.t() | ||
def convert_keys(enumerable, fun) when is_map(enumerable) do | ||
enumerable | ||
|> Enum.into(%{}, fn {key, value} -> | ||
{fun.(key), handle_value(value, fun)} | ||
end) | ||
end | ||
|
||
def convert_keys(enumerable, fun) when is_list(enumerable) do | ||
enumerable | ||
|> Enum.map(&handle_value(&1, fun)) | ||
end | ||
|
||
defp handle_value(value, fun) when is_map(value) or is_list(value) do | ||
convert_keys(value, fun) | ||
end | ||
|
||
defp handle_value(value, _), do: value | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
defmodule RecaseEnumerableTest do | ||
use ExUnit.Case | ||
|
||
test "should convert keys of a map" do | ||
assert Recase.Enumerable.convert_keys( | ||
%{"upper case" => "value", "upper-case2" => "value"}, | ||
&Recase.to_pascal/1 | ||
) == %{"UpperCase" => "value", "UpperCase2" => "value"} | ||
end | ||
|
||
test "should convert keys of a nested map" do | ||
assert Recase.Enumerable.convert_keys( | ||
%{"upper case" => %{"upper-case2" => "value"}}, | ||
&Recase.to_path(&1, "\\") | ||
) == %{"upper\\case" => %{"upper\\case2" => "value"}} | ||
end | ||
|
||
test "should convert keys of a map in list" do | ||
assert Recase.Enumerable.convert_keys( | ||
[%{"upper case" => "value", "upper-case2" => "value"}], | ||
&Recase.to_pascal/1 | ||
) == [%{"UpperCase" => "value", "UpperCase2" => "value"}] | ||
end | ||
|
||
test "should convert keys of a nested map in list" do | ||
assert Recase.Enumerable.convert_keys( | ||
[%{"upper case" => %{"upper-case2" => "value"}}], | ||
&Recase.to_pascal/1 | ||
) == [%{"UpperCase" => %{"UpperCase2" => "value"}}] | ||
end | ||
|
||
test "should return the same list when list items are usual text" do | ||
assert Recase.Enumerable.convert_keys( | ||
["upper case", "upper-case2"], | ||
&Recase.to_pascal/1 | ||
) == ["upper case", "upper-case2"] | ||
end | ||
end |