View Source Bonfire.Common.E (Bonfire v0.9.11-social-beta.6)

Helper to extract data nested in an object

Summary

Functions

Extracts a value from a map or other data structure, or returns a fallback if not present or empty. If additional arguments are provided, it searches for nested data structures, with the last argument always being the fallback.

Returns a value if it is not empty, or a fallback value if it is empty.

Extracts a value from a map or other data structure, or returns a fallback if not present or empty. If additional arguments are provided, it searches for nested data structures, with the last argument always being the fallback.

Returns a value from a nested map, or a fallback if not present

Functions

e(val, fallback \\ nil)

e(object, key1, fallback)

(macro)

Extracts a value from a map or other data structure, or returns a fallback if not present or empty. If additional arguments are provided, it searches for nested data structures, with the last argument always being the fallback.

Examples

iex> e(%{key: "value"}, :key, "fallback")
"value"

iex> e(%{key: nil}, :key, "fallback")
"fallback"

iex> e(%{key: "value"}, :missing_key, "fallback")
"fallback"

iex> e(%{key: %Ecto.Association.NotLoaded{}}, :key, "fallback")
"fallback"

iex> e({:ok, %{key: "value"}}, :key, "fallback") # FIXME
"value"

iex> e(%{__context__: %{key: "context_value"}}, :key, "fallback") # FIXME
"context_value"

iex> e(%{a: %{b: "value"}}, :a, :b, "fallback")
"value"

iex> e(%{a: %{b: %Ecto.Association.NotLoaded{}}}, :a, :b, "fallback")
"fallback"

iex> e(%{a: %{b: nil}}, :a, :b, "fallback")
"fallback"

iex> e(%{a: %{b: %{c: "value"}}}, :a, :b, :c, "fallback")
"value"

iex> e(%{a: %{b: %{c: "value"}}}, :a, :b, :c, :d, "fallback")
"fallback"

iex> e(%{a: %{b: %{c: %{d: "value"}}}}, :a, :b, :c, :d, "fallback")
"value"

iex> e(%{a: %{b: %{c: %{d: "value"}}}}, :a, :b, :c, :d, :ed, "fallback")
"fallback"

iex> e(%{a: %{b: %{c: %{d: %{e: "value"}}}}}, :a, :b, :c, :d, :e, "fallback")
"value"

e(object, key1, key2, fallback)

(macro)

e(object, key1, key2, key3, fallback)

(macro)

e(object, key1, key2, key3, key4, fallback)

(macro)

e(object, key1, key2, key3, key4, key5, fallback)

(macro)

e(object, key1, key2, key3, key4, key5, key6, fallback)

(macro)

ed(val, fallback \\ nil)

Returns a value if it is not empty, or a fallback value if it is empty.

This function delegates to Bonfire.Common.Enums.filter_empty/2 to determine if val is empty and returns fallback if so.

Examples

iex> ed("non-empty value", "fallback")
"non-empty value"

iex> ed("", "fallback")
"fallback"

iex> ed(nil, "fallback")
"fallback"

ed(map, key, fallback)

Extracts a value from a map or other data structure, or returns a fallback if not present or empty. If additional arguments are provided, it searches for nested data structures, with the last argument always being the fallback.

Examples

iex> ed(%{key: "value"}, :key, "fallback")
"value"

iex> ed(%{key: nil}, :key, "fallback")
"fallback"

iex> ed(%{key: "value"}, :missing_key, "fallback")
"fallback"

iex> ed(%{key: %Ecto.Association.NotLoaded{}}, :key, "fallback")
"fallback"

iex> ed({:ok, %{key: "value"}}, :key, "fallback")
"value"

iex> ed(%{__context__: %{key: "context_value"}}, :key, "fallback")
"context_value"

iex> ed(%{a: %{b: "value"}}, :a, :b, "fallback")
"value"

iex> ed(%{a: %{b: %Ecto.Association.NotLoaded{}}}, :a, :b, "fallback")
"fallback"

iex> ed(%{a: %{b: "value"}}, [:a, :b], "fallback")
"value"

iex> ed(%{a: %{b: nil}}, :a, :b, "fallback")
"fallback"

iex> ed(%{a: %{b: %{c: "value"}}}, :a, :b, :c, "fallback")
"value"

iex> ed(%{a: %{b: %{c: "value"}}}, :a, :b, :c, :d, "fallback")
"fallback"

iex> ed(%{a: %{b: %{c: %{d: "value"}}}}, :a, :b, :c, :d, "fallback")
"value"

iex> ed(%{a: %{b: %{c: %{d: "value"}}}}, :a, :b, :c, :d, :ed, "fallback")
"fallback"

iex> ed(%{a: %{b: %{c: %{d: %{e: "value"}}}}}, :a, :b, :c, :d, :e, "fallback")
"value"

ed(object, key1, key2, fallback)

Returns a value from a nested map, or a fallback if not present

ed(object, key1, key2, key3, fallback)

ed(object, key1, key2, key3, key4, fallback)

ed(object, key1, key2, key3, key4, key5, fallback)

ed(object, key1, key2, key3, key4, key5, key6, fallback)

pathex_fallback(arg1, keys, fallback)