Krug.MapUtil (Krug v2.0.27) View Source

Utilitary safe module to manipulate Map structs.

Link to this section Summary

Functions

Delete a value from a Map, respective to a key.

Delete all values from a Map, respective to received keys in list/enum.

Obtain a value from a Map, respective to a key.

Obtain a value from a Map, respective to a ATOM key.

Obtain a value from a Map, respective to a STRING key.

Replace/update a value from a Map, respective to a key.

Replace/update a value from a Map, respective to a ATOM key.

Replace/update a value from a Map, respective to a STRING key.

Link to this section Functions

Delete a value from a Map, respective to a key.

If receive a nil/invalid/empty key, return the map received.

If receive a nil map or received map is invalid or don't contains the key, return the map received.

Example

iex > map = %{a: 1, b: 3}
iex > Krug.MapUtil.delete(map,:a)
%{b: 3}

Delete all values from a Map, respective to received keys in list/enum.

If receive a nil/invalid/empty keys list/enum, return the map received.

Ignore keys that are invalid or don't exist in received map.

Example

iex > map = %{a: 1, b: 3, c: 10}
iex > Krug.MapUtil.delete_all(map,[:a,:c,:d])
%{b: 3}

Obtain a value from a Map, respective to a key.

If the map or key received is nil/invalid return nil.

If key don't exists in map, return nil.

Example

iex > map = %{a: 1, b: 3}
iex > Krug.MapUtil.get(map,:a)
1
Link to this function

get_by_atom_key(map, key)

View Source (since 1.1.42)

Obtain a value from a Map, respective to a ATOM key.

Use only for performatic reasons, and you are sure that key exists on map and is from atom type.

Example

iex > map = %{a: 1, b: 3}
iex > Krug.MapUtil.get_by_atom_key(map,:a)
1
iex > map = %{a: 1, b: 3}
iex > Krug.MapUtil.get_by_atom_key(map,:c)
throws an exception
iex > map = %{a: 1, b: 3}
iex > Krug.MapUtil.get_by_atom_key(map,"a")
throws an exception
Link to this function

get_by_string_key(map, key)

View Source (since 1.1.42)

Obtain a value from a Map, respective to a STRING key.

Use only for performatic reasons, and you are sure that key exists on map and is from string type.

Example

iex > map = %{"a" => 1, "b" => 3}
iex > Krug.MapUtil.get_by_string_key(map,"a")
1
iex > map = %{"a" => 1, "b" => 3}
iex > Krug.MapUtil.get_by_string_key(map,"c")
throws an exception
iex > map = %{"a" => 1, "b" => 3}
iex > Krug.MapUtil.get_by_string_key(map,:a)
throws an exception
Link to this function

replace(map, key, new_value)

View Source

Replace/update a value from a Map, respective to a key.

If receive a nil/invalid key, return the map received.

If receive a nil map or received map is invalid or don't contains the key, return the map received.

If receives a valid value and the key received is valid and not exists in map, then add the new key and value to map.

Examples

iex > map = %{a: 1, b: 3}
iex > Krug.MapUtil.replace(map,:a,3)
%{a: 3, b: 3}
iex > map = %{a: 1, b: 3}
iex > Krug.MapUtil.replace(map,:c,101)
%{a: 3, b: 3, c: 101}
Link to this function

replace_by_atom_key(map, key, new_value)

View Source (since 1.1.42)

Replace/update a value from a Map, respective to a ATOM key.

Use only for performatic reasons, and you are sure that key exists on map and is from atom type.

Examples

iex > map = %{a: 1, b: 3}
iex > Krug.MapUtil.replace_by_atom_key(map,:a,3)
%{a: 3, b: 3}
iex > map = %{a: 1, b: 3}
iex > Krug.MapUtil.replace_by_atom_key(map,:c,101)
throws an exception
iex > map = %{a: 1, b: 3}
iex > Krug.MapUtil.replace_by_atom_key(map,"b",101)
throws an exception
Link to this function

replace_by_string_key(map, key, new_value)

View Source (since 1.1.42)

Replace/update a value from a Map, respective to a STRING key.

Use only for performatic reasons, and you are sure that key exists on map and is from string type.

Examples

iex > map = %{"a" => 1, "b" => 3}
iex > Krug.MapUtil.replace_by_string_key(map,"a",3)
%{"a" => 3, "b" => 3}
iex > map = %{"a" => 1, "b" => 3}
iex > Krug.MapUtil.replace_by_string_key(map,"c",3)
throws an exception
iex > map = %{"a" => 1, "b" => 3}
iex > Krug.MapUtil.replace_by_string_key(map,:b,3)
throws an exception