mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-19 10:52:48 -07:00
Make all array filters that use filter_array util process empty string and nil correctly
This commit is contained in:
@@ -986,8 +986,11 @@ module Liquid
|
|||||||
attr_reader :context
|
attr_reader :context
|
||||||
|
|
||||||
def filter_array(input, property, target_value, default_value = [], &block)
|
def filter_array(input, property, target_value, default_value = [], &block)
|
||||||
|
if property.nil?
|
||||||
|
raise_property_error(property)
|
||||||
|
end
|
||||||
|
|
||||||
property = Liquid::Utils.to_s(property)
|
property = Liquid::Utils.to_s(property)
|
||||||
return default_value if property.empty?
|
|
||||||
|
|
||||||
ary = InputIterator.new(input, context)
|
ary = InputIterator.new(input, context)
|
||||||
return default_value if ary.empty?
|
return default_value if ary.empty?
|
||||||
|
|||||||
@@ -1033,6 +1033,24 @@ 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_empty_string_and_nil
|
||||||
|
array = [
|
||||||
|
"alpha",
|
||||||
|
"beta",
|
||||||
|
"gamma",
|
||||||
|
]
|
||||||
|
template = "{{ array | where: '' | join: ' ' }}"
|
||||||
|
expected_output = "alpha beta gamma"
|
||||||
|
assert_template_result(expected_output, template, { "array" => array })
|
||||||
|
|
||||||
|
template = "{{ array | where: nil | join: ' ' }}"
|
||||||
|
exception = assert_raises(Liquid::ArgumentError) do
|
||||||
|
assert_template_result(expected_output, template, { "array" => array })
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_equal("Liquid error (line 1): cannot select the property ''", exception.message)
|
||||||
|
end
|
||||||
|
|
||||||
def test_where_with_value
|
def test_where_with_value
|
||||||
array = [
|
array = [
|
||||||
{ "handle" => "alpha", "ok" => true },
|
{ "handle" => "alpha", "ok" => true },
|
||||||
|
|||||||
Reference in New Issue
Block a user