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

@spec assign(
  map(),
  keyword()
) :: map()
Link to this function

assign(map, key, values)

View Source
@spec assign(map(), atom(), keyword()) :: map()
@spec assign(map(), atom(), map()) :: map()

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"}
@spec from_struct(struct()) :: map()

Recursively converts a struct to a map

Link to this function

put_if_blank(values, key, fun)

View Source
@spec put_if_blank(map(), binary() | atom(), function()) :: map()

Puts a value in a map if the value for that key is blank

Link to this function

put_if_present(values, key, value)

View Source
@spec put_if_present(map(), binary() | atom(), any()) :: map()

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}