indifferent v0.5.0 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

Returns a Keyword of named values at several indifferent paths

Examples

Functions

access(data, options \\ [])

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
at(key)

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, %{}}
get_and_update_in(data, keys, updater)

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}}}
get_in(data, keys)

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
pop_in(data, keys)

Auto wrap version of Kernel.pop_in

Examples

iex> Indifferent.pop_in(%{"a" => 1}, [:a])
{1, %{}}
unwrap(indifferent)
wrap(data, indifferent)

Macros

path(path)

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
path(data, path)

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> [1, [2, 3]] |> Indifferent.path([1][-1])
3

iex> [9, %{"c" => {:ok, %{"e" => 4}}}] |> Indifferent.path(1.c["1"].e)
4
paths(data, names_and_paths)

Returns a Keyword of named values at several indifferent paths.

Examples

iex> [x: v] = %{"a" => 1, "b" => %{"c" => 2}} |> Indifferent.paths(x: b.c)
...> v
2
read(path)

Examples

iex> require Indifferent
iex> System.put_env("COLOR", "red")
iex> Indifferent.read(System.get_env.COLOR)
"red"

iex> require Indifferent
iex> data = %{"x" => [1, 2]}
iex> Indifferent.read(data.x[-1])
2

iex> require Indifferent
iex> Indifferent.read(%{"x" => {1, 2}}.x[1])
2