ExEssentials.Core.Map (ExEssentials v0.7.0)

View Source

A 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

compact(map)

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

compact_blank(map)

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}

compact_nil(map)

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

renake(map, keys)

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}

renake(map, keys, transform_fn)

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}