Compare commits

..
Author SHA1 Message Date
Melanie Wang 2bcecf0556 Document math filters 2022-03-14 13:51:07 -04:00
+14 -91
View File
@@ -95,32 +95,7 @@ module Liquid
end
end
# @public_docs
# @type filter
# @category string
# @summary
# Truncates a string down to a specified number of characters.
# @description
# Truncates a string down to a specified number of characters. By default, an ellipsis (`...`)
# is appended to the truncated string.
#
# > Tip:
# > The number of characters in both default and custom ellipses is included in the
# > character count for the truncated string. For example, if you want to truncate a string
# > down to ten characters and use the default ellipsis (`...`), then you should set the
# > character count parameter to `13`.
#
# ### Custom ellipsis
#
# The `truncate` filter accepts an optional parameter to specify a custom ellipsis to be
# appended to the truncated string.
#
# ### No ellipsis
#
# If you don't want an ellipsis appended to your truncated string, then you can set the
# ellipsis parameter to a blank string (`''`).
# @syntax {{ string | truncate: character_count, ellipsis }}
# @return string
# Truncate a string down to x characters
def truncate(input, length = 50, truncate_string = "...")
return if input.nil?
input_str = input.to_s
@@ -134,26 +109,6 @@ module Liquid
input_str.length > length ? input_str[0...l].concat(truncate_string_str) : input_str
end
# @public_docs
# @type filter
# @category string
# @summary
# Truncates a string down to a specified number of words.
# @description
# Truncates a string down to a specified number of words. By default, an ellipsis (`...`)
# is appended to the truncated string.
#
# ### Custom ellipsis
#
# The `truncate` filter accepts an optional parameter to specify a custom ellipsis to be
# appended to the truncated string.
#
# ### No ellipsis
#
# If you don't want an ellipsis appended to your truncated string, then you can set the
# ellipsis parameter to a blank string (`''`).
# @syntax {{ string | truncatewords: word_count, ellipsis }}
# @return string
def truncatewords(input, words = 15, truncate_string = "...")
return if input.nil?
input = input.to_s
@@ -182,54 +137,18 @@ module Liquid
input.to_s.split(pattern.to_s)
end
# @public_docs
# @type filter
# @category string
# @summary
# Strips all whitespace, such as tabs, spaces, and newlines, from the left and right sides of a string.
# @description
# Strips all whitespace, such as tabs, spaces, and newlines, from the left and right sides of a string.
# @syntax {{ string | strip }}
# @return string
def strip(input)
input.to_s.strip
end
# @public_docs
# @type filter
# @category string
# @summary
# Strips all whitespace, such as tabs, spaces, and newlines, from the left side of a string.
# @description
# Strips all whitespace, such as tabs, spaces, and newlines, from the left side of a string.
# @syntax {{ string | lstrip }}
# @return string
def lstrip(input)
input.to_s.lstrip
end
# @public_docs
# @type filter
# @category string
# @summary
# Strips all whitespace, such as tabs, spaces, and newlines, from the right side of a string.
# @description
# Strips all whitespace, such as tabs, spaces, and newlines, from the right side of a string.
# @syntax {{ string | rstrip }}
# @return string
def rstrip(input)
input.to_s.rstrip
end
# @public_docs
# @type filter
# @category string
# @summary
# Strips all HTML tags from a string.
# @description
# Strips all HTML tags from a string.
# @syntax {{ string | strip_html }}
# @return string
def strip_html(input)
empty = ''
result = input.to_s.gsub(STRIP_HTML_BLOCKS, empty)
@@ -237,15 +156,7 @@ module Liquid
result
end
# @public_docs
# @type filter
# @category string
# @summary
# Strips all line breaks and newlines from a string.
# @description
# Strips all line breaks and newlines from a string.
# @syntax {{ string | strip_newlines }}
# @return string
# Remove all newlines from the string
def strip_newlines(input)
input.to_s.gsub(/\r?\n/, '')
end
@@ -561,6 +472,12 @@ module Liquid
raise Liquid::FloatDomainError, e.message
end
# @public_docs
# @syntax {{ number | at_least: number }}
# @summary Limits a number to a minimum value.
# @type filter
# @category Math
# @return number
def at_least(input, n)
min_value = Utils.to_number(n)
@@ -569,6 +486,12 @@ module Liquid
result.is_a?(BigDecimal) ? result.to_f : result
end
# @public_docs
# @syntax {{ number | at_most: number }}
# @summary Limits a number to a maximum value.
# @type filter
# @category Math
# @return number
def at_most(input, n)
max_value = Utils.to_number(n)