transmap v0.1.0 Transmap

Summary

Functions

Transforms the given map

Functions

transform(data, rule)

Specs

transform(data :: any, rule :: any) :: any

Transforms the given map.

rule_map is a map has elements key => rule.

case {key, rule} do
  {:_default, rule} -> # Change the default rule for the map from false to the given rule
  {:_spread, targets} -> # Pop the maps for specific keys and merge it into the result map
  {key, true} -> # Put the value for a specific key to the result map.
  {key, false} -> # Don't put the value for a specific key to the result map.
  {key, rule_map} when is_map(rule_map) -> # Transform the map for a specific key with the given rule
  {key, {new_key, rule}} -> # Rename the key to new_key
  {key, new_key} -> # Shorthand for {new_key, true}
end

Examples

Basic transformation:

iex> map = %{a: 1, b: 2, c: 3}
iex> rule = %{a: true}
iex> Transmap.transform(map, rule)
%{a: 1}

iex> map = %{a: %{b: 1, c: 2}, d: %{e: 3, f: 4}}
iex> rule = %{a: %{c: true}, d: true}
iex> Transmap.transform(map, rule)
%{a: %{c: 2}, d: %{e: 3, f: 4}}

Transfomation with _default:

iex> map = %{a: 1, b: 2, c: %{d: 3, e: 4}}
iex> rule = %{_default: true, b: false, c: %{_default: true}}
iex> Transmap.transform(map, rule)
%{a: 1, c: %{d: 3, e: 4}}

Transformation with _spread:

iex> map = %{a: %{b: 1, c: 2}, d: %{e: %{f: 3, g: 4}, h: 5}}
iex> rule = %{_spread: [[:a], [:d, :e]], a: true, d: %{e: %{f: true}}}
iex> Transmap.transform(map, rule)
%{b: 1, c: 2, f: 3}

Transformation with renaming:

iex> map = %{a: 1, b: 2, c: %{d: 3, e: 4}, f: %{g: 5}}
iex> rule = %{_spread: [[:f]], a: {"A", true}, b: "B", c: {:C, %{d: 6}}, f: %{g: "G"}}
iex> Transmap.transform(map, rule)
%{"A" => 1, "B" => 2, :C => %{6 => 3}, "G" => 5}