mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
Fix the find filter to return nil when filtering empty arrays
This commit is contained in:
committed by
Guilherme Carreiro
parent
b0dbc62696
commit
5718c4cee2
@@ -2,9 +2,14 @@
|
|||||||
|
|
||||||
## 5.8.0 (unreleased)
|
## 5.8.0 (unreleased)
|
||||||
|
|
||||||
|
## 5.7.1 2025-01-23
|
||||||
|
|
||||||
|
* Fix the `find` filter to return `nil` when filtering empty arrays
|
||||||
|
|
||||||
## 5.7.0 2025-01-16
|
## 5.7.0 2025-01-16
|
||||||
|
|
||||||
### Features
|
### Features
|
||||||
|
|
||||||
* Add `find`, `find_index`, `has`, and `reject` filters to arrays
|
* Add `find`, `find_index`, `has`, and `reject` filters to arrays
|
||||||
* Compatibility with Ruby 3.4
|
* Compatibility with Ruby 3.4
|
||||||
|
|
||||||
|
|||||||
@@ -472,7 +472,7 @@ module Liquid
|
|||||||
# @liquid_syntax array | find: string, string
|
# @liquid_syntax array | find: string, string
|
||||||
# @liquid_return [untyped]
|
# @liquid_return [untyped]
|
||||||
def find(input, property, target_value = nil)
|
def find(input, property, target_value = nil)
|
||||||
filter_array(input, property, target_value) { |ary, &block| ary.find(&block) }
|
filter_array(input, property, target_value, nil) { |ary, &block| ary.find(&block) }
|
||||||
end
|
end
|
||||||
|
|
||||||
# @liquid_public_docs
|
# @liquid_public_docs
|
||||||
@@ -969,10 +969,10 @@ module Liquid
|
|||||||
|
|
||||||
attr_reader :context
|
attr_reader :context
|
||||||
|
|
||||||
def filter_array(input, property, target_value, &block)
|
def filter_array(input, property, target_value, default_value = [], &block)
|
||||||
ary = InputIterator.new(input, context)
|
ary = InputIterator.new(input, context)
|
||||||
|
|
||||||
return [] if ary.empty?
|
return default_value if ary.empty?
|
||||||
|
|
||||||
block.call(ary) do |item|
|
block.call(ary) do |item|
|
||||||
if target_value.nil?
|
if target_value.nil?
|
||||||
|
|||||||
@@ -2,5 +2,5 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
module Liquid
|
module Liquid
|
||||||
VERSION = "5.7.0"
|
VERSION = "5.7.1"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -157,6 +157,10 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
assert_equal([], @filters.slice(input, -(1 << 63), 6))
|
assert_equal([], @filters.slice(input, -(1 << 63), 6))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_find_on_empty_array
|
||||||
|
assert_nil(@filters.find([], '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))
|
||||||
@@ -1042,6 +1046,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_with_empty_arrays
|
||||||
|
template = <<~LIQUID
|
||||||
|
{%- assign product = products | find: 'title.content', 'Not found' -%}
|
||||||
|
{%- unless product -%}
|
||||||
|
Product not found.
|
||||||
|
{%- endunless -%}
|
||||||
|
LIQUID
|
||||||
|
expected_output = "Product not found."
|
||||||
|
|
||||||
|
assert_template_result(expected_output, template, { "products" => [] })
|
||||||
|
end
|
||||||
|
|
||||||
def test_find_index_with_value
|
def test_find_index_with_value
|
||||||
products = [
|
products = [
|
||||||
{ "title" => "Pro goggles", "price" => 1299 },
|
{ "title" => "Pro goggles", "price" => 1299 },
|
||||||
|
|||||||
Reference in New Issue
Block a user