diff --git a/History.md b/History.md index eda21408..94d25da3 100644 --- a/History.md +++ b/History.md @@ -4,7 +4,8 @@ ## 5.7.1 2025-01-23 -* Fix the `find` filter to return `nil` when filtering empty arrays +* Fix the `find` and `find_index`filters to return `nil` when filtering empty arrays +* Fix the `has` filter to return `false` when filtering empty arrays ## 5.7.0 2025-01-16 diff --git a/lib/liquid/standardfilters.rb b/lib/liquid/standardfilters.rb index c4bd8758..aee01184 100644 --- a/lib/liquid/standardfilters.rb +++ b/lib/liquid/standardfilters.rb @@ -459,7 +459,7 @@ module Liquid # @liquid_syntax array | some: string, string # @liquid_return [boolean] def has(input, property, target_value = nil) - filter_array(input, property, target_value) { |ary, &block| ary.any?(&block) } + filter_array(input, property, target_value, false) { |ary, &block| ary.any?(&block) } end # @liquid_public_docs @@ -485,7 +485,7 @@ module Liquid # @liquid_syntax array | find_index: string, string # @liquid_return [number] def find_index(input, property, target_value = nil) - filter_array(input, property, target_value) { |ary, &block| ary.find_index(&block) } + filter_array(input, property, target_value, nil) { |ary, &block| ary.find_index(&block) } end # @liquid_public_docs diff --git a/test/integration/standard_filter_test.rb b/test/integration/standard_filter_test.rb index 3ea9abae..0ef8e2bc 100644 --- a/test/integration/standard_filter_test.rb +++ b/test/integration/standard_filter_test.rb @@ -161,6 +161,14 @@ class StandardFiltersTest < Minitest::Test assert_nil(@filters.find([], 'foo', 'bar')) end + def test_find_index_on_empty_array + assert_nil(@filters.find_index([], 'foo', 'bar')) + end + + def test_has_on_empty_array + refute(@filters.has([], 'foo', 'bar')) + end + def test_truncate assert_equal('1234...', @filters.truncate('1234567890', 7)) assert_equal('1234567890', @filters.truncate('1234567890', 20)) @@ -980,6 +988,18 @@ class StandardFiltersTest < Minitest::Test assert_template_result(expected_output, "{{ array | has: 'ok', true }}", { "array" => array }) end + def test_has_with_empty_arrays + template = <<~LIQUID + {%- assign has_product = products | has: 'title.content', 'Not found' -%} + {%- unless has_product -%} + Product not found. + {%- endunless -%} + LIQUID + expected_output = "Product not found." + + assert_template_result(expected_output, template, { "products" => [] }) + end + def test_has_with_false_value array = [ { "handle" => "alpha", "ok" => true }, @@ -1086,6 +1106,18 @@ class StandardFiltersTest < Minitest::Test assert_template_result(expected_output, template, { "products" => TestDeepEnumerable.new }) end + def test_find_index_with_empty_arrays + template = <<~LIQUID + {%- assign index = products | find_index: 'title.content', 'Not found' -%} + {%- unless index -%} + Index not found. + {%- endunless -%} + LIQUID + expected_output = "Index not found." + + assert_template_result(expected_output, template, { "products" => [] }) + end + def test_where array = [ { "handle" => "alpha", "ok" => true },