File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -7,18 +7,18 @@ defmodule CodeCorps.MapUtils do
77
88 def keys_to_string ( map ) , do: stringify_keys ( map )
99
10+ # Intercept incoming %DateTime arguments; otherwise they will match %{}
11+ defp stringify_keys ( % DateTime { } = val ) , do: val
1012 # Goes through a list and stringifies keys of any map member
11- def stringify_keys ( nil ) , do: nil
12- def stringify_keys ( map = % { } ) do
13+ defp stringify_keys ( map = % { } ) do
1314 map
1415 |> Enum . map ( fn { k , v } -> { stringify_key ( k ) , stringify_keys ( v ) } end )
1516 |> Enum . into ( % { } )
1617 end
17- def stringify_keys ( [ head | rest ] ) , do: [ stringify_keys ( head ) | stringify_keys ( rest ) ]
18+ defp stringify_keys ( [ head | rest ] ) , do: [ stringify_keys ( head ) | stringify_keys ( rest ) ]
1819 # Default
19- def stringify_keys ( not_a_map ) , do: not_a_map
20-
21- def stringify_key ( k ) when is_atom ( k ) , do: Atom . to_string ( k )
22- def stringify_key ( k ) , do: k
20+ defp stringify_keys ( val ) , do: val
2321
22+ defp stringify_key ( k ) when is_atom ( k ) , do: Atom . to_string ( k )
23+ defp stringify_key ( k ) , do: k
2424end
Original file line number Diff line number Diff line change 11defmodule CodeCorps.MapUtilsTest do
22 use ExUnit.Case , async: true
33
4- import CodeCorps.MapUtils , only: [ rename: 3 ]
4+ import CodeCorps.MapUtils , only: [ keys_to_string: 1 , rename: 3 ]
55
66 test "&rename/3 renames old key in map to new key" do
77 assert % { "foo" => 2 } |> rename ( "foo" , "bar" ) == % { "bar" => 2 }
88 end
9+
10+ test "&keys_to_string/1 stringifies any keys in map" do
11+ assert % { :a => "one" , :b => "two" } |> keys_to_string == % { "a" => "one" , "b" => "two" }
12+ assert % { } |> keys_to_string == % { }
13+ end
914end
You can’t perform that action at this time.
0 commit comments