iteraptor v0.2.1 Iteraptor

Iteraptor makes complicated nested structures (currently maps and lists) iteration easier.

Summary

Functions

Iterates the given nested structure, calling the callback provided on each value. The key returned is a concatenated names of all the parent keys (and or indices in a case of an array.)

Build a nested structure out of a flatmap given, decomposing the names of keys and handling lists carefully. Example

Build a flatmap out of nested structure, concatenating the names of keys. Example

Functions

each(input, joiner \\ ".", fun)

Iterates the given nested structure, calling the callback provided on each value. The key returned is a concatenated names of all the parent keys (and or indices in a case of an array.)

Example:

  %{a: %{b: %{c: 42}}} |> Iteraptor.each(fn {k, v} -> IO.inspect({k, v}) end)
  #⇒ %{"a.b.c": 42}

The return value is the result of call to to_flatmap.

iex> %{a: %{b: %{c: 42}}} |> Iteraptor.each(fn {k, v} -> IO.inspect({k, v}) end) %{“a.b.c”: 42}

from_flatmap(input, joiner \\ ".")

Build a nested structure out of a flatmap given, decomposing the names of keys and handling lists carefully. Example:

  %{"a.b.c": 42, "a.b.d.0": nil, "a.b.d.1": 42, "a.e.0": :f, "a.e.1": 42} |> Iteraptor.from_flatmap
  %{a: %{b: %{c: 42, d: [nil, 42]}, e: [:f, 42]}}

iex> %{“a.b.c”: 42} |> Iteraptor.from_flatmap %{a: %{b: %{c: 42}}}

iex> %{“a.b.c”: 42, “a.b.d”: 42} |> Iteraptor.from_flatmap %{a: %{b: %{c: 42, d: 42}}}

iex> %{“a.b.c”: 42, “a.b.d”: 42, “a.e”: 42} |> Iteraptor.from_flatmap %{a: %{b: %{c: 42, d: 42}, e: 42}}

iex> %{“0”: 42, “1”: 42} |> Iteraptor.from_flatmap [42, 42]

iex> %{“1”: :a1, “0”: :a0, “2”: :a2, “3”: :a3, “4”: :a4, “5”: :a5, “6”: :a6, “7”: :a7, “8”: :a8, “9”: :a9, “10”: :a10, “11”: :a11} |> Iteraptor.from_flatmap [:a0, :a1, :a2, :a3, :a4, :a5, :a6, :a7, :a8, :a9, :a10, :a11]

iex> %{“0.a”: 42, “0.b”: 42} |> Iteraptor.from_flatmap [%{a: 42, b: 42}]

iex> %{“a.b.c”: 42, “a.b.d.0”: nil, “a.b.d.1”: 42, “a.e.0”: :f, “a.e.1”: 42} |> Iteraptor.from_flatmap %{a: %{b: %{c: 42, d: [nil, 42]}, e: [:f, 42]}}

to_flatmap(input, joiner \\ ".")

Build a flatmap out of nested structure, concatenating the names of keys. Example:

  %{a: %{b: %{c: 42, d: [nil, 42]}, e: [:f, 42]}} |> Iteraptor.to_flatmap
  %{"a.b.c": 42, "a.b.d.0": nil, "a.b.d.1": 42, "a.e.0": :f, "a.e.1": 42}

Lists are handled gracefully, index is used as a key in resulting map.

iex> [:a, 42] |> Iteraptor.to_flatmap %{“0”: :a, “1”: 42}

iex> %{a: 42} |> Iteraptor.to_flatmap %{a: 42}

iex> %{a: 42, b: 42} |> Iteraptor.to_flatmap %{a: 42, b: 42}

iex> %{a: %{b: 42}, d: 42} |> Iteraptor.to_flatmap %{“a.b”: 42, d: 42}

iex> %{a: [:b, 42], d: 42} |> Iteraptor.to_flatmap %{“a.0”: :b, “a.1”: 42, d: 42}

iex> %{a: %{b: [:c, 42]}, d: 42} |> Iteraptor.to_flatmap %{“a.b.0”: :c, “a.b.1”: 42, d: 42}

iex> %{a: %{b: 42}} |> Iteraptor.to_flatmap %{“a.b”: 42}

iex> %{a: %{b: %{c: 42}}} |> Iteraptor.to_flatmap %{“a.b.c”: 42}

iex> %{a: %{b: %{c: 42}}, d: 42} |> Iteraptor.to_flatmap %{“a.b.c”: 42, d: 42}

iex> %{a: %{b: %{c: 42, d: [nil, 42]}, e: [:f, 42]}} |> Iteraptor.to_flatmap %{“a.b.c”: 42, “a.b.d.0”: nil, “a.b.d.1”: 42, “a.e.0”: :f, “a.e.1”: 42}