KitchenSink v0.0.9 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

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

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(struct)

clean_struct take in a struct and removes the default values from it and returns a map

deep_merge(list)
deep_merge(left, right)

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.

make_nested(key_list, value)

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(list)

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}

remap_keys(map, key_map)

Similar to rename_key/3

All of the keys that don’t have a remapping are preserved in the returned Map

remap_keys(map, key_map, list)

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

rename_key(map, options)

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}

rename_key(map, key, key)
transform(key_value_transform_map)

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.

transform(map, key_value_transform_map)
transform(Map.t, Map.t) :: Map.t

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.

transform(map, key_value_transform_map, opts)
transform(Map.t, Map.t, Keyword.t) :: Map.t

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.

transform_values(transformer_map)

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

transform_values(map, transformer_map)

transforms the values of a map based on a map of functions.

similar to transform_values/1, however doesn’t return a function.