Skip to content

Commit

Permalink
Improve doc
Browse files Browse the repository at this point in the history
  • Loading branch information
Serabe committed Aug 1, 2023
1 parent 9c70809 commit 0530142
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
37 changes: 36 additions & 1 deletion lib/dom_helpers/selectors.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ defmodule DomHelpers.Selectors do

@doc """
Complements the selector by checking that the attribute is present.
## Examples
```
iex> with_attr("input", "checked")
~s/input["checked"]/
```
"""
def with_attr(selector, attr), do: "#{selector}[\"#{attr}\"]"

Expand All @@ -25,6 +32,7 @@ defmodule DomHelpers.Selectors do
## Examples
```
iex> with_attr("input", "type", "text")
~s/input["type"="text"]/
Expand All @@ -42,7 +50,7 @@ defmodule DomHelpers.Selectors do
iex> with_attr("main", "lang", {:subcode, "es"})
~s/main["lang"|="es"]/
```
"""
def with_attr(selector, attr, value),
do: "#{selector}[\"#{attr}\"#{matcher(value)}\"#{get_attr_value(value)}\"]"
Expand All @@ -52,8 +60,10 @@ defmodule DomHelpers.Selectors do
## Examples
```
iex> without_attr("input", "checked")
~s/input:not(["checked"])/
```
"""
def without_attr(selector, attr), do: "#{selector}:not(#{with_attr("", attr)})"

Expand All @@ -70,11 +80,13 @@ defmodule DomHelpers.Selectors do
## Examples
```
iex> with_attrs("input", type: "checkbox", checked: true)
~s/input["type"="checkbox"]["checked"]/
iex> with_attrs("input", name: false, class: {:contains_word, "hola"}, "data-test": false)
~s/input["class"~="hola"]:not(["name"]):not(["data-test"])/
```
"""
def with_attrs(selector, attrs) when is_map(attrs) or is_list(attrs) do
{without_attr, with_attr} =
Expand Down Expand Up @@ -102,11 +114,13 @@ defmodule DomHelpers.Selectors do
## Examples
```
iex> with_class("div", "hidden")
"div.hidden"
iex> with_class("span", "flex")
"span.flex"
```
"""
def with_class(selector, class), do: "#{selector}.#{class}"

Expand All @@ -115,11 +129,13 @@ defmodule DomHelpers.Selectors do
## Examples
```
iex> with_classes("div", ~w(flex mb-4))
"div.flex.mb-4"
iex> with_classes("span", ~w(p-1 m-2))
"span.p-1.m-2"
```
"""
def with_classes(selector, classes) when is_list(classes) do
Enum.reduce(classes, selector, &with_class(&2, &1))
Expand All @@ -130,8 +146,10 @@ defmodule DomHelpers.Selectors do
## Examples
```
iex> without_class("div", "hidden")
~s/div:not(.hidden)/
```
"""
def without_class(selector, class), do: "#{selector}:not(.#{class})"

Expand All @@ -140,13 +158,30 @@ defmodule DomHelpers.Selectors do
## Examples
```
iex> without_classes("div", ~w(flex mb-4))
~s/div:not(.flex):not(.mb-4)/
```
"""
def without_classes(selector, classes) when is_list(classes) do
Enum.reduce(classes, selector, &without_class(&2, &1))
end

@doc """
Adds the given id to the selector passed in.
## Examples
```
iex> with_id("main", "main_content")
"main#main_content"
iex> with_id("div", "modal")
"div#modal"
```
"""
def with_id(selector, id), do: "#{selector}##{id}"

defp get_attr_value({_matcher, value}), do: value
defp get_attr_value(value), do: value

Expand Down
4 changes: 3 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
defmodule DomHelpers.MixProject do
use Mix.Project

@version "0.1.1"

def project do
[
app: :dom_helpers,
version: "0.1.0",
version: @version,
elixir: "~> 1.15",
start_permanent: Mix.env() == :prod,
elixirc_paths: elixirc_paths(Mix.env()),
Expand Down

0 comments on commit 0530142

Please sign in to comment.