Turbo.Ecto.Utils (turbo_ecto v1.0.2)

Utils func

Link to this section 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

Link to this section Functions

Link to this function

compactify!(map)

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

Examples

iex> Turbo.Ecto.Utils.compactify!(%{nil_key: nil, not_nil: "nil"})
%{not_nil: "nil"}
iex> Turbo.Ecto.Utils.compactify!([1, nil, "string", %{key: :value}])
[1, "string", %{key: :value}]
iex> Turbo.Ecto.Utils.compactify!([a: nil, b: 2, c: "string"])
[b: 2, c: "string"]
iex> Turbo.Ecto.Utils.compactify!(%{empty: %{}, not: "not"})
%{not: "not"}
iex> Turbo.Ecto.Utils.compactify!({"not", "a map"})
** (ArgumentError) expecting a map or a list, got: {"not", "a map"}
Link to this function

compactify!(bool, list)

Link to this function

compaction!(value)

Specs

compaction!(Map.t() | List.t()) :: Map.t() | List.t()

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

Examples

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

iex> Turbo.Ecto.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> Turbo.Ecto.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]]
Link to this function

stringify_keys(map)

Specs

stringify_keys(map()) :: map()

Converts all (atoms) map keys to string.

Example

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

Link to this function

symbolize_keys(map)

Specs

symbolize_keys(map()) :: map()

Converts all (string) map keys to atoms

Examples

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