Maps: Add filter_empty_values/1
This commit is contained in:
parent
626c22961f
commit
0de1a7629c
|
@ -1,5 +1,5 @@
|
||||||
# Pleroma: A lightweight social networking server
|
# Pleroma: A lightweight social networking server
|
||||||
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
# Copyright © 2017-2024 Pleroma Authors <https://pleroma.social/>
|
||||||
# SPDX-License-Identifier: AGPL-3.0-only
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
defmodule Pleroma.Maps do
|
defmodule Pleroma.Maps do
|
||||||
|
@ -18,4 +18,17 @@ def safe_put_in(data, keys, value) when is_map(data) and is_list(keys) do
|
||||||
rescue
|
rescue
|
||||||
_ -> data
|
_ -> data
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def filter_empty_values(data) do
|
||||||
|
# TODO: Change to Map.filter in Elixir 1.13+
|
||||||
|
data
|
||||||
|
|> Enum.filter(fn
|
||||||
|
{_k, nil} -> false
|
||||||
|
{_k, ""} -> false
|
||||||
|
{_k, []} -> false
|
||||||
|
{_k, %{} = v} -> Map.keys(v) != []
|
||||||
|
{_k, _v} -> true
|
||||||
|
end)
|
||||||
|
|> Map.new()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
# Pleroma: A lightweight social networking server
|
||||||
|
# Copyright © 2024 Pleroma Authors <https://pleroma.social/>
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
defmodule Pleroma.MapsTest do
|
||||||
|
use Pleroma.DataCase, async: true
|
||||||
|
|
||||||
|
alias Pleroma.Maps
|
||||||
|
|
||||||
|
describe "filter_empty_values/1" do
|
||||||
|
assert %{"bar" => "b", "ray" => ["foo"], "objs" => %{"a" => "b"}} ==
|
||||||
|
Maps.filter_empty_values(%{
|
||||||
|
"foo" => nil,
|
||||||
|
"fooz" => "",
|
||||||
|
"bar" => "b",
|
||||||
|
"rei" => [],
|
||||||
|
"ray" => ["foo"],
|
||||||
|
"obj" => %{},
|
||||||
|
"objs" => %{"a" => "b"}
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue