diff options
author | Haelwenn (lanodan) Monnier <contact@hacktivis.me> | 2024-01-25 00:26:55 +0100 |
---|---|---|
committer | Haelwenn (lanodan) Monnier <contact@hacktivis.me> | 2024-01-26 16:18:29 +0100 |
commit | 0de1a7629c5dbf3f6fba69a41bbeff5947558899 (patch) | |
tree | 83d52739e7238f8693fb4a054c4c2885917b972c | |
parent | 626c22961f7eb0bfc8735a11ef005e5b991f75ce (diff) | |
download | pleroma-0de1a7629c5dbf3f6fba69a41bbeff5947558899.tar.gz pleroma-0de1a7629c5dbf3f6fba69a41bbeff5947558899.zip |
Maps: Add filter_empty_values/1
-rw-r--r-- | lib/pleroma/maps.ex | 15 | ||||
-rw-r--r-- | test/pleroma/maps_test.exs | 22 |
2 files changed, 36 insertions, 1 deletions
diff --git a/lib/pleroma/maps.ex b/lib/pleroma/maps.ex index 6d586e53e..5020a8ff8 100644 --- a/lib/pleroma/maps.ex +++ b/lib/pleroma/maps.ex @@ -1,5 +1,5 @@ # 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 defmodule Pleroma.Maps do @@ -18,4 +18,17 @@ defmodule Pleroma.Maps do rescue _ -> data 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 diff --git a/test/pleroma/maps_test.exs b/test/pleroma/maps_test.exs new file mode 100644 index 000000000..05f1b18b2 --- /dev/null +++ b/test/pleroma/maps_test.exs @@ -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 |