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 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}
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
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}}
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
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}
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: %{}