View Source Ext.LenientMap (ext v1.4.0)

Ext.LenientMap provides helper functions related to Map by treating string and atom keys as equivalent.

Summary

Functions

Delete a specific key from the map.

Returns the value for a specific key from the map.

Returns the value for a specific key and delete it from the map.

Returns true if the map as the specified key as an atom or a string.

Puts the given value in map. An atom key is used if map has primarily atom keys. Otherwise, a string key is used.

Updates the value at the specified key with the given function.

Functions

delete(map, atom_key)

@spec delete(map(), atom()) :: map()

Delete a specific key from the map.

If the map has both the atom and string keys, both of them will be deleted.

Examples

iex> Ext.LenientMap.delete(%{a: 1, b: 2}, :a)
%{b: 2}

iex> Ext.LenientMap.delete(%{"a" => 1, "b" => 2}, :a)
%{"b" => 2}

iex> Ext.LenientMap.delete(%{"a" => 1, :a => 2, "b" => 3}, :a)
%{"b" => 3}

iex> Ext.LenientMap.delete(%{a: 1, b: 2}, :c)
%{a: 1, b: 2}

get(map, atom_key, default \\ nil)

@spec get(map(), atom(), Map.value()) :: Map.value()

Returns the value for a specific key from the map.

If a value can be found using both the atom and string keys, the value given by the atom key is returned.

Examples

iex> Ext.LenientMap.get(%{a: 1, b: 2}, :a)
1

iex> Ext.LenientMap.get(%{"a" => 1, "b" => 2}, :a)
1

iex> Ext.LenientMap.get(%{"a" => 1, :a => 2, "b" => 3}, :a)
2

iex> Ext.LenientMap.get(%{a: 1, b: 2}, :c)
nil

iex> Ext.LenientMap.get(%{a: 1, b: 2}, :c, :not_found)
:not_found

get_and_delete(map, atom_key, default \\ nil)

@spec get_and_delete(map(), atom(), Map.value()) :: {Map.value(), map()}

Returns the value for a specific key and delete it from the map.

If a value can be found using both the atom and string keys, the value given by the atom key is returned, but both entries are dropped from the map.

Examples

iex> Ext.LenientMap.get_and_delete(%{a: 1, b: 2}, :a)
{1, %{b: 2}}

iex> Ext.LenientMap.get_and_delete(%{"a" => 1, "b" => 2}, :a)
{1, %{"b" => 2}}

iex> Ext.LenientMap.get_and_delete(%{"a" => 1, :a => 2, "b" => 3}, :a)
{2, %{"b" => 3}}

iex> Ext.LenientMap.get_and_delete(%{a: 1, b: 2}, :c)
{nil, %{a: 1, b: 2}}

iex> Ext.LenientMap.get_and_delete(%{a: 1, b: 2}, :c, :not_found)
{:not_found, %{a: 1, b: 2}}

has_key?(map, atom_key)

@spec has_key?(map(), atom()) :: boolean()

Returns true if the map as the specified key as an atom or a string.

Examples

iex> Ext.LenientMap.has_key?(%{a: 1, b: 2}, :a)
true

iex> Ext.LenientMap.has_key?(%{"a" => 1, "b" => 2}, :a)
true

iex> Ext.LenientMap.has_key?(%{"a" => 1, :a => 2, "b" => 3}, :a)
true

iex> Ext.LenientMap.has_key?(%{a: 1, b: 2}, :c)
false

put(map, atom_key, value)

@spec put(map(), atom(), Map.value()) :: map()

Puts the given value in map. An atom key is used if map has primarily atom keys. Otherwise, a string key is used.

Examples

iex> Ext.LenientMap.put(%{a: 1, b: 2}, :c, 3)
%{a: 1, b: 2, c: 3}

iex> Ext.LenientMap.put(%{"a" => 1, "b" => 2}, :c, 3)
%{"a" => 1, "b" => 2, "c" => 3}

update!(map, atom_key, fun)

@spec update!(map(), atom(), (Map.value() -> Map.value())) :: map()

Updates the value at the specified key with the given function.

If a value can be found using both the atom and string keys, the value given by the atom key is passed to the updater while the string key is deleted from the map.

If the key is not present in map, a KeyError exception is raised.

Examples

iex> Ext.LenientMap.update!(%{a: 1, b: 2}, :a, &(&1 * 10))
%{a: 10, b: 2}

iex> Ext.LenientMap.update!(%{"a" => 1, "b" => 2}, :a, &(&1 * 10))
%{"a" => 10, "b" => 2}

iex> Ext.LenientMap.update!(%{"a" => 1, :a => 2, "b" => 3}, :a, &(&1 * 10))
%{:a => 20, "b" => 3}

iex> Ext.LenientMap.update!(%{}, :c, &(&1 * 10))
** (KeyError) key :c not found in: %{}