ExEssentials.Core.Map (ExEssentials v0.7.0)
View SourceA collection of utility functions for working with maps.
This module provides tools to:
- Rename or extract specific keys from a map.
- Remove entries with `nil` or empty values (`""`, `[]`, `%{}`).
Common use cases include transforming map structures and cleaning up payloads before encoding or external communication.
Summary
Functions
Removes all key-value pairs where the value is either nil
, ""
, []
, or %{}
.
Removes all key-value pairs where the value is considered "blank".
A blank value is one of: ""
, []
, %{}
.
Removes all key-value pairs where the value is nil
.
Renames and/or filters keys from a map, preserving only the specified keys.
Keys can be passed as atoms (no renaming) or as {from, to}
tuples for renaming.
Like renake/2
, but applies a transformation function to each value before inserting it into the resulting map.
Functions
Removes all key-value pairs where the value is either nil
, ""
, []
, or %{}
.
## Examples
iex> map = %{a: nil, b: "", c: [], d: %{}, e: "keep"}
iex> ExEssentials.Core.Map.compact(map)
%{e: "keep"}
Removes all key-value pairs where the value is considered "blank".
A blank value is one of: ""
, []
, %{}
.
## Examples
iex> map = %{a: "", b: [], c: %{}, d: 42}
iex> ExEssentials.Core.Map.compact_blank(map)
%{d: 42}
Removes all key-value pairs where the value is nil
.
## Examples
iex> map = %{a: 1, b: nil, c: "text"}
iex> ExEssentials.Core.Map.compact_nil(map)
%{a: 1, c: "text"}
Renames and/or filters keys from a map, preserving only the specified keys.
Keys can be passed as atoms (no renaming) or as {from, to}
tuples for renaming.
## Examples
iex> map = %{name: "Alice", age: 30, email: "alice@example.com"}
iex> ExEssentials.Core.Map.renake(map, [:name, age: :years])
%{name: "Alice", years: 30}
Like renake/2
, but applies a transformation function to each value before inserting it into the resulting map.
## Examples
iex> map = %{price: 100, discount: 10}
iex> ExEssentials.Core.Map.renake(map, [:price, discount: :off], fn {_field, value} -> value * 2 end)
%{price: 200, off: 20}