Skip to content

Commit cc04892

Browse files
authored
Merge pull request #1943 from Shopify/revert-to-s
Revert `Utils.to_s` on all array filters
2 parents dbe709c + f676e69 commit cc04892

File tree

3 files changed

+8
-52
lines changed

3 files changed

+8
-52
lines changed

lib/liquid/standardfilters.rb

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,6 @@ def sort(input, property = nil)
386386
end
387387
elsif ary.all? { |el| el.respond_to?(:[]) }
388388
begin
389-
property = Utils.to_s(property)
390389
ary.sort { |a, b| nil_safe_compare(a[property], b[property]) }
391390
rescue TypeError
392391
raise_property_error(property)
@@ -416,7 +415,6 @@ def sort_natural(input, property = nil)
416415
end
417416
elsif ary.all? { |el| el.respond_to?(:[]) }
418417
begin
419-
property = Utils.to_s(property)
420418
ary.sort { |a, b| nil_safe_casecmp(a[property], b[property]) }
421419
rescue TypeError
422420
raise_property_error(property)
@@ -504,7 +502,6 @@ def uniq(input, property = nil)
504502
elsif ary.empty? # The next two cases assume a non-empty array.
505503
[]
506504
else
507-
property = Utils.to_s(property)
508505
ary.uniq do |item|
509506
item[property]
510507
rescue TypeError
@@ -536,11 +533,6 @@ def reverse(input)
536533
# @liquid_syntax array | map: string
537534
# @liquid_return [array[untyped]]
538535
def map(input, property)
539-
property = Utils.to_s(property)
540-
541-
# Return the input array if property is empty (no-op)
542-
return InputIterator.new(input, context).to_a if property.empty?
543-
544536
InputIterator.new(input, context).map do |e|
545537
e = e.call if e.is_a?(Proc)
546538

@@ -570,7 +562,6 @@ def compact(input, property = nil)
570562
elsif ary.empty? # The next two cases assume a non-empty array.
571563
[]
572564
else
573-
property = Liquid::Utils.to_s(property)
574565
ary.reject do |item|
575566
item[property].nil?
576567
rescue TypeError
@@ -960,8 +951,6 @@ def default(input, default_value = '', options = {})
960951
# @liquid_syntax array | sum
961952
# @liquid_return [number]
962953
def sum(input, property = nil)
963-
property = property.nil? ? nil : Utils.to_s(property)
964-
965954
ary = InputIterator.new(input, context)
966955
return 0 if ary.empty?
967956

@@ -990,6 +979,7 @@ def sum(input, property = nil)
990979

991980
def filter_array(input, property, target_value, default_value = [], &block)
992981
ary = InputIterator.new(input, context)
982+
993983
return default_value if ary.empty?
994984

995985
property = Utils.to_s(property)
@@ -1009,7 +999,7 @@ def filter_array(input, property, target_value, default_value = [], &block)
1009999
end
10101000

10111001
def raise_property_error(property)
1012-
raise Liquid::ArgumentError, "cannot select the property '#{property}'"
1002+
raise Liquid::ArgumentError, "cannot select the property '#{Utils.to_s(property)}'"
10131003
end
10141004

10151005
def apply_operation(input, operand, operation)

lib/liquid/version.rb

Lines changed: 1 addition & 1 deletion
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.4"
5+
VERSION = "5.8.5"
66
end

test/integration/standard_filter_test.rb

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -560,26 +560,6 @@ def test_map_returns_empty_on_2d_input_array
560560
end
561561
end
562562

563-
def test_map_with_nil_property
564-
array = [
565-
{ "handle" => "alpha", "value" => "A" },
566-
{ "handle" => "beta", "value" => "B" },
567-
{ "handle" => "gamma", "value" => "C" }
568-
]
569-
570-
assert_template_result("alpha beta gamma", "{{ array | map: nil | map: 'handle' | join: ' ' }}", { "array" => array })
571-
end
572-
573-
def test_map_with_empty_string_property
574-
array = [
575-
{ "handle" => "alpha", "value" => "A" },
576-
{ "handle" => "beta", "value" => "B" },
577-
{ "handle" => "gamma", "value" => "C" }
578-
]
579-
580-
assert_template_result("alpha beta gamma", "{{ array | map: '' | map: 'handle' | join: ' ' }}", { "array" => array })
581-
end
582-
583563
def test_map_with_value_property
584564
array = [
585565
{ "handle" => "alpha", "value" => "A" },
@@ -591,16 +571,15 @@ def test_map_with_value_property
591571
end
592572

593573
def test_map_returns_input_with_no_property
594-
input = [
574+
foo = [
595575
[1],
596576
[2],
597577
[3],
598578
]
599-
result = @filters.map(input, nil)
600-
assert_equal(input.flatten, result)
601579

602-
result = @filters.map(input, '')
603-
assert_equal(input.flatten, result)
580+
assert_raises(Liquid::ArgumentError) do
581+
@filters.map(foo, nil)
582+
end
604583
end
605584

606585
def test_sort_works_on_enumerables
@@ -1109,19 +1088,6 @@ def test_where_with_false_value
11091088
assert_template_result(expected_output, template, { "array" => array })
11101089
end
11111090

1112-
def test_where_with_non_string_property
1113-
array = [
1114-
{ "handle" => "alpha", "{}" => true },
1115-
{ "handle" => "beta", "{}" => false },
1116-
{ "handle" => "gamma", "{}" => false },
1117-
{ "handle" => "delta", "{}" => true },
1118-
]
1119-
template = "{{ array | where: some_property, true | map: 'handle' | join: ' ' }}"
1120-
expected_output = "alpha delta"
1121-
1122-
assert_template_result(expected_output, template, { "array" => array, "some_property" => {} })
1123-
end
1124-
11251091
def test_where_string_keys
11261092
input = [
11271093
"alpha", "beta", "gamma", "delta"
@@ -1330,7 +1296,7 @@ def test_sum_with_floats_and_indexable_map_values
13301296
end
13311297

13321298
def test_sum_with_non_string_property
1333-
input = [{ "true" => 1 }, { "1.0" => 0.2, "1" => -0.3 }, { "1..5" => 0.4 }]
1299+
input = [{ true => 1 }, { 1.0 => 0.2, 1 => -0.3 }, { 1..5 => 0.4 }]
13341300

13351301
assert_equal(1, @filters.sum(input, true))
13361302
assert_equal(0.2, @filters.sum(input, 1.0))

0 commit comments

Comments
 (0)