remove .flatten on standard filters

This commit is contained in:
Arthur Neves
2014-07-23 17:14:26 -04:00
parent c864a75903
commit 6a061cbe81
2 changed files with 9 additions and 11 deletions
+7 -9
View File
@@ -92,13 +92,13 @@ module Liquid
# Join elements of the array with certain character between them
def join(input, glue = ' '.freeze)
[input].flatten.join(glue)
Array(input).join(glue)
end
# Sort elements of the array
# provide optional property with which to sort an array of hashes or drops
def sort(input, property = nil)
ary = flatten_if_necessary(input)
ary = map_input(input)
if property.nil?
ary.sort
elsif ary.first.respond_to?('[]'.freeze) and !ary.first[property].nil?
@@ -110,13 +110,13 @@ module Liquid
# Reverse the elements of an array
def reverse(input)
ary = [input].flatten
ary = Array(input)
ary.reverse
end
# map/collect on a given 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)
if property == "to_liquid".freeze
@@ -265,13 +265,11 @@ module Liquid
private
def flatten_if_necessary(input)
ary = if input.is_a?(Array)
input.flatten
elsif input.is_a?(Enumerable) && !input.is_a?(Hash)
def map_input(input)
ary = if input.is_a?(Array) || input.kind_of?(Enumerable)
input
else
[input].flatten
Array(input)
end
ary.map{ |e| e.respond_to?(:to_liquid) ? e.to_liquid : e }
end
+2 -2
View File
@@ -67,12 +67,12 @@ class FiltersTest < Test::Unit::TestCase
@context['value'] = 3
@context['numbers'] = [2,1,4,3]
@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 ['alphabetic', 'as', 'expected'], Variable.new("words | 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
def test_strip_html