EctoTurbo.Utils (EctoTurbo v1.0.0)

View Source

Utils functions.

Summary

Functions

Takes a map or list and removes keys or elements that have nil or empty values, or are empty maps.

At the map object or list object, delete the key with Value is_nil or == "", and recursion is also considered.

Converts all (atoms) map keys to string.

Converts all (string) map keys to atoms

Functions

compactify!(map)

Takes a map or list and removes keys or elements that have nil or empty values, or are empty maps.

Examples

iex> EctoTurbo.Utils.compactify!(%{nil_key: nil, not_nil: "nil"})
%{not_nil: "nil"}

iex> EctoTurbo.Utils.compactify!([1, nil, "string", %{key: :value}])
[1, "string", %{key: :value}]

iex> EctoTurbo.Utils.compactify!([a: nil, b: 2, c: "string"])
[b: 2, c: "string"]

iex> EctoTurbo.Utils.compactify!(%{empty: %{}, not: "not"})
%{not: "not"}

iex> EctoTurbo.Utils.compactify!({"not", "a map"})
** (ArgumentError) expecting a map or a list, got: {"not", "a map"}

compaction!(value)

@spec compaction!(map() | list()) :: map() | list()

At the map object or list object, delete the key with Value is_nil or == "", and recursion is also considered.

Examples

iex> EctoTurbo.Utils.compaction!(%{nil_nil: nil, not_nil: "a value", nested: %{nil_val: nil, other: "other"}})
%{not_nil: "a value", nested: %{other: "other"}}

iex> EctoTurbo.Utils.compaction!(%{nil_nil: nil, not_nil: "a value", nested: %{nil_val: nil, other: "other", nested_empty: %{}}})
%{not_nil: "a value", nested: %{other: "other"}}

iex> EctoTurbo.Utils.compaction!([nil, "string", %{nil_nil: nil, not_nil: "a value", nested: %{nil_val: nil, other: "other", nested_empty: %{}}}, ["nested", nil, 2]])
["string", %{not_nil: "a value", nested: %{other: "other"}}, ["nested", 2]]

stringify_keys(map)

@spec stringify_keys(any()) :: any()

Converts all (atoms) map keys to string.

Example

iex> map = %{a: 1, b: %{c: 3, d: 4}}
iex> EctoTurbo.Utils.stringify_keys(map)
%{"a" => 1, "b" => %{"c" => 3, "d" => 4}}

symbolize_keys(map)

@spec symbolize_keys(map()) :: map()

Converts all (string) map keys to atoms

Examples

iex> map = %{"a" => 1, "b" => %{"c" => 3, "d" => 4}}
iex> EctoTurbo.Utils.symbolize_keys(map)
%{a: 1, b: %{c: 3, d: 4}}