-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Add max and min filters #954
Conversation
lib/liquid/standardfilters.rb
Outdated
@@ -355,6 +355,16 @@ def floor(input) | |||
raise Liquid::FloatDomainError, e.message | |||
end | |||
|
|||
def max(input, n) | |||
result = [input, n].map { |number| Utils.to_number(number) }.min |
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.
Allocating an array just for this is not great, we're not running on something fancy like TruffleRuby that can optimize that allocation away.
14c3043
to
c89d724
Compare
@pushrax Updated the methods to use conditions instead of allocating arrays, and added additional tests for number-like things. |
|
||
assert_template_result "4.5", "{{ 4.5 | max:5 }}" | ||
assert_template_result "5", "{{ width | max:5 }}", 'width' => NumberLikeThing.new(6) | ||
assert_template_result "4", "{{ width | max:5 }}", 'width' => NumberLikeThing.new(4) |
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.
Could add a test for {{ 5 | max: width }}
c89d724
to
4bdaaf0
Compare
Added tests for |
This will introduce
max
andmin
filters. An example use case:The above can be replaced with:
If invalid inputs are given (eg.
"foo"
), they would default to 0 (eg.{{ "foo" | max: 5 }
returns0
, and{{ "foo" | min:5 }}
returns5
), which is consistent with other filters that work on numbers (ceil, floor etc), but I haven't added test cases for those.@Thibaut