View Source JetExt.Map (jet_ext v0.2.5)

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

@spec atomize_keys(map()) :: map()

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}]}
@spec fetch(map(), key) :: {key, {:ok, term()} | :error} when key: term()

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}
Link to this function

filter(data, filter_funciton)

View Source
@spec filter(map(), function()) :: map()

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}
Link to this function

indifferent_fetch(map, key)

View Source
@spec indifferent_fetch(map(), key :: atom()) :: term()

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
Link to this function

indifferent_fetch!(map, key)

View Source
@spec indifferent_fetch!(map(), key :: atom()) :: term()

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}
Link to this function

indifferent_get(map, key, default \\ nil)

View Source
@spec indifferent_get(map(), key :: atom(), default :: term()) :: term()

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
Link to this function

indifferent_has_key(map, key)

View Source
@spec indifferent_has_key(map(), key :: atom()) :: {:ok, binary() | atom()} | :error

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
Link to this function

indifferent_take(map, keys)

View Source
@spec indifferent_take(map(), keys :: [atom()]) :: map()

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])
%{}
Link to this function

indifferent_update!(map, key, fun)

View Source
@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}
Link to this function

put_field(map, key, value)

View Source
@spec put_field(map(), atom(), term()) :: map()

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}
Link to this function

recursive_atomize_keys(data)

View Source
@spec recursive_atomize_keys(term()) :: term()

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"]
@spec stringify_keys(map()) :: map()

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