Skip to content

Commit

Permalink
Apply the same fix for find_index and has
Browse files Browse the repository at this point in the history
  • Loading branch information
karreiro committed Jan 24, 2025
1 parent 5718c4c commit bf1419b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
3 changes: 2 additions & 1 deletion History.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions lib/liquid/standardfilters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ def reject(input, property, target_value = nil)
# @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
Expand All @@ -485,7 +485,7 @@ def find(input, property, target_value = nil)
# @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
Expand Down
32 changes: 32 additions & 0 deletions test/integration/standard_filter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ def test_find_on_empty_array
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))
Expand Down Expand Up @@ -980,6 +988,18 @@ def test_has_when_does_not_have_it
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 },
Expand Down Expand Up @@ -1086,6 +1106,18 @@ def test_find_index_with_deep_enumerables
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 },
Expand Down

0 comments on commit bf1419b

Please sign in to comment.