View Source Moar.Map (Moar v1.6.0)

Map-related functions.

Link to this section Summary

Functions

Converts keys in map to atoms.

Converts keys to atoms, traversing through descendant lists and maps.

Deeply merges two enumerables into a single map.

Merges two enumerables into a single map.

Returns a copy of map with old_key_name changed to new_key_name.

Returns a copy of map with old_key_name changed to new_key_name.

Returns a copy of map after changing key names supplied by keys_map.

Converts keys in map to strings.

Transforms values of map using transformer function.

Link to this section Functions

@spec atomize_keys(map()) :: map()

Converts keys in map to atoms.

iex> Moar.Map.atomize_keys(%{"a" => 1, "b" => 2})
%{a: 1, b: 2}
@spec deep_atomize_keys(list() | map()) :: map()

Converts keys to atoms, traversing through descendant lists and maps.

iex> Moar.Map.deep_atomize_keys(%{"a" => %{"aa" => 1}, "b" => [%{"bb" => 2}, %{"bbb" => 3}]})
%{a: %{aa: 1}, b: [%{bb: 2}, %{bbb: 3}]}
@spec deep_merge(Enum.t(), Enum.t()) :: map()

Deeply merges two enumerables into a single map.

iex> Moar.Map.deep_merge(%{fruit: %{apples: 3, bananas: 5}, veggies: %{carrots: 10}}, [fruit: [cherries: 20]])
%{fruit: %{apples: 3, bananas: 5, cherries: 20}, veggies: %{carrots: 10}}
@spec merge(Enum.t(), Enum.t()) :: map()

Merges two enumerables into a single map.

iex> Moar.Map.merge(%{a: 1}, [b: 2])
%{a: 1, b: 2}
Link to this function

rename_key(map, old_and_new_key)

View Source
@spec rename_key(
  map(),
  {binary(), binary()}
) :: map()

Returns a copy of map with old_key_name changed to new_key_name.

old_key_name and new_key_name are passed in as a {old_key_name, new_key_name} tuple.

iex> %{"color" => "red", "size" => "medium"} |> Moar.Map.rename_key({"color", "colour"})
%{"colour" => "red", "size" => "medium"}
Link to this function

rename_key(map, old_key_name, new_key_name)

View Source
@spec rename_key(map(), binary() | atom(), binary() | atom()) :: map()

Returns a copy of map with old_key_name changed to new_key_name.

iex> %{"color" => "red", "size" => "medium"} |> Moar.Map.rename_key("color", "colour")
%{"colour" => "red", "size" => "medium"}
Link to this function

rename_keys(map, keys_map)

View Source
@spec rename_keys(map(), map()) :: map()

Returns a copy of map after changing key names supplied by keys_map.

%{"behavior" => "chill", "color" => "red"} |> Moar.Map.rename_keys(%{"behavior" => "behaviour", "color" => "colour"})
%{"behaviour" => "chill", "colour" => "red"}
@spec stringify_keys(map()) :: map()

Converts keys in map to strings.

iex> Moar.Map.stringify_keys(%{a: 1, b: 2} )
%{"a" => 1, "b" => 2}
Link to this function

transform(map, key, transformer)

View Source
@spec transform(map(), atom() | binary() | list(), (any() -> any())) :: map()

Transforms values of map using transformer function.

iex> %{"foo" => "chicken", "bar" => "cow", "baz" => "pig"} |> Moar.Map.transform("foo", &String.upcase/1)
%{"foo" => "CHICKEN", "bar" => "cow", "baz" => "pig"}

iex> %{"foo" => "chicken", "bar" => "cow", "baz" => "pig"} |> Moar.Map.transform(["foo", "bar"], &String.upcase/1)
%{"foo" => "CHICKEN", "bar" => "COW", "baz" => "pig"}