indifferent v0.1.1 Indifferent
Summary
Functions
Creates an indifferent accessor for the given data structure
Returns a function for accessing an indifferent item
Auto wrap version of Kernel.get_and_update_in
Auto wrap version of Kernel.get_in
Auto wrap version of Kernel.pop_in
Macros
Returns an accessor-function for the given path, intended to be used by Kernel functions
Given a data structure and a path, fetches the corresponding value
Functions
Creates an indifferent accessor for the given data structure.
Options can be key_transforms:
and value_transforms:
.
See Indifferent.Transform
key_transforms
are a list of functions for casting indifferent keys
for example converting an string to an existing atom, these functions
must return {:ok, new_key}
if conversion was possible.
Examples
iex> i = Indifferent.access(%{"a" => 1})
iex> i[:a]
1
iex> i = Indifferent.access(%{"a" => 1},
...> key_transforms: [fn x, _indifferent -> {:ok, String.downcase(x)} end])
iex> i["A"]
1
Returns a function for accessing an indifferent item.
Intended to be used with Kernel access functions.
Examples
iex> Kernel.get_in(%{"a" => 1}, [Indifferent.at(:a)])
1
iex> keys = [:a, :b] |> Enum.map(&Indifferent.at/1)
iex> Kernel.get_and_update_in(%{"a" => %{"b" => 2}}, keys, fn x -> {x * 3, x * 4} end)
{6, %{"a" => %{"b" => 8}}}
iex> Kernel.pop_in(%{"a" => 1}, [Indifferent.at(:a)])
{1, %{}}
Auto wrap version of Kernel.get_and_update_in
Examples
iex> Indifferent.get_and_update_in(%{"a" => %{"x" => 1}}, [:a, :x], fn x -> {x * 2, x * 4} end)
{2, %{"a" => %{"x" => 4}}}
iex> Indifferent.get_and_update_in(%{"a" => %{"x" => 1}}, [:a, :y], fn nil -> {2, 4} end)
{2, %{"a" => %{:y => 4, "x" => 1}}}
Auto wrap version of Kernel.get_in
Examples
iex> Indifferent.get_in(%{"a" => %{"x" => 1}}, [:a])
%{"x" => 1}
iex> Indifferent.get_in(%{"a" => %{"x" => 1}}, [:a, :x])
1
Macros
Returns an accessor-function for the given path, intended to be used by Kernel functions.
Examples
iex> Kernel.get_in(%{“a” => {0, 1}}, Indifferent.path(a[“1”])) 1
Given a data structure and a path, fetches the corresponding value.
Examples
iex> %{“a” => 1, “b” => %{“c” => 2}} |> Indifferent.path(b.c) 2
iex> %{“b” => %{“c” => %{“d” => %{“e” => 4}}}} |> Indifferent.path(b[“c”][:d].e) 4
iex> [1, 2] |> Indifferent.path(0) 1
iex> {1, 2} |> Indifferent.path(0) 1
iex> [1, {2, 3}] |> Indifferent.path([1][0]) 2
iex> [9, %{“c” => {:ok, %{“e” => 4}}}] |> Indifferent.path(1.c[“1”].e) 4