View Source Flamel.Map (flamel v1.10.0)
A bunch of helper functions for Maps
Summary
Functions
Assign values in a map
Converts the top level keys in a map from string to atoms
Recursively converts a struct to a map
Puts a value in a map if the value for that key is blank
Puts a value in a map if the value is present else it returns the map mutated
Converts the top level keys in a map from atoms to strings
Functions
Assign values in a map
Examples
iex> map = Flamel.Map.assign(%{tags: []}, push: [tags: "new"])
iex> map[:tags]
iex> ["new"]
iex> map = Flamel.Map.assign(%{}, set: [name: "Clark"])
iex> map[:name]
iex> "Clark"
iex> map = Flamel.Map.assign(%{assigns: %{}}, :assigns, set: [name: "Osa"])
iex> get_in(map, [:assigns, :name])
iex> "Osa"
iex> map = Flamel.Map.assign(%{assigns: %{}}, :assigns, %{name: "Clark"})
iex> get_in(map, [:assigns, :name])
iex> "Clark"
Converts the top level keys in a map from string to atoms
Examples
iex> Flamel.Map.atomize_keys(%{"first_name" => "Thomas", "dob" => "07/01/1981"})
%{first_name: "Thomas", dob: "07/01/1981"}
iex> Flamel.Map.atomize_keys(%{"first_name" => "Thomas", "dob" => "07/01/1981", "skills" => [%{"name" => "sewing"}]})
%{first_name: "Thomas", dob: "07/01/1981", skills: [%{name: "sewing"}]}
iex> Flamel.Map.atomize_keys(%{"person" => %{"first_name" => "Thomas", "dob" => "07/01/1981"}})
%{person: %{first_name: "Thomas", dob: "07/01/1981"}}
iex> Flamel.Map.atomize_keys(%{first_name: "Thomas", dob: "07/01/1981"})
%{first_name: "Thomas", dob: "07/01/1981"}
Recursively converts a struct to a map
Puts a value in a map if the value for that key is blank
Puts a value in a map if the value is present else it returns the map mutated
Converts the top level keys in a map from atoms to strings
Examples
iex> Flamel.Map.stringify_keys(%{a: 1, b: 2})
%{"a" => 1, "b" => 2}
iex> Flamel.Map.stringify_keys(%{a: 1, b: [%{a: 1}, %{b: 2}]})
%{"a" => 1, "b" => [%{"a" => 1}, %{"b" => 2}]}
iex> Flamel.Map.stringify_keys(%{a: 1, b: 2, c: %{d: 3, e: 4}})
%{"a" => 1, "b" => 2, "c" => %{"d" => 3, "e" => 4}}
iex> Flamel.Map.stringify_keys(%{"a" => 1, "b" => 2})
%{"a" => 1, "b" => 2}