Current section

Files

Jump to
simple_form lib simple_form attrs.ex
Raw

lib/simple_form/attrs.ex

defmodule SimpleForm.Attrs do
@moduledoc """
`SimpleForm.Attrs` implements functions for manipulating SimpleForm attributes,
such as getting a specific attribute from a Simpleform element or list of SimpleForm attributes.
"""
@doc """
Get the key of an attribute.
## Examples
iex> SimpleForm.Attrs.key({"a", 123})
"a"
iex> SimpleForm.Attrs.key(nil)
nil
"""
@spec key(attribute :: nil) :: nil
@spec key(attribute :: SimpleForm.attribute()) :: String.t()
def key(nil), do: nil
def key({key, _}), do: key
@doc """
Get the value of an attribute.
## Examples
iex> SimpleForm.Attrs.value({"a", 123})
123
iex> SimpleForm.Attrs.value(nil)
nil
"""
@spec value(attribute :: nil) :: nil
@spec value(attribute :: SimpleForm.attribute()) :: String.t()
def value(nil), do: nil
def value({_, value}), do: value
@doc """
Get an attribute from a SimpleForm element or its list of attributes.
## Examples
iex> SimpleForm.Attrs.get({"w:p", [{"a", 123}, {"b", 6}], []}, "a")
{"a", 123}
iex> SimpleForm.Attrs.get([{"a", 123}, {"b", 6}], "b")
{"b", 6}
iex> SimpleForm.Attrs.get([{"a", 123}, {"b", 6}], "c")
nil
iex> SimpleForm.Attrs.get([{"a", 123}, {"b", 6}], "c", "default")
"default"
iex> SimpleForm.Attrs.get(nil, "a")
nil
iex> SimpleForm.Attrs.get(nil, "a", "default")
"default"
"""
@spec get(nil, String.t(), default) :: default when default: var
@spec get(
element :: SimpleForm.element(),
key :: String.t(),
default :: String.t() | default
) :: SimpleForm.attribute() | default
when default: var
@spec get(
attrs :: [SimpleForm.attribute()],
key :: String.t(),
default :: String.t() | default
) :: SimpleForm.attribute() | default
when default: var
def get(element_or_attrs, key, default \\ nil)
def get(nil, _, default), do: default
def get({_, attrs, _}, key, default) when is_list(attrs), do: get(attrs, key, default)
def get(attrs, key, default) when is_list(attrs),
do: List.keyfind(attrs, key, 0, default)
@doc """
Get the value of an attribute from a SimpleForm element or its list of attributes.
## Examples
iex> SimpleForm.Attrs.get_value({"w:p", [{"a", 123}, {"b", 6}], []}, "a")
123
iex> SimpleForm.Attrs.get_value([{"a", 123}, {"b", 6}], "b")
6
iex> SimpleForm.Attrs.get_value([{"a", 123}, {"b", 6}], "c")
nil
iex> SimpleForm.Attrs.get_value([{"a", 123}, {"b", 6}], "c", "default")
"default"
iex> SimpleForm.Attrs.get_value(nil, "a")
nil
iex> SimpleForm.Attrs.get_value(nil, "a", "default")
"default"
"""
@spec get_value(nil, String.t(), default) :: default when default: var
@spec get_value(
element :: SimpleForm.element(),
key :: String.t(),
default :: default
) :: String.t() | default
when default: var
@spec get_value(
attrs :: [SimpleForm.attribute()],
key :: String.t(),
default :: String.t() | default
) :: String.t() | default
when default: var
def get_value(element_or_attrs, key, default \\ nil)
def get_value(nil, _, default), do: default
def get_value(element_or_attrs, key, default),
do: get(element_or_attrs, key) |> value() || default
@doc """
Get multiple attributes from a SimpleForm element or its list of attributes.
The order and placement of the attributes in the list is preserved w.r.t. the given keys.
This function is more performant than calling `get/3` multiple times,
as it only iterates over `elements` once.
## Examples
iex> SimpleForm.Attrs.get_many({"w:p", [{"a", 123}, {"b", 6}, {"c", 42}], []}, ["a", "b", "c"])
[{"a", 123}, {"b", 6}, {"c", 42}]
iex> SimpleForm.Attrs.get_many([{"a", 123}, {"b", 6}, {"c", 42}], ["a", "c"])
[{"a", 123}, {"c", 42}]
iex> SimpleForm.Attrs.get_many([{"a", 123}, {"b", 6}, {"c", 42}], ["a", "d", "c"])
[{"a", 123}, nil, {"c", 42}]
iex> SimpleForm.Attrs.get_many([{"a", 123}, {"b", 6}, {"c", 42}], ["d", "e"])
[nil, nil]
"""
def get_many({_, attrs, _}, keys) when is_list(attrs), do: get_many(attrs, keys)
def get_many(attrs, keys) when is_list(attrs) do
attrs_map = attrs |> Map.new(fn {key, value} -> {key, {key, value}} end)
keys |> Enum.map(&Map.get(attrs_map, &1))
end
@doc """
Get multiple attribute values from a SimpleForm element or its list of attributes.
The order and placement of the attribute value in the list is preserved w.r.t. the given keys.
This function is more performant than calling `get_value/3` multiple times,
as it only iterates over `elements` once.
## Examples
iex> SimpleForm.Attrs.get_many_value({"w:p", [{"a", 123}, {"b", 6}, {"c", 42}], []}, ["a", "b", "c"])
[123, 6, 42]
iex> SimpleForm.Attrs.get_many_value([{"a", 123}, {"b", 6}, {"c", 42}], ["a", "c"])
[123, 42]
iex> SimpleForm.Attrs.get_many_value([{"a", 123}, {"b", 6}, {"c", 42}], ["a", "d", "c"])
[123, nil, 42]
iex> SimpleForm.Attrs.get_many_value([{"a", 123}, {"b", 6}, {"c", 42}], ["d", "e"])
[nil, nil]
"""
def get_many_value({_, attrs, _}, keys) when is_list(attrs), do: get_many_value(attrs, keys)
def get_many_value(attrs, keys) when is_list(attrs),
do: attrs |> get_many(keys) |> Enum.map(&value/1)
end