View Source Ext.LenientMap (ext v1.2.0)

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

Summary

Functions

Returns the value for a specific key from the map.

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

Functions

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