mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
Always stringify properties in all array filters (#1936)
* Always stringify sum property. * Add test * always stringify properties in all array filters * fix syntax error * up version --------- Co-authored-by: Dominic Petrick <[email protected]>
This commit is contained in:
co-authored by
Dominic Petrick
parent
f5d6a36574
commit
aa1640035f
@@ -387,6 +387,7 @@ module Liquid
|
|||||||
end
|
end
|
||||||
elsif ary.all? { |el| el.respond_to?(:[]) }
|
elsif ary.all? { |el| el.respond_to?(:[]) }
|
||||||
begin
|
begin
|
||||||
|
property = Utils.to_s(property)
|
||||||
ary.sort { |a, b| nil_safe_compare(a[property], b[property]) }
|
ary.sort { |a, b| nil_safe_compare(a[property], b[property]) }
|
||||||
rescue TypeError
|
rescue TypeError
|
||||||
raise_property_error(property)
|
raise_property_error(property)
|
||||||
@@ -416,6 +417,7 @@ module Liquid
|
|||||||
end
|
end
|
||||||
elsif ary.all? { |el| el.respond_to?(:[]) }
|
elsif ary.all? { |el| el.respond_to?(:[]) }
|
||||||
begin
|
begin
|
||||||
|
property = Utils.to_s(property)
|
||||||
ary.sort { |a, b| nil_safe_casecmp(a[property], b[property]) }
|
ary.sort { |a, b| nil_safe_casecmp(a[property], b[property]) }
|
||||||
rescue TypeError
|
rescue TypeError
|
||||||
raise_property_error(property)
|
raise_property_error(property)
|
||||||
@@ -503,6 +505,7 @@ module Liquid
|
|||||||
elsif ary.empty? # The next two cases assume a non-empty array.
|
elsif ary.empty? # The next two cases assume a non-empty array.
|
||||||
[]
|
[]
|
||||||
else
|
else
|
||||||
|
property = Utils.to_s(property)
|
||||||
ary.uniq do |item|
|
ary.uniq do |item|
|
||||||
item[property]
|
item[property]
|
||||||
rescue TypeError
|
rescue TypeError
|
||||||
@@ -534,6 +537,7 @@ module Liquid
|
|||||||
# @liquid_syntax array | map: string
|
# @liquid_syntax array | map: string
|
||||||
# @liquid_return [array[untyped]]
|
# @liquid_return [array[untyped]]
|
||||||
def map(input, property)
|
def map(input, property)
|
||||||
|
property = Utils.to_s(property)
|
||||||
InputIterator.new(input, context).map do |e|
|
InputIterator.new(input, context).map do |e|
|
||||||
e = e.call if e.is_a?(Proc)
|
e = e.call if e.is_a?(Proc)
|
||||||
|
|
||||||
@@ -563,6 +567,7 @@ module Liquid
|
|||||||
elsif ary.empty? # The next two cases assume a non-empty array.
|
elsif ary.empty? # The next two cases assume a non-empty array.
|
||||||
[]
|
[]
|
||||||
else
|
else
|
||||||
|
property = Liquid::Utils.to_s(property)
|
||||||
ary.reject do |item|
|
ary.reject do |item|
|
||||||
item[property].nil?
|
item[property].nil?
|
||||||
rescue TypeError
|
rescue TypeError
|
||||||
@@ -952,6 +957,8 @@ module Liquid
|
|||||||
# @liquid_syntax array | sum
|
# @liquid_syntax array | sum
|
||||||
# @liquid_return [number]
|
# @liquid_return [number]
|
||||||
def sum(input, property = nil)
|
def sum(input, property = nil)
|
||||||
|
property = property.nil? ? nil : Utils.to_s(property)
|
||||||
|
|
||||||
ary = InputIterator.new(input, context)
|
ary = InputIterator.new(input, context)
|
||||||
return 0 if ary.empty?
|
return 0 if ary.empty?
|
||||||
|
|
||||||
|
|||||||
@@ -2,5 +2,5 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
module Liquid
|
module Liquid
|
||||||
VERSION = "5.8.1"
|
VERSION = "5.8.2"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1281,6 +1281,17 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
assert_template_result("0", "{{ input | sum: 'subtotal' }}", { "input" => input })
|
assert_template_result("0", "{{ input | sum: 'subtotal' }}", { "input" => input })
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_sum_with_non_string_property
|
||||||
|
input = [{ "true" => 1 }, { "1.0" => 0.2, "1" => -0.3 }, { "1..5" => 0.4 }]
|
||||||
|
|
||||||
|
assert_equal(1, @filters.sum(input, true))
|
||||||
|
assert_equal(0.2, @filters.sum(input, 1.0))
|
||||||
|
assert_equal(-0.3, @filters.sum(input, 1))
|
||||||
|
assert_equal(0.4, @filters.sum(input, (1..5)))
|
||||||
|
assert_equal(0, @filters.sum(input, nil))
|
||||||
|
assert_equal(0, @filters.sum(input, ""))
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def with_timezone(tz)
|
def with_timezone(tz)
|
||||||
|
|||||||
Reference in New Issue
Block a user