mirror of
https://github.com/Shopify/liquid.git
synced 2026-10-01 16:15:14 -07:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7740adb36f |
@@ -298,12 +298,7 @@ module Liquid
|
||||
end
|
||||
end
|
||||
|
||||
# @public_docs
|
||||
# @syntax {{ string | replace: string, substring }}
|
||||
# @summary Replaces all occurrences of a string with a substring.
|
||||
# @type filter
|
||||
# @category string
|
||||
# @return string
|
||||
# Replace occurrences of a string with another
|
||||
def replace(input, string, replacement = '')
|
||||
input.to_s.gsub(string.to_s, replacement.to_s)
|
||||
end
|
||||
@@ -328,22 +323,12 @@ module Liquid
|
||||
output
|
||||
end
|
||||
|
||||
# @public_docs
|
||||
# @syntax {{ string | remove: string }}
|
||||
# @summary Removes a substring.
|
||||
# @type filter
|
||||
# @category string
|
||||
# @return string
|
||||
# remove a substring
|
||||
def remove(input, string)
|
||||
replace(input, string, '')
|
||||
end
|
||||
|
||||
# @public_docs
|
||||
# @syntax {{ string | remove_first: string }}
|
||||
# @summary Removes the first occurrences of a substring.
|
||||
# @type filter
|
||||
# @category string
|
||||
# @return string
|
||||
# remove the first occurrences of a substring
|
||||
def remove_first(input, string)
|
||||
replace_first(input, string, '')
|
||||
end
|
||||
@@ -365,12 +350,7 @@ module Liquid
|
||||
InputIterator.new(input, context).concat(array)
|
||||
end
|
||||
|
||||
# @public_docs
|
||||
# @syntax {{ string | prepend: string }}
|
||||
# @summary Prepend a string to another string.
|
||||
# @type filter
|
||||
# @category string
|
||||
# @return string
|
||||
# prepend a string to another
|
||||
def prepend(input, string)
|
||||
string.to_s + input.to_s
|
||||
end
|
||||
@@ -437,40 +417,79 @@ module Liquid
|
||||
array.last if array.respond_to?(:last)
|
||||
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)
|
||||
result = Utils.to_number(input).abs
|
||||
result.is_a?(BigDecimal) ? result.to_f : result
|
||||
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)
|
||||
apply_operation(input, operand, :+)
|
||||
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)
|
||||
apply_operation(input, operand, :-)
|
||||
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)
|
||||
apply_operation(input, operand, :*)
|
||||
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)
|
||||
apply_operation(input, operand, :/)
|
||||
rescue ::ZeroDivisionError => e
|
||||
raise Liquid::ZeroDivisionError, e.message
|
||||
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)
|
||||
apply_operation(input, operand, :%)
|
||||
rescue ::ZeroDivisionError => e
|
||||
raise Liquid::ZeroDivisionError, e.message
|
||||
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)
|
||||
result = Utils.to_number(input).round(Utils.to_number(n))
|
||||
result = result.to_f if result.is_a?(BigDecimal)
|
||||
@@ -480,12 +499,24 @@ module Liquid
|
||||
raise Liquid::FloatDomainError, e.message
|
||||
end
|
||||
|
||||
# @public_docs
|
||||
# @syntax {{ number | ceil }}
|
||||
# @summary Rounds an output up to the nearest integer.
|
||||
# @type filter
|
||||
# @category Math
|
||||
# @return number
|
||||
def ceil(input)
|
||||
Utils.to_number(input).ceil.to_i
|
||||
rescue ::FloatDomainError => e
|
||||
raise Liquid::FloatDomainError, e.message
|
||||
end
|
||||
|
||||
# @public_docs
|
||||
# @syntax {{ number | floor }}
|
||||
# @summary Rounds an output down to the nearest integer.
|
||||
# @type filter
|
||||
# @category Math
|
||||
# @return number
|
||||
def floor(input)
|
||||
Utils.to_number(input).floor.to_i
|
||||
rescue ::FloatDomainError => e
|
||||
|
||||
Reference in New Issue
Block a user