KitchenSink v1.0.1 KitchenSink.Map
This module is a mixin for the elixir map namespace
Summary
Functions
clean_struct take in a struct and removes the default values from it and returns a map
deep_merge overcomes a limitation of Map.merge in that it will merge trees. deep_merge will attempt to make the minimum possible change when it merges 2 trees
Compares two maps and returns a list of paths (used in Access) and the differences in values between the maps
Takes a Map and returns a List of Paths
takes in a list of keys, and a value. creates a nested map of each successive key nested as a child, with the most
nested map having the value value
given to the funciton
merge an array of maps! using merge/1
Similar to rename_key/3
Similar to rename_key, however this uses a key_map
that is larger
Takes a list of tuples of path(s) that describe from->to remappings of an object
rename_key remaps a value from one key to another in a map
like transform/2, but returns a function that takes a Map to be transformed
like transform/3 but doesn’t prune the output Map, preserving key value pairs that are not part of the transformation_map
transforms the keys and values of a map based on a map or tuple {key_list, function}
transforms the values of a map based on a map of functions
transforms the values of a map based on a map of functions
Functions
clean_struct take in a struct and removes the default values from it and returns a map
deep_merge overcomes a limitation of Map.merge in that it will merge trees. deep_merge will attempt to make the minimum possible change when it merges 2 trees.
Compares two maps and returns a list of paths (used in Access) and the differences in values between the maps
iex> KitchenSink.Map.diff(%{a: 1, b: 2}, %{a: 3, b: 2}) [{[:a], 1, 3}]
iex> KitchenSink.Map.diff(%{c: %{d: 1}}, %{c: %{d: 4}}) [{[:c, :d], 1, 4}]
Takes a Map and returns a List of Paths.
A Path is a list of keys that you can use to access a leaf node/value in a Map. Useful with get_in
Access syntax.
get_in(my_map, [:a, :b, :c])
Example
iex> KitchenSink.Map.key_paths(%{a: %{b: 1, c: 2}, d: 3})
[[:a, :b], [:a, :c], [:d]]
takes in a list of keys, and a value. creates a nested map of each successive key nested as a child, with the most
nested map having the value value
given to the funciton
merge an array of maps! using merge/1
Example:
iex> import KitchenSink.Map iex> merge [%{a: 1}, %{b: 2}, %{c: 3}, %{d: 4}] %{a: 1, b: 2, c: 3, d: 4}
Similar to rename_key/3
All of the keys that don’t have a remapping are preserved in the returned Map
Similar to rename_key, however this uses a key_map
that is larger.
key_map = %{
old_key1 => new_key1,
old_key2 => new_key2,
}
If any of the values of the key_map
are lists, then the output for that key will be a nested map based on the items
in the list.
The third argument is optional, if you use :prune then the Map returned will only contain the keys that are in the
key_map
Takes a list of tuples of path(s) that describe from->to remappings of an object
one can consider this to be like remap_keys or Map.take, but for nested keys. for each tuple of paths, the value of the first path will move to the second path. if there is only one path provided, then this will act more like Map.take
Example
iex> KitchenSink.Map.remapper(%{a: %{b: 1, c: 2}, d: 3}, [{[:a, :c]}])
%{a: %{c: 2}}
iex> KitchenSink.Map.remapper(%{a: %{b: 1, c: 2}, d: 3}, [{[:a, :c], [:d, :z]}])
%{d: %{z: 2}}
iex> KitchenSink.Map.remapper(%{a: %{b: 1, c: 2}, d: 3}, [{[:e], [:d, :c]}])
%{}
This function prunes the input map
rename_key remaps a value from one key to another in a map
Example:
iex> import KitchenSink.Map iex> rename_key %{a: 1, b: 2, c: 3, d: 4}, :a, :z %{z: 1, b: 2, c: 3, d: 4}
like transform/2, but returns a function that takes a Map to be transformed.
Examples
iex> KitchenSink.Map.transform(%{b: {[:b1, :b2], &Float.round(&1, 2)}}).(%{a: 1, b: 0.9876}) %{a: 1, b1: %{b2: 0.99}}
output is a function that takes a Map. the output Map is made from appling each of the transformers in the transformer-map to the corresponding keys and values in the Map, outputing a Map where each key-value in the Map has been transformed.
like transform/3 but doesn’t prune the output Map, preserving key value pairs that are not part of the transformation_map
Examples
iex> KitchenSink.Map.transform(%{a: 1, b: 0.9876}, %{b: {[:b1, :b2], &Float.round(&1, 2)}})
%{a: 1, b1: %{b2: 0.99}}
output is a transformed Map. the output Map is made from appling each of the
transformers in the transformer-map to the corresponding keys and values in the Map, outputing a Map where each
key-value in the Map has been transformed. supplying prune: true
prunes the map so only transformed values are
output.
transforms the keys and values of a map based on a map or tuple {key_list, function}.
input is a map of keys to tuples {key_list, function}
, representing a transformer-map.
key_list = any || [any, …] function = (any -> any)
Examples
iex> KitchenSink.Map.transform(%{b: 0.9876}, %{b: {[:b1, :b2], &Float.round(&1, 2)}}, prune: true)
%{b1: %{b2: 0.99}}
output is a transformed Map. the output Map is made from appling each of the
transformers in the transformer-map to the corresponding keys and values in the Map, outputing a Map where each
key-value in the Map has been transformed. supplying prune: true
prunes the map so only transformed values are
output.
transforms the values of a map based on a map of functions.
input is a map of keys to functions, representing a transformer-map. %{ a: a_transform_fun, b: b_transform_fun, … }
output is a function that takes a map and applies each of the transformers in the transformer-map to the corresponding value in the map, outputing a map where each key-value in the map has been transformed. prunes the map so only transformed values are output.
fn(%{a:, b: …}) -> %{a: a_transform_fun(a), b: b_transform_fun(b) …}