KitchenSink v1.3.7 KitchenSink.Map View Source

This module is a mixin for the elixir map namespace

Link to this section 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

Walks an object and recursively removes nodes with nil/empty values

Link to this section Functions

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

Link to this function deep_merge(left, right, options \\ []) View Source

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.

Link to this function deep_merge_list(list, options \\ []) View Source
Link to this function diff(primary, secondary) View Source

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}]

iex> KitchenSink.Map.diff(%{c: %{d: 1, e: 7}}, %{c: %{d: 4}}) [ {[:c, :d], 1, 4}, {[:c, :e], 7, nil} ]

iex> KitchenSink.Map.diff(%{c: %{d: 1}}, %{c: %{d: 4, e: 7}}) [ {[:c, :d], 1, 4}, {[:c, :e], nil, 7} ]

Link to this function do_trim(map, empty_val_fn?) View Source

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]]
Link to this function make_nested(value, key_list) View Source

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}

Link to this function remap_keys(map, key_map) View Source

Similar to rename_key/3

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

Link to this function remap_keys(map, key_map, list) View Source

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

Link to this function remapper(map, remap_list, lookup_nil_val \\ %{}) View Source

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

Link to this function rename_key(map, options) View Source

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}

Link to this function rename_key(map, current_key, new_key, options \\ [overwrite: false]) View Source
Link to this function transform(transformation_map) View Source

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.

Link to this function transform(map, transformation_map) View Source
transform(map, map) :: map

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.

Link to this function transform(map, transformation_map, opts) View Source
transform(map, map, Keyword.t) :: map

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.

Link to this function transform_values(transformer_map) View Source

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

Link to this function transform_values(map, transformer_map) View Source

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

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

Walks an object and recursively removes nodes with nil/empty values

Works for Structs as well!

Link to this function trim(map, empty_val_fn?) View Source