Skip to content

Commit 6b39870

Browse files
committed
Combine class/3 and class_name/3 with third argument with default value
1 parent d71c9a7 commit 6b39870

File tree

1 file changed

+47
-63
lines changed

1 file changed

+47
-63
lines changed

lib/ex_css_modules/ex_css_modules.ex

Lines changed: 47 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -48,88 +48,45 @@ defmodule ExCSSModules do
4848
attribute. The `keys` argument is used to retrieve the class name or multiple
4949
class names with class_name/1.
5050
51+
Returns nil if `return_class?` is falsy. `return_class?` is optional and
52+
truthy by default.
53+
5154
Returns nil for a class_name that does not exist.
5255
5356
## Examples
5457
5558
iex> class(%{ "hello" => "world"}, "hello")
5659
{:safe, ~s(class="world")}
5760
58-
iex> class(%{"hello" => "world"}, "foo")
59-
nil
60-
61-
"""
62-
def class(definition, keys) do
63-
definition
64-
|> class_name(keys)
65-
|> class_attribute()
66-
end
67-
68-
@doc """
69-
If `return_class?` is truthy, reads the class definitions and maps them to a
70-
class attribute. When `return_class?` is falsy returns nil.
71-
72-
## Examples
73-
74-
iex> class(%{ "hello" => "world"}, "hello", true)
61+
iex> class(%{ "hello" => "world"}, :hello, true)
7562
{:safe, ~s(class="world")}
7663
7764
iex> class(%{ "hello" => "world"}, "hello", false)
7865
nil
7966
67+
iex> class(%{"hello" => "world"}, "foo")
68+
nil
69+
8070
"""
81-
def class(definition, keys, return_class?) do
71+
def class(definition, keys, return_class? \\ true) do
8272
definition
8373
|> class_name(keys, return_class?)
8474
|> class_attribute()
8575
end
8676

87-
@doc """
88-
Returns the class name from the definition map if the last argument is truthy.
89-
Returns nil if the last argument is falsy.
90-
91-
## Examples
92-
93-
iex> class_name(%{"hello" => "world"}, "hello", true)
94-
"world"
95-
96-
iex> class_name(%{"hello" => "world"}, "hello", "anything")
97-
"world"
98-
99-
iex> class_name(%{"hello" => "world"}, :hello, true)
100-
"world"
101-
102-
iex> class_name(%{"hello" => "world"}, "hello", false)
103-
nil
104-
105-
iex> class_name(%{"hello" => "world"}, "hello", nil)
106-
nil
107-
108-
"""
109-
def class_name(_, _, false), do: nil
110-
def class_name(_, _, nil), do: nil
111-
def class_name(definition, key, _), do: class_name(definition, key)
112-
11377
@doc """
11478
Returns the class name or class names from the definition map, concatenated as
11579
one string separated by spaces.
11680
117-
Second argument can be a string or atom name of the key, a tuple with
118-
`{key, boolean}` or a list of keys or tuples.
119-
120-
## Examples
81+
Second argument `key` can be a string or atom name of the key, a tuple with
82+
`{key, boolean}` or a list of either keys or tuples.
12183
122-
iex> class_name(%{"hello" => "world"}, "hello")
123-
"world"
84+
Returns nil if `return_class?` is falsy. `return_class?` is optional and
85+
truthy by default.
12486
125-
iex> class_name(%{"hello" => "world"}, "foo")
126-
nil
127-
128-
iex> class_name(%{"hello" => "world"}, {"hello", true})
129-
"world"
87+
Returns nil for a key that does not exist.
13088
131-
iex> class_name(%{"hello" => "world"}, {"hello", false})
132-
nil
89+
## Examples
13390
13491
iex> class_name(%{"hello" => "world", "foo" => "bar"}, ["hello", "foo"])
13592
"world bar"
@@ -143,26 +100,53 @@ defmodule ExCSSModules do
143100
iex> class_name(%{"hello" => "world", "foo" => "bar"}, [{:hello, true}, {:foo, false}])
144101
"world"
145102
146-
iex> class_name(%{"hello" => "world", "foo" => "bar"}, [{"hello", false}])
103+
iex> class_name(%{"hello" => "world"}, "hello")
104+
"world"
105+
106+
iex> class_name(%{"hello" => "world"}, :hello, true)
107+
"world"
108+
109+
iex> class_name(%{"hello" => "world"}, "hello", "anything")
110+
"world"
111+
112+
iex> class_name(%{"hello" => "world"}, {"hello", true})
113+
"world"
114+
115+
iex> class_name(%{"hello" => "world"}, "hello", false)
116+
nil
117+
118+
iex> class_name(%{"hello" => "world"}, {:hello, nil})
119+
nil
120+
121+
iex> class_name(%{"hello" => "world"}, "foo")
147122
nil
148123
149124
iex> class_name(%{}, "hello")
150125
nil
151126
152127
"""
153-
def class_name(definition, keys) when is_list(keys) do
128+
def class_name(definition, key, return_class? \\ true)
129+
130+
def class_name(_, _, false), do: nil
131+
def class_name(_, _, nil), do: nil
132+
133+
def class_name(definition, keys, _) when is_list(keys) do
154134
keys
155135
|> Enum.map(&class_name(definition, &1))
156136
|> Enum.reject(&is_nil/1)
157137
|> join_class_name()
158138
end
159139

160-
def class_name(definition, {key, return_class?}), do: class_name(definition, key, return_class?)
140+
def class_name(definition, {key, return_class?}, _),
141+
do: class_name(definition, key, return_class?)
161142

162-
def class_name(definition, key) when is_atom(key),
163-
do: key |> Atom.to_string() |> (&class_name(definition, &1)).()
143+
def class_name(definition, key, _) when is_atom(key) do
144+
key
145+
|> Atom.to_string()
146+
|> (&class_name(definition, &1)).()
147+
end
164148

165-
def class_name(definition, key) do
149+
def class_name(definition, key, _) do
166150
definition
167151
|> stylesheet()
168152
|> Map.get(key, nil)

0 commit comments

Comments
 (0)