Dotmap (dotmap v0.1.1)

A module for handling conversions of maps into dot notation and vice versa.

Summary

Functions

Converts a map into an array of tuples. Returns a tuple with the result or an error message.

Converts a map into an array of tuples. An ArgumentError is raised if a key is not a string.

Converts an array of tuples into a map. Returns a tuple with the result or an error message.

Converts an array of tuples into a map.

Adds a key value pair to a map. It will create any necessary nested maps if they do not exist.

Types

Link to this type

compact_list()

@type compact_list() :: [{String.t(), String.t()}]
Link to this type

string_map()

@type string_map() :: %{required(String.t()) => any()}

Functions

@spec contract(map :: string_map()) :: {:ok, compact_list()} | {:error, String.t()}

Converts a map into an array of tuples. Returns a tuple with the result or an error message.

Examples

iex> Dotmap.contract(%{"a" => 1, "b" => 2})
{:ok, [{"a", 1}, {"b", 2}]}

iex> Dotmap.contract(%{1 => 1})
{:error, "Key must be a string"}
@spec contract!(map :: string_map()) :: compact_list()

Converts a map into an array of tuples. An ArgumentError is raised if a key is not a string.

Examples

iex> Dotmap.contract!(%{"a" => 1, "b" => 2})
[{"a", 1}, {"b", 2}]

iex> Dotmap.contract!(%{"a" => 1, "b" => 2, "c" => %{"d" => 3, "e" => 4}})
[{"a", 1}, {"b", 2}, {"c.d", 3}, {"c.e", 4}]

iex> Dotmap.contract!(%{1 => 1})
** (ArgumentError) Key must be a string
@spec expand(list :: compact_list()) :: {:ok, string_map()} | {:error, String.t()}

Converts an array of tuples into a map. Returns a tuple with the result or an error message.

Examples

iex> Dotmap.expand([{"a", 1}, {"b", 2}])
{:ok, %{"a" => 1, "b" => 2}}

iex> Dotmap.expand([{1, 1}])
{:error, "Key must be a string"}
@spec expand!(list :: compact_list()) :: string_map()

Converts an array of tuples into a map.

Examples

iex> Dotmap.expand!([{"a", 1}, {"b", 2}])
%{"a" => 1, "b" => 2}

iex> Dotmap.expand!([{"a", 1}, {"b", 2}, {"c.d", 3}, {"c.e", 4}])
%{"a" => 1, "b" => 2, "c" => %{"d" => 3, "e" => 4}}

iex> Dotmap.expand!([{1, 1}])
** (ArgumentError) Key must be a string
Link to this function

place(map, key, value)

@spec place(map :: string_map(), key :: String.t(), value :: any()) :: string_map()

Adds a key value pair to a map. It will create any necessary nested maps if they do not exist.

Examples

iex> Dotmap.place(%{"a" => 1}, "b.c", 2)
%{"a" => 1, "b" => %{"c" => 2}}