From 6a061cbe8111d0a29565c108b05167f0c7da1e2d Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Wed, 14 Aug 2013 17:29:25 -0400 Subject: [PATCH] remove .flatten on standard filters --- lib/liquid/standardfilters.rb | 16 +++++++--------- test/integration/filter_test.rb | 4 ++-- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/lib/liquid/standardfilters.rb b/lib/liquid/standardfilters.rb index a244cc5a..1b8a511b 100644 --- a/lib/liquid/standardfilters.rb +++ b/lib/liquid/standardfilters.rb @@ -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 diff --git a/test/integration/filter_test.rb b/test/integration/filter_test.rb index 58c92513..b6d1d4bf 100644 --- a/test/integration/filter_test.rb +++ b/test/integration/filter_test.rb @@ -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