Skip to content

Commit bf1419b

Browse files
committedJan 24, 2025
Apply the same fix for find_index and has
1 parent 5718c4c commit bf1419b

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed
 

‎History.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
## 5.7.1 2025-01-23
66

7-
* Fix the `find` filter to return `nil` when filtering empty arrays
7+
* Fix the `find` and `find_index`filters to return `nil` when filtering empty arrays
8+
* Fix the `has` filter to return `false` when filtering empty arrays
89

910
## 5.7.0 2025-01-16
1011

‎lib/liquid/standardfilters.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ def reject(input, property, target_value = nil)
459459
# @liquid_syntax array | some: string, string
460460
# @liquid_return [boolean]
461461
def has(input, property, target_value = nil)
462-
filter_array(input, property, target_value) { |ary, &block| ary.any?(&block) }
462+
filter_array(input, property, target_value, false) { |ary, &block| ary.any?(&block) }
463463
end
464464

465465
# @liquid_public_docs
@@ -485,7 +485,7 @@ def find(input, property, target_value = nil)
485485
# @liquid_syntax array | find_index: string, string
486486
# @liquid_return [number]
487487
def find_index(input, property, target_value = nil)
488-
filter_array(input, property, target_value) { |ary, &block| ary.find_index(&block) }
488+
filter_array(input, property, target_value, nil) { |ary, &block| ary.find_index(&block) }
489489
end
490490

491491
# @liquid_public_docs

‎test/integration/standard_filter_test.rb

+32
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ def test_find_on_empty_array
161161
assert_nil(@filters.find([], 'foo', 'bar'))
162162
end
163163

164+
def test_find_index_on_empty_array
165+
assert_nil(@filters.find_index([], 'foo', 'bar'))
166+
end
167+
168+
def test_has_on_empty_array
169+
refute(@filters.has([], 'foo', 'bar'))
170+
end
171+
164172
def test_truncate
165173
assert_equal('1234...', @filters.truncate('1234567890', 7))
166174
assert_equal('1234567890', @filters.truncate('1234567890', 20))
@@ -980,6 +988,18 @@ def test_has_when_does_not_have_it
980988
assert_template_result(expected_output, "{{ array | has: 'ok', true }}", { "array" => array })
981989
end
982990

991+
def test_has_with_empty_arrays
992+
template = <<~LIQUID
993+
{%- assign has_product = products | has: 'title.content', 'Not found' -%}
994+
{%- unless has_product -%}
995+
Product not found.
996+
{%- endunless -%}
997+
LIQUID
998+
expected_output = "Product not found."
999+
1000+
assert_template_result(expected_output, template, { "products" => [] })
1001+
end
1002+
9831003
def test_has_with_false_value
9841004
array = [
9851005
{ "handle" => "alpha", "ok" => true },
@@ -1086,6 +1106,18 @@ def test_find_index_with_deep_enumerables
10861106
assert_template_result(expected_output, template, { "products" => TestDeepEnumerable.new })
10871107
end
10881108

1109+
def test_find_index_with_empty_arrays
1110+
template = <<~LIQUID
1111+
{%- assign index = products | find_index: 'title.content', 'Not found' -%}
1112+
{%- unless index -%}
1113+
Index not found.
1114+
{%- endunless -%}
1115+
LIQUID
1116+
expected_output = "Index not found."
1117+
1118+
assert_template_result(expected_output, template, { "products" => [] })
1119+
end
1120+
10891121
def test_where
10901122
array = [
10911123
{ "handle" => "alpha", "ok" => true },

0 commit comments

Comments
 (0)
Failed to load comments.