KitchenSink v0.0.27 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 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(struct)

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

deep_merge(left, right, options \\ [])

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.

deep_merge_list(list, options \\ [])
key_paths(map)

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]]
make_nested(value, key_list)

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

remapper(map, remap_list, lookup_nil_val \\ %{})

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(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, current_key, new_key, options \\ [overwrite: false])
transform(transformation_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, transformation_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, transformation_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.