morphix v0.0.3 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 as an argument and returns the same map with string keys converted to atom keys. Does not examine nested 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 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 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]}