mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-29 07:05:12 -07:00
document math filters
This commit is contained in:
@@ -417,40 +417,79 @@ module Liquid
|
|||||||
array.last if array.respond_to?(:last)
|
array.last if array.respond_to?(:last)
|
||||||
end
|
end
|
||||||
|
|
||||||
# absolute value
|
# @public_docs
|
||||||
|
# @syntax {{ number | abs }}
|
||||||
|
# @summary Returns the absolute value of a number.
|
||||||
|
# @type filter
|
||||||
|
# @category Math
|
||||||
|
# @return number
|
||||||
|
# @description
|
||||||
|
# `abs` will also work on a string if the string only contains a number.
|
||||||
def abs(input)
|
def abs(input)
|
||||||
result = Utils.to_number(input).abs
|
result = Utils.to_number(input).abs
|
||||||
result.is_a?(BigDecimal) ? result.to_f : result
|
result.is_a?(BigDecimal) ? result.to_f : result
|
||||||
end
|
end
|
||||||
|
|
||||||
# addition
|
# @public_docs
|
||||||
|
# @syntax {{ number | plus: number }}
|
||||||
|
# @summary Adds a number to an output.
|
||||||
|
# @type filter
|
||||||
|
# @category Math
|
||||||
|
# @return number
|
||||||
def plus(input, operand)
|
def plus(input, operand)
|
||||||
apply_operation(input, operand, :+)
|
apply_operation(input, operand, :+)
|
||||||
end
|
end
|
||||||
|
|
||||||
# subtraction
|
# @public_docs
|
||||||
|
# @syntax {{ number | minus: number }}
|
||||||
|
# @summary Subtracts a number from an output.
|
||||||
|
# @type filter
|
||||||
|
# @category Math
|
||||||
|
# @return number
|
||||||
def minus(input, operand)
|
def minus(input, operand)
|
||||||
apply_operation(input, operand, :-)
|
apply_operation(input, operand, :-)
|
||||||
end
|
end
|
||||||
|
|
||||||
# multiplication
|
# @public_docs
|
||||||
|
# @syntax {{ number | times: number }}
|
||||||
|
# @summary Multiplies an output by a number.
|
||||||
|
# @type filter
|
||||||
|
# @category Math
|
||||||
|
# @return number
|
||||||
def times(input, operand)
|
def times(input, operand)
|
||||||
apply_operation(input, operand, :*)
|
apply_operation(input, operand, :*)
|
||||||
end
|
end
|
||||||
|
|
||||||
# division
|
# @public_docs
|
||||||
|
# @syntax {{ number | divided_by: number }}
|
||||||
|
# @summary Divides an output by a number. The output is rounded down to the nearest integer.
|
||||||
|
# @type filter
|
||||||
|
# @category Math
|
||||||
|
# @return number
|
||||||
def divided_by(input, operand)
|
def divided_by(input, operand)
|
||||||
apply_operation(input, operand, :/)
|
apply_operation(input, operand, :/)
|
||||||
rescue ::ZeroDivisionError => e
|
rescue ::ZeroDivisionError => e
|
||||||
raise Liquid::ZeroDivisionError, e.message
|
raise Liquid::ZeroDivisionError, e.message
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @public_docs
|
||||||
|
# @syntax {{ number | modulo: number }}
|
||||||
|
# @summary Divides an output by a number and returns the remainder.
|
||||||
|
# @type filter
|
||||||
|
# @category Math
|
||||||
|
# @return number
|
||||||
def modulo(input, operand)
|
def modulo(input, operand)
|
||||||
apply_operation(input, operand, :%)
|
apply_operation(input, operand, :%)
|
||||||
rescue ::ZeroDivisionError => e
|
rescue ::ZeroDivisionError => e
|
||||||
raise Liquid::ZeroDivisionError, e.message
|
raise Liquid::ZeroDivisionError, e.message
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @public_docs
|
||||||
|
# @syntax {{ number | round }}
|
||||||
|
# @summary Rounds the output to the nearest integer or specified number of decimals.
|
||||||
|
# @type filter
|
||||||
|
# @category Math
|
||||||
|
# @return number
|
||||||
def round(input, n = 0)
|
def round(input, n = 0)
|
||||||
result = Utils.to_number(input).round(Utils.to_number(n))
|
result = Utils.to_number(input).round(Utils.to_number(n))
|
||||||
result = result.to_f if result.is_a?(BigDecimal)
|
result = result.to_f if result.is_a?(BigDecimal)
|
||||||
@@ -460,12 +499,24 @@ module Liquid
|
|||||||
raise Liquid::FloatDomainError, e.message
|
raise Liquid::FloatDomainError, e.message
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @public_docs
|
||||||
|
# @syntax {{ number | ceil }}
|
||||||
|
# @summary Rounds an output up to the nearest integer.
|
||||||
|
# @type filter
|
||||||
|
# @category Math
|
||||||
|
# @return number
|
||||||
def ceil(input)
|
def ceil(input)
|
||||||
Utils.to_number(input).ceil.to_i
|
Utils.to_number(input).ceil.to_i
|
||||||
rescue ::FloatDomainError => e
|
rescue ::FloatDomainError => e
|
||||||
raise Liquid::FloatDomainError, e.message
|
raise Liquid::FloatDomainError, e.message
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @public_docs
|
||||||
|
# @syntax {{ number | floor }}
|
||||||
|
# @summary Rounds an output down to the nearest integer.
|
||||||
|
# @type filter
|
||||||
|
# @category Math
|
||||||
|
# @return number
|
||||||
def floor(input)
|
def floor(input)
|
||||||
Utils.to_number(input).floor.to_i
|
Utils.to_number(input).floor.to_i
|
||||||
rescue ::FloatDomainError => e
|
rescue ::FloatDomainError => e
|
||||||
|
|||||||
Reference in New Issue
Block a user