View Source Ext.LenientMap (ext v1.3.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.

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