mirror of
https://github.com/Shopify/liquid.git
synced 2026-10-03 09:05:13 -07:00
Merge pull request #368 from Shopify/add-round-ceil-and-floor
[Liquid] Add round, ceil and floor standard filters
This commit is contained in:
@@ -243,6 +243,21 @@ module Liquid
|
|||||||
apply_operation(input, operand, :%)
|
apply_operation(input, operand, :%)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def round(input, n = 0)
|
||||||
|
result = to_number(input).round(to_number(n))
|
||||||
|
result = result.to_f if result.is_a?(BigDecimal)
|
||||||
|
result = result.to_i if n == 0
|
||||||
|
result
|
||||||
|
end
|
||||||
|
|
||||||
|
def ceil(input)
|
||||||
|
to_number(input).ceil.to_i
|
||||||
|
end
|
||||||
|
|
||||||
|
def floor(input)
|
||||||
|
to_number(input).floor.to_i
|
||||||
|
end
|
||||||
|
|
||||||
def default(input, default_value = "".freeze)
|
def default(input, default_value = "".freeze)
|
||||||
is_blank = input.respond_to?(:empty?) ? input.empty? : !input
|
is_blank = input.respond_to?(:empty?) ? input.empty? : !input
|
||||||
is_blank ? default_value : input
|
is_blank ? default_value : input
|
||||||
|
|||||||
@@ -273,6 +273,22 @@ class StandardFiltersTest < Test::Unit::TestCase
|
|||||||
assert_template_result "1", "{{ 3 | modulo:2 }}"
|
assert_template_result "1", "{{ 3 | modulo:2 }}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_round
|
||||||
|
assert_template_result "5", "{{ input | round }}", 'input' => 4.6
|
||||||
|
assert_template_result "4", "{{ '4.3' | round }}"
|
||||||
|
assert_template_result "4.56", "{{ input | round: 2 }}", 'input' => 4.5612
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_ceil
|
||||||
|
assert_template_result "5", "{{ input | ceil }}", 'input' => 4.6
|
||||||
|
assert_template_result "5", "{{ '4.3' | ceil }}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_floor
|
||||||
|
assert_template_result "4", "{{ input | floor }}", 'input' => 4.6
|
||||||
|
assert_template_result "4", "{{ '4.3' | floor }}"
|
||||||
|
end
|
||||||
|
|
||||||
def test_append
|
def test_append
|
||||||
assigns = {'a' => 'bc', 'b' => 'd' }
|
assigns = {'a' => 'bc', 'b' => 'd' }
|
||||||
assert_template_result('bcd',"{{ a | append: 'd'}}",assigns)
|
assert_template_result('bcd',"{{ a | append: 'd'}}",assigns)
|
||||||
|
|||||||
Reference in New Issue
Block a user