mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
Change behaviour of arithmetic filters to cast arguments to numbers
This commit is contained in:
@@ -185,22 +185,35 @@ module Liquid
|
||||
|
||||
# addition
|
||||
def plus(input, operand)
|
||||
input + operand if input.respond_to?('+')
|
||||
to_number(input) + to_number(operand)
|
||||
end
|
||||
|
||||
# subtraction
|
||||
def minus(input, operand)
|
||||
input - operand if input.respond_to?('-')
|
||||
to_number(input) - to_number(operand)
|
||||
end
|
||||
|
||||
# multiplication
|
||||
def times(input, operand)
|
||||
input * operand if input.respond_to?('*')
|
||||
to_number(input) * to_number(operand)
|
||||
end
|
||||
|
||||
# division
|
||||
def divided_by(input, operand)
|
||||
input / operand if input.respond_to?('/')
|
||||
to_number(input) / to_number(operand)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def to_number(obj)
|
||||
case obj
|
||||
when Numeric
|
||||
obj
|
||||
when String
|
||||
(obj.strip =~ /^\d+\.\d+$/) ? obj.to_f : obj.to_i
|
||||
else
|
||||
0
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user