Elixir Utility methods for the Map module
Summary
Functions
Atomize the given keys of a map.
Removes all keys with blank values.
Flattens nested maps and lists into a map keyed by their access paths.
Converts a struct/map to plain map.
Puts a key-value pair into a map when the value is not blank.
Converts the given map keys to strings.
Functions
Atomize the given keys of a map.
Passed keys must be a list of atoms or :all. Using a list of atoms is
the recommended way. If you use :all, you must be careful to avoid
calling it on external or unknown data, which can grow the Erlang Atom table,
and may even cause it to overload.
Options
:deep- Perform conversion for keys in nested maps. Defaults tofalse.:drop- Drop unconverted keys. Defaults tofalse.
Original, converted string keys are always dropped.
Examples
iex> map = %{"a" => 1, "b" => %{"c" => 3, "d" => 4}}
iex> ExUtils.Maps.atomize_keys(map, [:a, :b])
%{a: 1, b: %{"c" => 3, "d" => 4}}
iex> ExUtils.Maps.atomize_keys(map, [:a, :b], deep: true)
%{a: 1, b: %{"c" => 3, "d" => 4}}
iex> ExUtils.Maps.atomize_keys(map, [:a, :b, :d], deep: true)
%{a: 1, b: %{"c" => 3, d: 4}}
iex> ExUtils.Maps.atomize_keys(map, [:a], drop: true)
%{a: 1}
iex> ExUtils.Maps.atomize_keys(map, :all, deep: true)
%{a: 1, b: %{c: 3, d: 4}}
Removes all keys with blank values.
Options
:blanks- Values considered blank. Defaults to[nil, "", []].
Examples
iex> ExUtils.Maps.drop_blank_values(%{a: 123, b: nil, c: "", d: [], e: false})
%{a: 123, e: false}
iex> ExUtils.Maps.drop_blank_values(%{count: 0, active: false}, blanks: [nil, 0])
%{active: false}
@spec flatten(map(), Keyword.t()) :: %{required(String.t()) => any()} | %{required([any()]) => any()}
Flattens nested maps and lists into a map keyed by their access paths.
Structs and empty maps or lists are preserved as values.
Options
:key_format- Format used for flattened keys. Use:stringfor dot-separated string or:pathfor lists containing the original path segments. Defaults to:string.
Examples
iex> ExUtils.Maps.flatten(%{user: %{name: "Ada", settings: %{theme: "dark"}}})
%{"user.name" => "Ada", "user.settings.theme" => "dark"}
iex> ExUtils.Maps.flatten(%{profiles: [%{platform: "github", username: "ada"}]})
%{"profiles.0.platform" => "github", "profiles.0.username" => "ada"}
iex> ExUtils.Maps.flatten(%{user: %{name: "Ada"}}, key_format: :path)
%{[:user, :name] => "Ada"}
Converts a struct/map to plain map.
Options
:deep- recursively converts nested structs whentrue. Defaults totrue.
Date, time, and date-time structs are preserved.
Examples
iex> ExUtils.Maps.from_struct(1..3)
%{first: 1, last: 3, step: 1}
iex> date = ~D[2026-10-04]
iex> ExUtils.Maps.from_struct(date)
~D[2026-10-04]
Puts a key-value pair into a map when the value is not blank.
Options
:blanks- Values considered blank. Defaults to[nil, "", []].
Examples
iex> ExUtils.Maps.maybe_put(%{}, :name, "John")
%{name: "John"}
iex> ExUtils.Maps.maybe_put(%{name: "Mary"}, :name, "John")
%{name: "John"}
iex> ExUtils.Maps.maybe_put(%{name: "John"}, :name, nil)
%{name: "John"}
iex> ExUtils.Maps.maybe_put(%{}, :count, 0, blanks: [nil, 0])
%{}
Converts the given map keys to strings.
Passed keys must be a list of atoms or :all.
Options
:deep- Perform conversion for keys in nested maps. Defaults tofalse.:drop- Drop unconverted keys. Defaults tofalse.
Original, converted atom keys are always dropped.
Examples
iex> map = %{a: 1, b: %{c: 3, d: 4}}
iex> ExUtils.Maps.stringify_keys(map, [:a, :b])
%{"a" => 1, "b" => %{c: 3, d: 4}}
iex> ExUtils.Maps.stringify_keys(map, [:a, :b], deep: true)
%{"a" => 1, "b" => %{c: 3, d: 4}}
iex> ExUtils.Maps.stringify_keys(map, [:a, :b, :d], deep: true)
%{"a" => 1, "b" => %{:c => 3, "d" => 4}}
iex> ExUtils.Maps.stringify_keys(map, [:a], drop: true)
%{"a" => 1}
iex> ExUtils.Maps.stringify_keys(map, :all, deep: true)
%{"a" => 1, "b" => %{"c" => 3, "d" => 4}}