SimpleForm.Attrs (SimpleForm v1.0.9)

View Source

SimpleForm.Attrs implements functions for manipulating SimpleForm attributes, such as getting a specific attribute from a Simpleform element or list of SimpleForm attributes.

Summary

Functions

Get an attribute from a SimpleForm element or its list of attributes.

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.

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.

Get the value of an attribute from a SimpleForm element or its list of attributes.

Get the key of an attribute.

Get the value of an attribute.

Functions

get(element_or_attrs, key, default \\ nil)

@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

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"

get_many(attrs, keys)

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]

get_many_value(attrs, keys)

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]

get_value(element_or_attrs, key, default \\ nil)

@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

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"

key(arg1)

@spec key(attribute :: nil) :: nil
@spec key(attribute :: SimpleForm.attribute()) :: String.t()

Get the key of an attribute.

Examples

iex> SimpleForm.Attrs.key({"a", 123})
"a"
iex> SimpleForm.Attrs.key(nil)
nil

value(arg1)

@spec value(attribute :: nil) :: nil
@spec value(attribute :: SimpleForm.attribute()) :: String.t()

Get the value of an attribute.

Examples

iex> SimpleForm.Attrs.value({"a", 123})
123
iex> SimpleForm.Attrs.value(nil)
nil