morphix v0.0.7 Morphix
Morphix provides convenience methods for dealing with Maps, Lists, and Tuples.
morphiflat
and morphiflat!
flatten maps, discarding top level keys.
Examples:
iex> Morphix.morphiflat %{flatten: %{this: "map"}, if: "you please"}
{:ok, %{this: "map", if: "you please"}}
iex> Morphix.morphiflat! %{flatten: %{this: "map"}, o: "k"}
%{this: "map", o: "k"}
morphify!
and morphify
will take either a List or a Tuple as the first argument, and a function as the second. Returns a map, with the keys of the map being the function applied to each member of the input.
Examples:
iex> Morphix.morphify!({[1,2,3], [12], [1,2,3,4]}, &length/1)
%{1 => [12], 3 => [1,2,3], 4 => [1,2,3,4]}
atomorphify
and atomorphiform
take a map as an input and return the map with all string keys converted to atoms. atomorphiform
is recursive.
Examples:
iex> Morphix.atomorphify(%{"a" => "2", :a => 2, 'a' => :two})
{:ok, %{:a => 2, 'a' => :two }}
Summary
Functions
Takes a map as an argument and returns the same map, with all string keys (including keys in nested maps) converted to atom keys
Takes a map and the :safe
flag as arguments and returns {:ok, map}
, with any strings that are existing atoms converted to atoms, and any strings that are not existing atoms left as strings
Takes a map as an argument and returns the same map with string keys converted to atom keys. Does not examine nested maps
Takes a map and the :safe
flag, returns the same map, with string keys converted to existing atoms if possible, and ignored otherwise. Ignores nested maps
Removes keys with nil values from maps, handles nested maps and treats empty maps as nil values
Removes keys with nil values from nested maps, also eliminates empty maps
Takes a map and removes any keys that have nil values
Takes a map and removes keys that have nil values, or are empty maps
Takes a map and returns a flattened version of that map. If the map has nested maps (or the maps nested maps have nested maps, etc.) morphiflat moves all nested key/value pairs to the top level, discarding the original keys
Takes a map and returns a flattend version of that map, discarding any nested keys
Takes a List and a function as arguments and returns {:ok, Map}
, with the keys of the map the result of applying the function to each item in the list
Takes a list and a function as arguments and returns a Map, with the keys of the map the result of applying the function to each item in the list
Functions
Takes a map as an argument and returns the same map, with all string keys (including keys in nested maps) converted to atom keys.
Examples:
iex> Morphix.atomorphiform(%{:this => %{map: %{"has" => "a", :nested => "string", :for => %{a: :key}}}, "the" => %{"other" => %{map: :does}}, as: "well"})
{:ok,%{this: %{map: %{has: "a", nested: "string", for: %{a: :key}}}, the: %{other: %{map: :does}}, as: "well"} }
iex> Morphix.atomorphiform(%{"this" => ["map", %{"has" => ["a", "list"]}], "inside" => "it"})
{:ok, %{this: ["map", %{has: ["a", "list"]}], inside: "it"}}
Takes a map and the :safe
flag as arguments and returns {:ok, map}
, with any strings that are existing atoms converted to atoms, and any strings that are not existing atoms left as strings.
Works recursively on embedded maps.
Examples:
iex> [:allowed, :values]
iex> map = %{"allowed" => "atoms", "embed" => %{"will" => "convert", "values" => "to atoms"}}
iex> Morphix.atomorphiform(map, :safe)
{:ok, %{"embed" => %{"will" => "convert", values: "to atoms"}, allowed: "atoms"}}
Takes a map as an argument and returns the same map with string keys converted to atom keys. Does not examine nested maps.
Examples
iex> Morphix.atomorphify(%{"this" => "map", "has" => %{"string" => "keys"}})
{:ok, %{this: "map", has: %{"string" => "keys"}}}
iex> Morphix.atomorphify(%{1 => "2", "1" => 2, "one" => :two})
{:ok, %{1 => "2", "1": 2, one: :two}}
Takes a map and the :safe
flag, returns the same map, with string keys converted to existing atoms if possible, and ignored otherwise. Ignores nested maps.
Examples:
iex> :existing_atom
iex> Morphix.atomorphify(%{"existing_atom" => "exists", "non_existent_atom" => "does_not", 1 => "is_ignored"}, :safe)
{:ok, %{ "non_existent_atom" => "does_not", 1 => "is_ignored", existing_atom: "exists"}}
Removes keys with nil values from maps, handles nested maps and treats empty maps as nil values.
Examples
iex> Morphix.compactiform(%{a: nil, b: "not", c: %{d: nil, e: %{}, f: %{g: "value"}}})
{:ok, %{b: "not", c: %{f: %{g: "value"}}}}
iex> Morphix.compactiform(5)
{:error, %FunctionClauseError{arity: 1, function: :compactiform!, module: Morphix}}
Removes keys with nil values from nested maps, also eliminates empty maps.
Examples
iex> Morphix.compactiform!(%{nil_nil: nil, not_nil: "a value", nested: %{nil_val: nil, other: "other"}})
%{not_nil: "a value", nested: %{other: "other"}}
iex> Morphix.compactiform!(%{nil_nil: nil, not_nil: "a value", nested: %{nil_val: nil, other: "other", nested_empty: %{}}})
%{not_nil: "a value", nested: %{other: "other"}}
Takes a map and removes any keys that have nil values.
Examples
iex> Morphix.compactify(%{nil_key: nil, not_nil: "real value"})
{:ok, %{not_nil: "real value"}}
iex> Morphix.compactify("won't work")
{:error, %FunctionClauseError{arity: 1, function: :compactify!, module: Morphix}}
Takes a map and removes keys that have nil values, or are empty maps.
Examples
iex> Morphix.compactify!(%{nil_key: nil, not_nil: "nil"})
%{not_nil: "nil"}
iex> Morphix.compactify!(%{empty: %{}, not: "not"})
%{not: "not"}
Takes a map and returns a flattened version of that map. If the map has nested maps (or the maps nested maps have nested maps, etc.) morphiflat moves all nested key/value pairs to the top level, discarding the original keys.
Examples:
iex> Morphix.morphiflat %{this: %{nested: :map, inner: %{twonested: :map, is: "now flat"}}}
{:ok, %{nested: :map, twonested: :map, is: "now flat"}}
In the example, the key :this
is discarded, along with the key inner
, because they both point to map values.
Will return {:error, <input> is not a Map}
if the input is not a map.
Examples:
iex> Morphix.morphiflat({1,2,3})
{:error, "{1, 2, 3} is not a Map"}
Takes a map and returns a flattend version of that map, discarding any nested keys.
Examples:
iex> Morphix.morphiflat! %{you: "will", youwill: %{be: "discarded"}}
%{you: "will", be: "discarded"}
Takes a List and a function as arguments and returns {:ok, Map}
, with the keys of the map the result of applying the function to each item in the list.
If the function cannot be applied, will return {:error, message}
Examples
iex> Morphix.morphify([[1,2,3], [12], [1,2,3,4]], &Enum.count/1)
{:ok, %{1 => [12], 3 => [1,2,3], 4 => [1,2,3,4]}}
iex> Morphix.morphify({[1,2,3], [12], [1,2,3,4]}, &length/1)
{:ok, %{1 => [12], 3 => [1,2,3], 4 => [1,2,3,4]}}
iex> Morphix.morphify([1,2], &String.length/1)
{:error, "Unable to apply &String.length/1 to each of [1, 2]"}
Takes a list and a function as arguments and returns a Map, with the keys of the map the result of applying the function to each item in the list.
Examples
iex> Morphix.morphify!([[1,2,3], [12], [1,2,3,4]], &Enum.count/1)
%{1 => [12], 3 => [1,2,3], 4 => [1,2,3,4]}