mirror of
https://github.com/edgurgel/httparrot
synced 2025-04-06 00:32:34 -04:00
fix group_by_keys (#20)
When there are more than 2 values that have the same key, group_by_keys misbehave and produce something like `%{list: [["a", "b"], "c"]}`
This commit is contained in:
parent
e53c1a217e
commit
3b1da703f0
1 changed files with 3 additions and 3 deletions
|
@ -14,8 +14,8 @@ defmodule HTTParrot.GeneralRequestInfo do
|
||||||
@doc """
|
@doc """
|
||||||
Group by keys and if duplicated keys, aggregate them as a list
|
Group by keys and if duplicated keys, aggregate them as a list
|
||||||
|
|
||||||
iex> group_by_keys([a: "v1", a: "v2", b: "v3"])
|
iex> group_by_keys([a: "v1", a: "v2", b: "v3", a: "v4"])
|
||||||
%{a: ["v1", "v2"], b: "v3"}
|
%{a: ["v1", "v2", "v4"], b: "v3"}
|
||||||
"""
|
"""
|
||||||
@spec group_by_keys(list) :: map
|
@spec group_by_keys(list) :: map
|
||||||
def group_by_keys([]), do: %{}
|
def group_by_keys([]), do: %{}
|
||||||
|
@ -24,7 +24,7 @@ defmodule HTTParrot.GeneralRequestInfo do
|
||||||
|> Enum.map(fn {k, v} -> %{k => v} end)
|
|> Enum.map(fn {k, v} -> %{k => v} end)
|
||||||
|> Enum.reduce(fn m, acc ->
|
|> Enum.reduce(fn m, acc ->
|
||||||
Map.merge(m, acc, fn _k, v1, v2 ->
|
Map.merge(m, acc, fn _k, v1, v2 ->
|
||||||
[v2, v1]
|
List.wrap(v2) ++ List.wrap(v1)
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue