View Source Transmogrify.Transmogrifier (transmogrify v1.1.3)

Convert keys and values in maps and lists of maps

Summary

Functions

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

Functions

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

  • clean_nil: false (default) | true — remove list/map entry if value is nil

  • clean_empty_list: false (default) | true — remove list/map entry if zero-length list

  • clean_empty_string: false (default) | true — remove list/map entry if zero-length string

Examples

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

iex> transmogrify(%{"Tardis" => %{"Key" => 10}, "is" => 2, "The.Color" => "blue"}, key_convert: :atom, deep: false)
%{Tardis: %{"Key" => 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([
...>     %{a: 1,
...>       b: ["red", nil, "", [], ":green"],
...>       c: [], d: nil, e: "",
...>       f: %{TarVis: "blue"},
...>       j: ":sonic"}
...>   ],
...>   key_case: :snake, key_convert: :atom,
...>   value_case: :snake, value_convert: :none,
...>   clean_nil: true, clean_empty_list: true, clean_empty_string: true
...> )
[%{a: 1, b: ["red", ":green"], f: %{tar_vis: "blue"}, j: ":sonic"}]

iex> transmogrify([
...>     %{a: 1,
...>       b: ["red", nil, "", [], ":green"],
...>       c: [], d: nil, e: "",
...>       f: %{TarDis: "blue"},
...>       j: ":sonic"}, 10
...>   ],
...>   value_convert: :semi_atom
...> )
[%{ a: 1, b: ["red", nil, "", [], :green], f: %{TarDis: "blue"}, j: :sonic, c: [], d: nil, e: ""}, 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"}