mirror of
https://github.com/Shopify/liquid.git
synced 2026-10-03 09:05:13 -07:00
Merge pull request #1943 from Shopify/revert-to-s
Revert `Utils.to_s` on all array filters
This commit is contained in:
@@ -386,7 +386,6 @@ 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,7 +415,6 @@ 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)
|
||||||
@@ -504,7 +502,6 @@ 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
|
||||||
@@ -536,11 +533,6 @@ 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)
|
|
||||||
|
|
||||||
# Return the input array if property is empty (no-op)
|
|
||||||
return InputIterator.new(input, context).to_a if property.empty?
|
|
||||||
|
|
||||||
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)
|
||||||
|
|
||||||
@@ -570,7 +562,6 @@ 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
|
||||||
@@ -960,8 +951,6 @@ 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?
|
||||||
|
|
||||||
@@ -990,6 +979,7 @@ module Liquid
|
|||||||
|
|
||||||
def filter_array(input, property, target_value, default_value = [], &block)
|
def filter_array(input, property, target_value, default_value = [], &block)
|
||||||
ary = InputIterator.new(input, context)
|
ary = InputIterator.new(input, context)
|
||||||
|
|
||||||
return default_value if ary.empty?
|
return default_value if ary.empty?
|
||||||
|
|
||||||
property = Utils.to_s(property)
|
property = Utils.to_s(property)
|
||||||
@@ -1009,7 +999,7 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
def raise_property_error(property)
|
def raise_property_error(property)
|
||||||
raise Liquid::ArgumentError, "cannot select the property '#{property}'"
|
raise Liquid::ArgumentError, "cannot select the property '#{Utils.to_s(property)}'"
|
||||||
end
|
end
|
||||||
|
|
||||||
def apply_operation(input, operand, operation)
|
def apply_operation(input, operand, operation)
|
||||||
|
|||||||
@@ -2,5 +2,5 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
module Liquid
|
module Liquid
|
||||||
VERSION = "5.8.4"
|
VERSION = "5.8.5"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -560,26 +560,6 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_map_with_nil_property
|
|
||||||
array = [
|
|
||||||
{ "handle" => "alpha", "value" => "A" },
|
|
||||||
{ "handle" => "beta", "value" => "B" },
|
|
||||||
{ "handle" => "gamma", "value" => "C" }
|
|
||||||
]
|
|
||||||
|
|
||||||
assert_template_result("alpha beta gamma", "{{ array | map: nil | map: 'handle' | join: ' ' }}", { "array" => array })
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_map_with_empty_string_property
|
|
||||||
array = [
|
|
||||||
{ "handle" => "alpha", "value" => "A" },
|
|
||||||
{ "handle" => "beta", "value" => "B" },
|
|
||||||
{ "handle" => "gamma", "value" => "C" }
|
|
||||||
]
|
|
||||||
|
|
||||||
assert_template_result("alpha beta gamma", "{{ array | map: '' | map: 'handle' | join: ' ' }}", { "array" => array })
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_map_with_value_property
|
def test_map_with_value_property
|
||||||
array = [
|
array = [
|
||||||
{ "handle" => "alpha", "value" => "A" },
|
{ "handle" => "alpha", "value" => "A" },
|
||||||
@@ -591,16 +571,15 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_map_returns_input_with_no_property
|
def test_map_returns_input_with_no_property
|
||||||
input = [
|
foo = [
|
||||||
[1],
|
[1],
|
||||||
[2],
|
[2],
|
||||||
[3],
|
[3],
|
||||||
]
|
]
|
||||||
result = @filters.map(input, nil)
|
|
||||||
assert_equal(input.flatten, result)
|
|
||||||
|
|
||||||
result = @filters.map(input, '')
|
assert_raises(Liquid::ArgumentError) do
|
||||||
assert_equal(input.flatten, result)
|
@filters.map(foo, nil)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_sort_works_on_enumerables
|
def test_sort_works_on_enumerables
|
||||||
@@ -1109,19 +1088,6 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
assert_template_result(expected_output, template, { "array" => array })
|
assert_template_result(expected_output, template, { "array" => array })
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_where_with_non_string_property
|
|
||||||
array = [
|
|
||||||
{ "handle" => "alpha", "{}" => true },
|
|
||||||
{ "handle" => "beta", "{}" => false },
|
|
||||||
{ "handle" => "gamma", "{}" => false },
|
|
||||||
{ "handle" => "delta", "{}" => true },
|
|
||||||
]
|
|
||||||
template = "{{ array | where: some_property, true | map: 'handle' | join: ' ' }}"
|
|
||||||
expected_output = "alpha delta"
|
|
||||||
|
|
||||||
assert_template_result(expected_output, template, { "array" => array, "some_property" => {} })
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_where_string_keys
|
def test_where_string_keys
|
||||||
input = [
|
input = [
|
||||||
"alpha", "beta", "gamma", "delta"
|
"alpha", "beta", "gamma", "delta"
|
||||||
@@ -1330,7 +1296,7 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_sum_with_non_string_property
|
def test_sum_with_non_string_property
|
||||||
input = [{ "true" => 1 }, { "1.0" => 0.2, "1" => -0.3 }, { "1..5" => 0.4 }]
|
input = [{ true => 1 }, { 1.0 => 0.2, 1 => -0.3 }, { 1..5 => 0.4 }]
|
||||||
|
|
||||||
assert_equal(1, @filters.sum(input, true))
|
assert_equal(1, @filters.sum(input, true))
|
||||||
assert_equal(0.2, @filters.sum(input, 1.0))
|
assert_equal(0.2, @filters.sum(input, 1.0))
|
||||||
|
|||||||
Reference in New Issue
Block a user