mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
Apply the same fix for find_index and has
This commit is contained in:
committed by
Guilherme Carreiro
parent
5718c4cee2
commit
bf1419b8ac
+2
-1
@@ -4,7 +4,8 @@
|
|||||||
|
|
||||||
## 5.7.1 2025-01-23
|
## 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
|
## 5.7.0 2025-01-16
|
||||||
|
|
||||||
|
|||||||
@@ -459,7 +459,7 @@ module Liquid
|
|||||||
# @liquid_syntax array | some: string, string
|
# @liquid_syntax array | some: string, string
|
||||||
# @liquid_return [boolean]
|
# @liquid_return [boolean]
|
||||||
def has(input, property, target_value = nil)
|
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
|
end
|
||||||
|
|
||||||
# @liquid_public_docs
|
# @liquid_public_docs
|
||||||
@@ -485,7 +485,7 @@ module Liquid
|
|||||||
# @liquid_syntax array | find_index: string, string
|
# @liquid_syntax array | find_index: string, string
|
||||||
# @liquid_return [number]
|
# @liquid_return [number]
|
||||||
def find_index(input, property, target_value = nil)
|
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
|
end
|
||||||
|
|
||||||
# @liquid_public_docs
|
# @liquid_public_docs
|
||||||
|
|||||||
@@ -161,6 +161,14 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
assert_nil(@filters.find([], 'foo', 'bar'))
|
assert_nil(@filters.find([], 'foo', 'bar'))
|
||||||
end
|
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
|
def test_truncate
|
||||||
assert_equal('1234...', @filters.truncate('1234567890', 7))
|
assert_equal('1234...', @filters.truncate('1234567890', 7))
|
||||||
assert_equal('1234567890', @filters.truncate('1234567890', 20))
|
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 })
|
assert_template_result(expected_output, "{{ array | has: 'ok', true }}", { "array" => array })
|
||||||
end
|
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
|
def test_has_with_false_value
|
||||||
array = [
|
array = [
|
||||||
{ "handle" => "alpha", "ok" => true },
|
{ "handle" => "alpha", "ok" => true },
|
||||||
@@ -1086,6 +1106,18 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
assert_template_result(expected_output, template, { "products" => TestDeepEnumerable.new })
|
assert_template_result(expected_output, template, { "products" => TestDeepEnumerable.new })
|
||||||
end
|
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
|
def test_where
|
||||||
array = [
|
array = [
|
||||||
{ "handle" => "alpha", "ok" => true },
|
{ "handle" => "alpha", "ok" => true },
|
||||||
|
|||||||
Reference in New Issue
Block a user