Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions lib/ex_css_modules/ex_css_modules.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,26 @@ defmodule ExCSSModules do
end

@doc """
Returns the class name from the definition map is value is true.
If `value` is truthy, read the class definitions and maps them to a class attribute.
When `value` is falsy return nil.

## Examples
iex> class(%{ "hello" => "world"}, "hello", true)
{:safe, ~s(class="world")}

iex> class(%{ "hello" => "world"}, "hello", false)
nil
"""
def class(definition, classes, value) do
if value do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're reproducing existing functionality because class_name already checks the value for truthiness.

iex(1)> ExCSSModules.class_name(
...(1)> %{ "hello" => "world" }, "hello", false)
nil
iex(2)> ExCSSModules.class_name(%{ "hello" => "world" }, "hello", false)
nil
iex(3)> ExCSSModules.class_name(%{ "hello" => "world" }, "hello", true)
"world"
iex(4)> ExCSSModules.class_name(%{ "hello" => "world", "world" => "hello" }, ["world"])
"hello"
iex(5)> ExCSSModules.class_name(%{ "hello" => "world", "world" => "hello" }, [{"world", false}])
""
iex(6)> ExCSSModules.class_name(%{ "hello" => "world", "world" => "hello" }, [{"world", false}])

Looking at these results I would do the following:
Change the class_name() function that reacts to lists to return nil instead of an empty string when no results are present, ie:

iex(5)> ExCSSModules.class_name(%{ "hello" => "world", "world" => "hello" }, [{"world", false}])
nil

And change the class attribute to return an empty string if it receives nil:

iex> class_attribute(nil)
""

This way the class function behaves the exact same way as a class_name except that it pipes to the class_attribute() function.

definition
|> class_name(classes)
|> class_attribute
end
end

@doc """
Returns the class name from the definition map if value is true.

## Examples
iex> class_name(%{"hello" => "world"}, "hello", true)
Expand All @@ -63,7 +82,7 @@ defmodule ExCSSModules do
end

@doc """
Returns the class name from the definition map is the second argument
Returns the class name from the definition map if the second argument
in the tuple is true.

## Examples
Expand Down
1 change: 1 addition & 0 deletions lib/ex_css_modules/view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ defmodule ExCSSModules.View do
def stylesheet, do: ExCSSModules.stylesheet(@stylesheet)

def class(key), do: stylesheet() |> ExCSSModules.class(key)
def class(key, value), do: stylesheet() |> ExCSSModules.class(key, value)

def class_name(key) do
ExCSSModules.class_name(stylesheet(), key)
Expand Down
30 changes: 30 additions & 0 deletions test/ex_css_modules/ex_css_modules_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,36 @@ defmodule ExCSSModulesTest do
end
end

describe "class/2" do
test "returns a class attribute for an existing classname" do
assert ExCSSModules.class(
%{"hello" => "world"},
"hello"
) == {:safe, ~s(class="world")}
end

test "returns an empty class attribute for a non existing classname" do
assert ExCSSModules.class(
%{"hello" => "world"},
"foo"
) == {:safe, ~s(class="")}
end
end

describe "class/3" do
test "returns a class attribute for an existing classname when value is true" do
assert ExCSSModules.class(
%{"hello" => "world"},
"hello",
true
) == {:safe, ~s(class="world")}
end

test "returns nil for an existing classname when value is false" do
assert ExCSSModules.class(%{"hello" => "world"}, "hello", false) == nil
end
end

describe "class_name/3" do
test "returns the definition when value is true" do
assert ExCSSModules.class_name(
Expand Down
11 changes: 10 additions & 1 deletion test/ex_css_modules/view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,18 @@ defmodule ExCSSModules.ViewTest do
end

describe "class/1" do
test "creates a safe Phoenix HTML class based on the stylesheet" do
test "calls the class/2 method on ExCSSModules" do
assert ViewModuleTest.class("title") ==
ExCSSModules.class(@example_stylesheet, "title")
end
end

describe "class/2" do
test "calls the class/3 method on ExCSSModules" do
assert ViewModuleTest.class("title", true) ==
ExCSSModules.class(@example_stylesheet, "title", true)
assert ViewModuleTest.class("title", false) ==
ExCSSModules.class(@example_stylesheet, "title", false)
end
end
end