Box.Map (box v0.15.1)

View Source

Extra functions to work with map.

Specifically, this include function to work with maps independent of the key type. These functions should be used everywhere we manipulate changeset params to ensure they both work with string and atom maps and support UI inputs as well as console input.

Summary

Functions

Gets a value in a map from key. The provided needs to be an atom though it'll automatically be converted to a string if the map is string keyed.

Gets first entry that matches in the map

Gets a value from a key in a map with a default value. Unlike Map.get/3, this function will return the default even if the key exists but the value is nil.

Maps map values within the same key

Puts a value in a map. The provided needs to be an atom though it'll automatically be converted to a string if the map is string keyed.

Puts a new value in a map. The provided needs to be an atom though it'll automatically be converted to a string if the map is string keyed.

Puts a new lazy value in a map. The provided needs to be an atom though it'll automatically be converted to a string if the map is string keyed.

Takes only the given keys no matter the key type

Updates a map value from its key only if it exists

Functions

get(map, key)

@spec get(map(), atom()) :: any()

Gets a value in a map from key. The provided needs to be an atom though it'll automatically be converted to a string if the map is string keyed.

get_first(map, keys)

@spec get_first(map(), [atom()]) :: {atom(), any()} | nil

Gets first entry that matches in the map

get_with_default(map, key, default)

@spec get_with_default(map(), any(), any()) :: any()

Gets a value from a key in a map with a default value. Unlike Map.get/3, this function will return the default even if the key exists but the value is nil.

map_values(map, function)

@spec map_values(map(), (any(), any() -> any())) :: map()

Maps map values within the same key

put(map, key, value)

@spec put(map(), atom(), any()) :: map()

Puts a value in a map. The provided needs to be an atom though it'll automatically be converted to a string if the map is string keyed.

put_new(map, key, value)

@spec put_new(map(), atom(), any()) :: map()

Puts a new value in a map. The provided needs to be an atom though it'll automatically be converted to a string if the map is string keyed.

put_new_lazy(map, key, value_function)

@spec put_new_lazy(map(), atom(), (-> any())) :: map()

Puts a new lazy value in a map. The provided needs to be an atom though it'll automatically be converted to a string if the map is string keyed.

rename(map, from, to, default \\ nil)

@spec rename(map(), any(), any(), any()) :: map()

Renames a key

take(map, keys)

@spec take(map(), [atom()]) :: map()

Takes only the given keys no matter the key type

update_if_exists(map, key, function)

@spec update_if_exists(map(), any(), (any() -> any())) :: map()

Updates a map value from its key only if it exists