mirror of
https://github.com/Shopify/liquid.git
synced 2026-10-02 16:45:12 -07:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7740adb36f |
@@ -298,12 +298,7 @@ module Liquid
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# @public_docs
|
# Replace occurrences of a string with another
|
||||||
# @syntax {{ string | replace: string, substring }}
|
|
||||||
# @summary Replaces all occurrences of a string with a substring.
|
|
||||||
# @type filter
|
|
||||||
# @category string
|
|
||||||
# @return string
|
|
||||||
def replace(input, string, replacement = '')
|
def replace(input, string, replacement = '')
|
||||||
input.to_s.gsub(string.to_s, replacement.to_s)
|
input.to_s.gsub(string.to_s, replacement.to_s)
|
||||||
end
|
end
|
||||||
@@ -328,22 +323,12 @@ module Liquid
|
|||||||
output
|
output
|
||||||
end
|
end
|
||||||
|
|
||||||
# @public_docs
|
# remove a substring
|
||||||
# @syntax {{ string | remove: string }}
|
|
||||||
# @summary Removes a substring.
|
|
||||||
# @type filter
|
|
||||||
# @category string
|
|
||||||
# @return string
|
|
||||||
def remove(input, string)
|
def remove(input, string)
|
||||||
replace(input, string, '')
|
replace(input, string, '')
|
||||||
end
|
end
|
||||||
|
|
||||||
# @public_docs
|
# remove the first occurrences of a substring
|
||||||
# @syntax {{ string | remove_first: string }}
|
|
||||||
# @summary Removes the first occurrences of a substring.
|
|
||||||
# @type filter
|
|
||||||
# @category string
|
|
||||||
# @return string
|
|
||||||
def remove_first(input, string)
|
def remove_first(input, string)
|
||||||
replace_first(input, string, '')
|
replace_first(input, string, '')
|
||||||
end
|
end
|
||||||
@@ -365,12 +350,7 @@ module Liquid
|
|||||||
InputIterator.new(input, context).concat(array)
|
InputIterator.new(input, context).concat(array)
|
||||||
end
|
end
|
||||||
|
|
||||||
# @public_docs
|
# prepend a string to another
|
||||||
# @syntax {{ string | prepend: string }}
|
|
||||||
# @summary Prepend a string to another string.
|
|
||||||
# @type filter
|
|
||||||
# @category string
|
|
||||||
# @return string
|
|
||||||
def prepend(input, string)
|
def prepend(input, string)
|
||||||
string.to_s + input.to_s
|
string.to_s + input.to_s
|
||||||
end
|
end
|
||||||
@@ -437,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)
|
||||||
@@ -480,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