# `Carvex.Attributes`
[🔗](https://github.com/paradox460/carvex/blob/v0.0.1/lib/carvex/attributes.ex#L1)

A node's attributes, as carve's AST carries them.

  * `id` - the `#id`, or `nil`
  * `classes` - the `.class` names, in order
  * `key_values` - other `key=value` pairs (string keys)
  * `order` - the written order of the slots, as `"#id"`, `".class"` or a
    key name; `[]` when not recorded. Used by `Carvex.to_carve/2`.

Every function accepts `nil` as an empty set and returns a struct.

# `t`

```elixir
@type t() :: %Carvex.Attributes{
  classes: [String.t()],
  id: String.t() | nil,
  key_values: %{required(String.t()) =&gt; String.t()},
  order: [String.t()]
}
```

# `add_class`

```elixir
@spec add_class(t() | nil, String.t()) :: t()
```

Appends a class.

    iex> Carvex.Attributes.add_class(%Carvex.Attributes{classes: ["a"]}, "b")
    %Carvex.Attributes{classes: ["a", "b"]}

# `delete`

```elixir
@spec delete(t() | nil, String.t()) :: t()
```

Removes `key`.

    iex> Carvex.Attributes.delete(%Carvex.Attributes{id: "x", classes: ["a"], order: ["#id", ".class"]}, "class")
    %Carvex.Attributes{id: "x", order: ["#id"]}

# `empty?`

```elixir
@spec empty?(t() | nil) :: boolean()
```

True for `nil` or an attribute set with nothing in it.

    iex> Carvex.Attributes.empty?(nil)
    true
    iex> Carvex.Attributes.empty?(%Carvex.Attributes{id: "x"})
    false

# `get`

```elixir
@spec get(t() | nil, String.t()) :: String.t() | nil
```

Returns the value for `key`: the id for `"id"`, the classes joined with a
space for `"class"`, otherwise the key/value entry. `nil` when absent.

    iex> attrs = %Carvex.Attributes{id: "x", classes: ["a", "b"], key_values: %{"k" => "v"}}
    iex> Carvex.Attributes.get(attrs, "class")
    "a b"
    iex> Carvex.Attributes.get(attrs, "id")
    "x"
    iex> Carvex.Attributes.get(attrs, "k")
    "v"
    iex> Carvex.Attributes.get(nil, "class")
    nil

# `put`

```elixir
@spec put(t() | nil, String.t(), String.t()) :: t()
```

Sets `key`. `"class"` replaces the classes with the whitespace-split value.

    iex> Carvex.Attributes.put(nil, "class", "a b")
    %Carvex.Attributes{classes: ["a", "b"]}
    iex> Carvex.Attributes.put(%Carvex.Attributes{id: "x", order: ["#id"]}, "k", "v")
    %Carvex.Attributes{id: "x", key_values: %{"k" => "v"}, order: ["#id", "k"]}

---

*Consult [api-reference.md](api-reference.md) for complete listing*
