View Source Bonfire.Common.E (Bonfire v0.9.10-classic-beta.156)

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

Link to this macro

e(object, key1, fallback)

View Source (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"
Link to this macro

e(object, key1, key2, fallback)

View Source (macro)
Link to this macro

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

View Source (macro)
Link to this macro

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

View Source (macro)
Link to this macro

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

View Source (macro)
Link to this macro

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

View Source (macro)
Link to this function

ed(val, fallback \\ nil)

View Source

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"

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"
Link to this function

ed(object, key1, key2, fallback)

View Source

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

Link to this function

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

View Source
Link to this function

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

View Source
Link to this function

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

View Source
Link to this function

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

View Source