View Source Transmogrify.Transmogrifier (transmogrify v2.0.1)

Convert keys and values in maps and lists of maps

Summary

Functions

Shortcut to clean out nulls, empty lists, and empty maps from a map

Primary function for transmogrifying maps and lists of data that may include maps.

Functions

Shortcut to clean out nulls, empty lists, and empty maps from a map

iex> prune(%{sub: %{}, keep: %{this: 1}, all: %{more: %{deeper: []}}}) %{keep: %{this: 1}}

Primary function for transmogrifying maps and lists of data that may include maps.

Options

  • key_convert: — convert types on keys in maps
    • :none | nil (default) — do not convert key types

    • :atom — make all binary/string keys into atoms
    • :semi_atom - make binary/string keys starting with : into atoms
    • :string — make all keys into binary/strings
  • value_convert: — convert types on values in maps
    • :none | nil (default) — do not convert key types

    • :atom — make all binary/string keys into atoms
    • :semi_atom - make binary/string keys starting with : into atoms
    • :string — make all keys into binary/strings
  • key_case: — convert case on keys in maps
    • nil (default) — do not alter case
    • :snake — convert keys into snake_case
    • :camel — convert keys into camelCase
    • :pascal — convert keys into PascalCase
  • value_case: — convert case on values in maps
    • nil (default) — do not alter case
    • :snake — convert keys into snake_case
    • :camel — convert keys into camelCase
    • :pascal — convert keys into PascalCase
  • deep: false | true (default) — recurse full depth of maps/lists or not

  • no_{what}_{from} — remove the specified {what} from the {from}, where {what} is nil, or 0 and list, map, or string; and {from} is either value (for maps) or elem (for lists).
    • no_nil_value: false (default) | true — remove key/value from map if value is nil

    • no_0list_value: false (default) | true — remove key/value from map if value is a zero-length list

    • no_0map_value: false (default) | true — remove key/value from a map if value is an empty map

    • no_0string_value: false (default) | true — remove key/value from a map if value is a zero-length string

    • no_nil_elem: false (default) | true — remove element from list if elem is nil

    • no_0list_elem: false (default) | true — remove element from list if elem is a zero-length list

    • no_0map_elem: false (default) | true — remove element from a list if elem is an empty map

    • no_0string_elem: false (default) | true — remove element from a list if elem is a zero-length string

Examples

iex> transmogrify(%{"Tardis" => %{"Key1" => 10}, "is" => 2, "The.Color" => "blue"}, key_convert: :atom)
%{Tardis: %{Key1: 10}, is: 2, "The.Color": "blue"}

iex> transmogrify(%{"Tardis" => %{"Key2" => 10}, "is" => 2, "The.Color" => "blue"}, key_convert: :atom, deep: false)
%{Tardis: %{"Key2" => 10}, is: 2, "The.Color": "blue"}

iex> transmogrify(%{"Sonic" => 1, "ScrewDriver" => ":thIrd"}, key_convert: :atom, key_case: :snake, value_convert: :atom, value_case: :snake)
%{sonic: 1, screw_driver: :th_ird}

iex> transmogrify([%{ :thisCase => 1, "thatCase" => 2 }], key_case: :snake, key_convert: :none)
[%{ :this_case => 1, "that_case" => 2}]

iex> transmogrify([%{ :this_case => 1, "that_case" => 2 }], key_case: :camel, key_convert: :none)
[%{ :thisCase => 1, "thatCase" => 2}]

iex> transmogrify([
...>     %{t1a: 1,
...>       t1b: ["red", nil, "", [], ":green"],
...>       t1c: [%{}], t1d: nil, t1e: "",
...>       t1f: %{TarVis: "blue"},
...>       t1j: ":sonic"}
...>   ],
...>   key_case: :snake, key_convert: :atom,
...>   value_case: :snake, value_convert: :none,
...>   no_nil_value: true, no_0list_value: true, no_0string_value: true, no_0map_value: true,
...>   no_nil_elem: true, no_0list_elem: true, no_0string_elem: true, no_0map_elem: true
...> )
[%{t1a: 1, t1b: ["red", ":green"], t1f: %{tar_vis: "blue"}, t1j: ":sonic"}]

iex> transmogrify([
...>     %{t2a: 1,
...>       t2b: ["red", nil, "", [], ":green"],
...>       t2c: [], t2d: nil, t2e: "",
...>       t2f: %{TarDis: "blue"},
...>       t2j: ":sonic"}, 10
...>   ],
...>   value_convert: :semi_atom
...> )
[%{ t2a: 1, t2b: ["red", nil, "", [], :green], t2f: %{TarDis: "blue"}, t2j: :sonic, t2c: [], t2d: nil, t2e: ""}, 10]

iex> transmogrify(%{key1: 10, keyTwo: "foo"}, key_convert: :string)
%{"key1" => 10, "keyTwo" => "foo"}

iex> transmogrify(%{key1: 10, keyTwo: "foo"}, key_convert: :string, key_case: :snake)
%{"key1" => 10, "key_two" => "foo"}