mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
remove .flatten on standard filters
This commit is contained in:
@@ -92,13 +92,13 @@ module Liquid
|
|||||||
|
|
||||||
# Join elements of the array with certain character between them
|
# Join elements of the array with certain character between them
|
||||||
def join(input, glue = ' '.freeze)
|
def join(input, glue = ' '.freeze)
|
||||||
[input].flatten.join(glue)
|
Array(input).join(glue)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Sort elements of the array
|
# Sort elements of the array
|
||||||
# provide optional property with which to sort an array of hashes or drops
|
# provide optional property with which to sort an array of hashes or drops
|
||||||
def sort(input, property = nil)
|
def sort(input, property = nil)
|
||||||
ary = flatten_if_necessary(input)
|
ary = map_input(input)
|
||||||
if property.nil?
|
if property.nil?
|
||||||
ary.sort
|
ary.sort
|
||||||
elsif ary.first.respond_to?('[]'.freeze) and !ary.first[property].nil?
|
elsif ary.first.respond_to?('[]'.freeze) and !ary.first[property].nil?
|
||||||
@@ -110,13 +110,13 @@ module Liquid
|
|||||||
|
|
||||||
# Reverse the elements of an array
|
# Reverse the elements of an array
|
||||||
def reverse(input)
|
def reverse(input)
|
||||||
ary = [input].flatten
|
ary = Array(input)
|
||||||
ary.reverse
|
ary.reverse
|
||||||
end
|
end
|
||||||
|
|
||||||
# map/collect on a given property
|
# map/collect on a given property
|
||||||
def map(input, property)
|
def map(input, property)
|
||||||
flatten_if_necessary(input).map do |e|
|
map_input(input).map do |e|
|
||||||
e = e.call if e.is_a?(Proc)
|
e = e.call if e.is_a?(Proc)
|
||||||
|
|
||||||
if property == "to_liquid".freeze
|
if property == "to_liquid".freeze
|
||||||
@@ -265,13 +265,11 @@ module Liquid
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def flatten_if_necessary(input)
|
def map_input(input)
|
||||||
ary = if input.is_a?(Array)
|
ary = if input.is_a?(Array) || input.kind_of?(Enumerable)
|
||||||
input.flatten
|
|
||||||
elsif input.is_a?(Enumerable) && !input.is_a?(Hash)
|
|
||||||
input
|
input
|
||||||
else
|
else
|
||||||
[input].flatten
|
Array(input)
|
||||||
end
|
end
|
||||||
ary.map{ |e| e.respond_to?(:to_liquid) ? e.to_liquid : e }
|
ary.map{ |e| e.respond_to?(:to_liquid) ? e.to_liquid : e }
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -67,12 +67,12 @@ class FiltersTest < Test::Unit::TestCase
|
|||||||
@context['value'] = 3
|
@context['value'] = 3
|
||||||
@context['numbers'] = [2,1,4,3]
|
@context['numbers'] = [2,1,4,3]
|
||||||
@context['words'] = ['expected', 'as', 'alphabetic']
|
@context['words'] = ['expected', 'as', 'alphabetic']
|
||||||
@context['arrays'] = [['flattened'], ['are']]
|
@context['arrays'] = ['flower', 'are']
|
||||||
|
|
||||||
assert_equal [1,2,3,4], Variable.new("numbers | sort").render(@context)
|
assert_equal [1,2,3,4], Variable.new("numbers | sort").render(@context)
|
||||||
assert_equal ['alphabetic', 'as', 'expected'], Variable.new("words | sort").render(@context)
|
assert_equal ['alphabetic', 'as', 'expected'], Variable.new("words | sort").render(@context)
|
||||||
assert_equal [3], Variable.new("value | sort").render(@context)
|
assert_equal [3], Variable.new("value | sort").render(@context)
|
||||||
assert_equal ['are', 'flattened'], Variable.new("arrays | sort").render(@context)
|
assert_equal ['are', 'flower'], Variable.new("arrays | sort").render(@context)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_strip_html
|
def test_strip_html
|
||||||
|
|||||||
Reference in New Issue
Block a user