-
Notifications
You must be signed in to change notification settings - Fork 1
Add class/2 for conditional checking #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Codecov Report
@@ Coverage Diff @@
## master #10 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 2 2
Lines 25 27 +2
=====================================
+ Hits 25 27 +2
Continue to review full report at Codecov.
|
d1909d0
to
857ce69
Compare
857ce69
to
1f7e241
Compare
lib/ex_css_modules/ex_css_modules.ex
Outdated
nil | ||
""" | ||
def class(definition, classes, value) do | ||
if value do |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The class
function duplicates behaviour which can create unwanted results if we ever change any of the behaviours.
) == "world" | ||
end | ||
|
||
test "returns nil ...." do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this appended with ....
?
Should probably read returns nil if none of the conditions are met
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
woops
lib/ex_css_modules/ex_css_modules.ex
Outdated
|> Enum.map(&class_name(definition, &1)) | ||
|> Enum.reject(&is_nil/1) | ||
|
||
if Enum.any?(list) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer to away from if statements as much as possible, ie:
defp join_class_name(list) when length(list) == 0, do: nil
defp join_class_name(list), do: Enum.join(list, " ")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This way we can just keep piping the values as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kinda had the same thought, do you really think this is more clear?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know yet ;)
It is quite clear however when you read the join_class_name
function what it does, gets rid of an if statement and allows you to keep piping the value to the next function which I like a lot.
definition | ||
|> class_name(classes) | ||
|> class_attribute | ||
|> class_attribute() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Closes #9