mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-15 08:50:45 -07:00
Gracefully empty property in map filter
This commit is contained in:
@@ -536,11 +536,11 @@ module Liquid
|
|||||||
# @liquid_syntax array | map: string
|
# @liquid_syntax array | map: string
|
||||||
# @liquid_return [array[untyped]]
|
# @liquid_return [array[untyped]]
|
||||||
def map(input, property)
|
def map(input, property)
|
||||||
if property.nil?
|
|
||||||
raise_property_error(property)
|
|
||||||
end
|
|
||||||
|
|
||||||
property = Utils.to_s(property)
|
property = Utils.to_s(property)
|
||||||
|
|
||||||
|
# Return the input array if property is empty (no-op)
|
||||||
|
return InputIterator.new(input, context).to_a if property.empty?
|
||||||
|
|
||||||
InputIterator.new(input, context).map do |e|
|
InputIterator.new(input, context).map do |e|
|
||||||
e = e.call if e.is_a?(Proc)
|
e = e.call if e.is_a?(Proc)
|
||||||
|
|
||||||
|
|||||||
@@ -560,15 +560,47 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_map_returns_empty_with_no_property
|
def test_map_with_nil_property
|
||||||
foo = [
|
array = [
|
||||||
|
{ "handle" => "alpha", "value" => "A" },
|
||||||
|
{ "handle" => "beta", "value" => "B" },
|
||||||
|
{ "handle" => "gamma", "value" => "C" }
|
||||||
|
]
|
||||||
|
|
||||||
|
assert_template_result("alpha beta gamma", "{{ array | map: nil | map: 'handle' | join: ' ' }}", { "array" => array })
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_map_with_empty_string_property
|
||||||
|
array = [
|
||||||
|
{ "handle" => "alpha", "value" => "A" },
|
||||||
|
{ "handle" => "beta", "value" => "B" },
|
||||||
|
{ "handle" => "gamma", "value" => "C" }
|
||||||
|
]
|
||||||
|
|
||||||
|
assert_template_result("alpha beta gamma", "{{ array | map: '' | map: 'handle' | join: ' ' }}", { "array" => array })
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_map_with_value_property
|
||||||
|
array = [
|
||||||
|
{ "handle" => "alpha", "value" => "A" },
|
||||||
|
{ "handle" => "beta", "value" => "B" },
|
||||||
|
{ "handle" => "gamma", "value" => "C" }
|
||||||
|
]
|
||||||
|
|
||||||
|
assert_template_result("A B C", "{{ array | map: 'value' | join: ' ' }}", { "array" => array })
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_map_returns_input_with_no_property
|
||||||
|
input = [
|
||||||
[1],
|
[1],
|
||||||
[2],
|
[2],
|
||||||
[3],
|
[3],
|
||||||
]
|
]
|
||||||
assert_raises(Liquid::ArgumentError) do
|
result = @filters.map(input, nil)
|
||||||
@filters.map(foo, nil)
|
assert_equal(input.flatten, result)
|
||||||
end
|
|
||||||
|
result = @filters.map(input, '')
|
||||||
|
assert_equal(input.flatten, result)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_sort_works_on_enumerables
|
def test_sort_works_on_enumerables
|
||||||
|
|||||||
Reference in New Issue
Block a user