swiss v3.2.0 Swiss.Map View Source

A few extra functions to deal with Maps.

Link to this section Summary

Functions

Deep merges two maps. Only maps are merged, all other types are overriden.

Applies defaults to a map.

Wrapper around Map.from_struct/1 that tolerates nil.

Fetches a value from a map with indifferent access, i.e. given an atom, returns the value that is keyed by that atom, or by its string equivalent.

Runs Map.put/3 only if pred returns truthy when called on the value.

Runs Map.put/3 only if cond is truthy. Unlike Swiss.Map.put_if/4, takes a function that is called when the condition passes, that should return the value to insert in the map.

Converts an atom-keyed map into a string-keyed map.

Link to this section Functions

Link to this function

deep_merge(map_dest, map_src, max_depth \\ :infinity)

View Source
deep_merge(map(), map(), non_neg_integer() | :infinity) :: map()

Deep merges two maps. Only maps are merged, all other types are overriden.

iex> Swiss.Map.deep_merge(%{user: %{id: 42}}, %{user: %{name: "João"}})
%{user: %{id: 42, name: "João"}}

iex> Swiss.Map.deep_merge(
...> %{user: %{id: 42, message: %{id: 22}}},
...> %{user: %{message: %{text: "hi"}}},
...> 1
...> )
%{user: %{id: 42, message: %{text: "hi"}}}

iex> Swiss.Map.deep_merge(
...> %{user: %{id: 42}, messages: [%{id: 1}]},
...> %{user: %{id: 30, age: 40}, messages: [%{id: 2}]}
...> )
%{user: %{id: 30, age: 40}, messages: [%{id: 2}]}
Link to this function

defaults(map, defaults)

View Source
defaults(Map.t(), Map.t() | keyword()) :: Map.t()

Applies defaults to a map.

Examples

iex> Swiss.Map.defaults(%{a: 42}, %{b: 12})
%{a: 42, b: 12}

iex> Swiss.Map.defaults(%{a: 42}, %{a: 44, b: 12})
%{a: 42, b: 12}

iex> Swiss.Map.defaults(%{a: 42, c: nil}, [a: nil, b: 12, c: 13])
%{a: 42, b: 12, c: nil}
Link to this function

from_struct(struct)

View Source
from_struct(struct() | nil) :: Map.t() | nil

Wrapper around Map.from_struct/1 that tolerates nil.

Examples

iex> Swiss.Map.from_struct(nil)
nil

iex> Swiss.Map.from_struct(%{__struct__: SomeStruct, life: 42})
%{life: 42}
Link to this function

indif_fetch!(map, key)

View Source
indif_fetch!(Map.t(), atom()) :: any()

Fetches a value from a map with indifferent access, i.e. given an atom, returns the value that is keyed by that atom, or by its string equivalent.

If both atom and String keys exist in the map, the atom's value is returned.

Examples

iex> Swiss.Map.indif_fetch!(%{life: 42}, :life)
42

iex> Swiss.Map.indif_fetch!(%{"life" => 42}, :life)
42

iex> Swiss.Map.indif_fetch!(%{:life => 42, "life" => 64}, :life)
42

iex> Swiss.Map.indif_fetch!(%{}, :life)
** (KeyError) key :life not found in: %{}
Link to this function

put_if(map, key, value, pred \\ fn v -> !is_nil(v) end)

View Source
put_if(map(), any(), any(), (any() -> boolean())) :: map()

Runs Map.put/3 only if pred returns truthy when called on the value.

The default behavior is to put unless the value is nil.

Examples

iex> Swiss.Map.put_if(%{life: 42}, :life, 22)
%{life: 22}

iex> Swiss.Map.put_if(%{life: 42}, :life, nil)
%{life: 42}

iex> Swiss.Map.put_if(%{life: 42}, :life, nil, &is_nil/1)
%{life: nil}

iex> Swiss.Map.put_if(%{life: 42}, :life, 22, &(&1 < 55))
%{life: 22}
Link to this function

put_if_lazy(map, key, value_fn, condition)

View Source
put_if_lazy(map(), any(), (() -> any()), any()) :: map()

Runs Map.put/3 only if cond is truthy. Unlike Swiss.Map.put_if/4, takes a function that is called when the condition passes, that should return the value to insert in the map.

Examples

iex> Swiss.Map.put_if_lazy(%{life: 42}, :life, fn -> 12 end, true)
%{life: 12}

iex> Swiss.Map.put_if_lazy(%{life: 42}, :life, fn -> 12 end, false)
%{life: 42}
Link to this function

to_string_keys(map)

View Source
to_string_keys(Map.t()) :: Map.t()

Converts an atom-keyed map into a string-keyed map.

Examples

iex> Swiss.Map.to_string_keys(%{life: 42})
%{"life" => 42}

iex> Swiss.Map.to_string_keys(%{"life" => 42, death: 27})
%{"life" => 42, "death" => 27}