Skip to content

Commit fea7d7b

Browse files
authored
Merge pull request #605 from arubis/600-make-MapUtils__stringify_keys-private
Make &stringify_keys/1 private.
2 parents 06ffc7d + b350ac8 commit fea7d7b

2 files changed

Lines changed: 13 additions & 8 deletions

File tree

lib/code_corps/map_utils.ex

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff 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
2424
end
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
defmodule 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
914
end

0 commit comments

Comments
 (0)