Skip to content

Commit aa16400

Browse files
Maaarcocrdpetrick
andauthored
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]>
1 parent f5d6a36 commit aa16400

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

lib/liquid/standardfilters.rb

+7
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ def sort(input, property = nil)
387387
end
388388
elsif ary.all? { |el| el.respond_to?(:[]) }
389389
begin
390+
property = Utils.to_s(property)
390391
ary.sort { |a, b| nil_safe_compare(a[property], b[property]) }
391392
rescue TypeError
392393
raise_property_error(property)
@@ -416,6 +417,7 @@ def sort_natural(input, property = nil)
416417
end
417418
elsif ary.all? { |el| el.respond_to?(:[]) }
418419
begin
420+
property = Utils.to_s(property)
419421
ary.sort { |a, b| nil_safe_casecmp(a[property], b[property]) }
420422
rescue TypeError
421423
raise_property_error(property)
@@ -503,6 +505,7 @@ def uniq(input, property = nil)
503505
elsif ary.empty? # The next two cases assume a non-empty array.
504506
[]
505507
else
508+
property = Utils.to_s(property)
506509
ary.uniq do |item|
507510
item[property]
508511
rescue TypeError
@@ -534,6 +537,7 @@ def reverse(input)
534537
# @liquid_syntax array | map: string
535538
# @liquid_return [array[untyped]]
536539
def map(input, property)
540+
property = Utils.to_s(property)
537541
InputIterator.new(input, context).map do |e|
538542
e = e.call if e.is_a?(Proc)
539543

@@ -563,6 +567,7 @@ def compact(input, property = nil)
563567
elsif ary.empty? # The next two cases assume a non-empty array.
564568
[]
565569
else
570+
property = Liquid::Utils.to_s(property)
566571
ary.reject do |item|
567572
item[property].nil?
568573
rescue TypeError
@@ -952,6 +957,8 @@ def default(input, default_value = '', options = {})
952957
# @liquid_syntax array | sum
953958
# @liquid_return [number]
954959
def sum(input, property = nil)
960+
property = property.nil? ? nil : Utils.to_s(property)
961+
955962
ary = InputIterator.new(input, context)
956963
return 0 if ary.empty?
957964

lib/liquid/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
# frozen_string_literal: true
33

44
module Liquid
5-
VERSION = "5.8.1"
5+
VERSION = "5.8.2"
66
end

test/integration/standard_filter_test.rb

+11
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,17 @@ def test_sum_with_floats_and_indexable_map_values
12811281
assert_template_result("0", "{{ input | sum: 'subtotal' }}", { "input" => input })
12821282
end
12831283

1284+
def test_sum_with_non_string_property
1285+
input = [{ "true" => 1 }, { "1.0" => 0.2, "1" => -0.3 }, { "1..5" => 0.4 }]
1286+
1287+
assert_equal(1, @filters.sum(input, true))
1288+
assert_equal(0.2, @filters.sum(input, 1.0))
1289+
assert_equal(-0.3, @filters.sum(input, 1))
1290+
assert_equal(0.4, @filters.sum(input, (1..5)))
1291+
assert_equal(0, @filters.sum(input, nil))
1292+
assert_equal(0, @filters.sum(input, ""))
1293+
end
1294+
12841295
private
12851296

12861297
def with_timezone(tz)

0 commit comments

Comments
 (0)