mirror of
https://git.pleroma.social/pleroma/pleroma.git
synced 2025-01-03 13:58:41 +00:00
Maps: Add filter_empty_values/1
This commit is contained in:
parent
626c22961f
commit
0de1a7629c
2 changed files with 36 additions and 1 deletions
|
@ -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 @@ defmodule Pleroma.Maps 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
|
||||||
|
|
22
test/pleroma/maps_test.exs
Normal file
22
test/pleroma/maps_test.exs
Normal file
|
@ -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 a new issue