ExUtils.Maps (ExUtils v1.0.1)

Copy Markdown View Source

Elixir Utility methods for the Map module

Summary

Functions

atomize_keys(map) deprecated

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_keys(map)

This function is deprecated. Use ExUtils.Maps.atomize_keys/3 instead.

atomize_keys(map, keys, opts \\ [])

@spec atomize_keys(map(), :all | [atom()], Keyword.t()) :: map()

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 to false.
  • :drop - Drop unconverted keys. Defaults to false.

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}}

drop_blank_values(map, opts \\ [])

@spec drop_blank_values(map(), Keyword.t()) :: map()

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}

flatten(map, opts \\ [])

@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 :string for dot-separated string or :path for 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"}

from_struct(map, opts \\ [])

@spec from_struct(map(), Keyword.t()) :: map()

Converts a struct/map to plain map.

Options

  • :deep - recursively converts nested structs when true. Defaults to true.

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]

maybe_put(map, key, value, opts \\ [])

@spec maybe_put(map(), any(), any(), Keyword.t()) :: map()

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])
%{}

stringify_keys(map, keys, opts \\ [])

@spec stringify_keys(map(), :all | [atom()], Keyword.t()) :: map()

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 to false.
  • :drop - Drop unconverted keys. Defaults to false.

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}}

symbolize_keys(map, opts \\ [])

This function is deprecated. Use ExUtils.Maps.atomize_keys/3 instead.