View Source JetExt.Map (jet_ext v0.2.3)
This module introduces some useful functions to Map
.
Summary
Functions
Atomize keys of map.
Fetches the value in form of key tuple for a specific key in the given map.
Filter items of the map.
Indifferent access, or return :error
.
Indifferent access, or raise error.
Indifferent access, or return default value.
Indifferent updates the key in map with the given function.
Indifferent take.
Indifferent updates the key in map with the given function.
Put a value to map base on key type of the map.
Atomize keys of any map nested in the term recursively.
Stringify keys of map.
Functions
Atomize keys of map.
Example
iex> JetExt.Map.atomize_keys(%{"a" => 1, "b" => 2})
%{a: 1, b: 2}
iex> JetExt.Map.atomize_keys(%{"a" => 1, "b" => %{"c" => 2}})
%{a: 1, b: %{"c" => 2}}
iex> JetExt.Map.atomize_keys(%{"a" => 1, "b" => [%{"c" => 2}, %{"d" => 3}]})
%{a: 1, b: [%{"c" => 2}, %{"d" => 3}]}
Fetches the value in form of key tuple for a specific key in the given map.
Example
iex> JetExt.Map.fetch(%{a: 1, b: nil}, :a)
{:a, {:ok, 1}}
iex> JetExt.Map.fetch(%{a: 1, b: nil}, :b)
{:b, {:ok, nil}}
iex> JetExt.Map.fetch(%{a: 1, b: nil}, :c)
{:c, :error}
Filter items of the map.
Example
iex> JetExt.Map.filter(%{a: 1, b: 2, c: 3, d: 4}, fn {key, value} -> :a === key or 2 === value end)
%{a: 1, b: 2}
Indifferent access, or return :error
.
Example
iex> JetExt.Map.indifferent_fetch(%{a: 1}, :a)
{:ok, 1}
iex> JetExt.Map.indifferent_fetch(%{"a" => 1}, :a)
{:ok, 1}
iex> JetExt.Map.indifferent_fetch(%{"a" => 1}, :b)
:error
Indifferent access, or raise error.
Example
iex> JetExt.Map.indifferent_fetch!(%{a: 1}, :a)
1
iex> JetExt.Map.indifferent_fetch!(%{"a" => 1}, :a)
1
iex> JetExt.Map.indifferent_fetch!(%{"a" => 1}, :b)
** (KeyError) key :b not found in: %{"a" => 1}
Indifferent access, or return default value.
Example
iex> JetExt.Map.indifferent_get(%{a: 1}, :a)
1
iex> JetExt.Map.indifferent_get(%{"a" => 1}, :a)
1
iex> JetExt.Map.indifferent_get(%{"a" => 1}, :b)
nil
iex> JetExt.Map.indifferent_get(%{"a" => 1}, :b, 2)
2
iex> JetExt.Map.indifferent_get(%{"a" => 1}, "a")
1
Indifferent updates the key in map with the given function.
Example
iex> JetExt.Map.indifferent_has_key(%{a: 1}, :a)
{:ok, :a}
iex> JetExt.Map.indifferent_has_key(%{"a" => 1}, :a)
{:ok, "a"}
iex> JetExt.Map.indifferent_has_key(%{"a" => 1}, :b)
:error
Indifferent take.
Returns a new map with all the key-value pairs in map where the key, or String.to_atom(key) is in keys.
Example
iex> JetExt.Map.indifferent_take(%{"a" => 1, "b" => 2, c: 3, d: 4}, [:a, :b, :c])
%{a: 1, b: 2, c: 3}
iex> JetExt.Map.indifferent_take(%{"a" => 1}, [:a])
%{a: 1}
iex> JetExt.Map.indifferent_take(%{"a" => 1}, [:b])
%{}
@spec indifferent_update!( map(), key :: atom(), (existing_value :: value -> new_value :: value) ) :: map() when value: term()
Indifferent updates the key in map with the given function.
If key
is not present in map
, a KeyError
exception is raised.
Example
iex> JetExt.Map.indifferent_update!(%{a: 1}, :a, fn val -> val + 1 end)
%{a: 2}
iex> JetExt.Map.indifferent_update!(%{"a" => 1}, :a, fn val -> val + 1 end)
%{"a" => 2}
iex> JetExt.Map.indifferent_update!(%{"a" => 1}, :b, fn val -> val + 1 end)
** (KeyError) key :b and "b" not found in: %{"a" => 1}
Put a value to map base on key type of the map.
iex> JetExt.Map.put_field(%{a: 1, b: 2}, :c, 3)
%{a: 1, b: 2, c: 3}
iex> JetExt.Map.put_field(%{"a" => 1, "b" => 2}, :c, 3)
%{"a" => 1, "b" => 2, "c" => 3}
iex> JetExt.Map.put_field(%{"a" => 1, b: 2}, :c, 3)
** (ArgumentError) expected map to be a map with atoms or string keys, got a map with mixed keys: %{:b => 2, "a" => 1}
Atomize keys of any map nested in the term recursively.
Example
iex> JetExt.Map.recursive_atomize_keys(%{"a" => 1, "b" => 2})
%{a: 1, b: 2}
iex> JetExt.Map.recursive_atomize_keys(%{"a" => 1, "b" => %{"c" => 2}})
%{a: 1, b: %{c: 2}}
iex> JetExt.Map.recursive_atomize_keys(%{"a" => [%{"b" => %{"c" => 2}}]})
%{a: [%{b: %{c: 2}}]}
iex> JetExt.Map.recursive_atomize_keys(%{"a" => [%{"b" => %{"c" => 2}}], d: true})
%{a: [%{b: %{c: 2}}], d: true}
iex> JetExt.Map.recursive_atomize_keys([%{"a" => 1}, "b"])
[%{a: 1}, "b"]
Stringify keys of map.
Example
iex> JetExt.Map.stringify_keys(%{a: 1, b: 2})
%{"a" => 1, "b" => 2}
iex> JetExt.Map.stringify_keys(%{a: 1, b: %{c: 2}})
%{"a" => 1, "b" => %{c: 2}}