From 6a061cbe8111d0a29565c108b05167f0c7da1e2d Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Wed, 14 Aug 2013 17:29:25 -0400 Subject: [PATCH 1/8] 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 From d099878385fa13efe0149ecc8b77a1575e27281c Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Wed, 14 Aug 2013 18:43:57 -0400 Subject: [PATCH 2/8] add a input iterator to standard filter --- lib/liquid/standardfilters.rb | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/liquid/standardfilters.rb b/lib/liquid/standardfilters.rb index 1b8a511b..0c738f73 100644 --- a/lib/liquid/standardfilters.rb +++ b/lib/liquid/standardfilters.rb @@ -98,7 +98,7 @@ module Liquid # Sort elements of the array # provide optional property with which to sort an array of hashes or drops def sort(input, property = nil) - ary = map_input(input) + ary = InputIterator.new(input).to_a if property.nil? ary.sort elsif ary.first.respond_to?('[]'.freeze) and !ary.first[property].nil? @@ -116,7 +116,7 @@ module Liquid # map/collect on a given property def map(input, property) - map_input(input).map do |e| + InputIterator.new(input).map do |e| e = e.call if e.is_a?(Proc) if property == "to_liquid".freeze @@ -265,15 +265,6 @@ module Liquid private - def map_input(input) - ary = if input.is_a?(Array) || input.kind_of?(Enumerable) - input - else - Array(input) - end - ary.map{ |e| e.respond_to?(:to_liquid) ? e.to_liquid : e } - end - def to_number(obj) case obj when Float @@ -308,6 +299,25 @@ module Liquid result = to_number(input).send(operation, to_number(operand)) result.is_a?(BigDecimal) ? result.to_f : result end + + class InputIterator + include Enumerable + + def initialize(input) + @input = + if input.is_a?(Array) || input.kind_of?(Enumerable) + input + else + Array(input) + end + end + + def each + @input.each do |e| + yield(e.respond_to?(:to_liquid) ? e.to_liquid : e) + end + end + end end Template.register_filter(StandardFilters) From 02d42a14755054c97b645d766f0f4291df8825dd Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Wed, 14 Aug 2013 21:36:31 -0400 Subject: [PATCH 3/8] Array is a Enumerable --- lib/liquid/standardfilters.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/liquid/standardfilters.rb b/lib/liquid/standardfilters.rb index 0c738f73..1ddd800a 100644 --- a/lib/liquid/standardfilters.rb +++ b/lib/liquid/standardfilters.rb @@ -305,7 +305,7 @@ module Liquid def initialize(input) @input = - if input.is_a?(Array) || input.kind_of?(Enumerable) + if input.kind_of?(Enumerable) input else Array(input) From 994f30946549c7959b4f4f53de2242030bbc5af4 Mon Sep 17 00:00:00 2001 From: Florian Weingarten Date: Thu, 15 Aug 2013 03:49:19 +0200 Subject: [PATCH 4/8] Fix broken standardfilter test --- lib/liquid/standardfilters.rb | 2 +- test/integration/standard_filter_test.rb | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/liquid/standardfilters.rb b/lib/liquid/standardfilters.rb index 1ddd800a..849fe226 100644 --- a/lib/liquid/standardfilters.rb +++ b/lib/liquid/standardfilters.rb @@ -98,7 +98,7 @@ module Liquid # Sort elements of the array # provide optional property with which to sort an array of hashes or drops def sort(input, property = nil) - ary = InputIterator.new(input).to_a + ary = InputIterator.new(input) if property.nil? ary.sort elsif ary.first.respond_to?('[]'.freeze) and !ary.first[property].nil? diff --git a/test/integration/standard_filter_test.rb b/test/integration/standard_filter_test.rb index 9486ff6f..7b417ae6 100644 --- a/test/integration/standard_filter_test.rb +++ b/test/integration/standard_filter_test.rb @@ -7,6 +7,8 @@ class Filters end class TestThing + attr_reader :foo + def initialize @foo = 0 end @@ -149,7 +151,8 @@ class StandardFiltersTest < Test::Unit::TestCase def test_sort_calls_to_liquid t = TestThing.new - assert_template_result "woot: 1", '{{ foo | sort: "whatever" }}', "foo" => [t] + Liquid::Template.parse('{{ foo | sort: "whatever" }}').render("foo" => [t]) + assert t.foo > 0 end def test_map_over_proc From 18e8ce1eb05af73daa7b8cc3258d8e67ec388f5d Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Thu, 15 Aug 2013 16:06:56 -0400 Subject: [PATCH 5/8] add flatten filter --- lib/liquid/standardfilters.rb | 7 ++++++- test/integration/standard_filter_test.rb | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/liquid/standardfilters.rb b/lib/liquid/standardfilters.rb index 849fe226..7bd9cd86 100644 --- a/lib/liquid/standardfilters.rb +++ b/lib/liquid/standardfilters.rb @@ -101,7 +101,7 @@ module Liquid ary = InputIterator.new(input) if property.nil? ary.sort - elsif ary.first.respond_to?('[]'.freeze) and !ary.first[property].nil? + elsif ary.first.respond_to?('[]'.freeze) && !ary.first[property].nil? ary.sort {|a,b| a[property] <=> b[property] } elsif ary.first.respond_to?(property) ary.sort {|a,b| a.send(property) <=> b.send(property) } @@ -127,6 +127,11 @@ module Liquid end end + # flatten the given input + def flatten(input) + Array(input).flatten + end + # Replace occurrences of a string with another def replace(input, string, replacement = ''.freeze) input.to_s.gsub(string, replacement.to_s) diff --git a/test/integration/standard_filter_test.rb b/test/integration/standard_filter_test.rb index 7b417ae6..50f93c6a 100644 --- a/test/integration/standard_filter_test.rb +++ b/test/integration/standard_filter_test.rb @@ -149,6 +149,16 @@ class StandardFiltersTest < Test::Unit::TestCase "thing" => { "foo" => [ { "bar" => 42 }, { "bar" => 17 } ] } end + def test_flatten + assert_equal [1,2,3,4], @filters.flatten([1,2,3,4]) + assert_equal [1,2,3,4], @filters.flatten([[1,2,3,4]]) + assert_equal [1,2,3,4], @filters.flatten([[1],[2],[3],[4]]) + assert_equal [1], @filters.flatten(1) + + assert_template_result '1234', "{{ ary | flatten | flatten }}", + 'ary' => [[1],2,[3],4] + end + def test_sort_calls_to_liquid t = TestThing.new Liquid::Template.parse('{{ foo | sort: "whatever" }}').render("foo" => [t]) From b7b243a13dfe9abc3c68ac878a31a166d7c539c2 Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Tue, 7 Jan 2014 11:25:01 -0500 Subject: [PATCH 6/8] Fix regression on map --- lib/liquid/standardfilters.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/liquid/standardfilters.rb b/lib/liquid/standardfilters.rb index 7bd9cd86..5818acc7 100644 --- a/lib/liquid/standardfilters.rb +++ b/lib/liquid/standardfilters.rb @@ -309,8 +309,12 @@ module Liquid include Enumerable def initialize(input) - @input = - if input.kind_of?(Enumerable) + @input = + if input.is_a?(Array) + input.flatten + elsif input.is_a?(Hash) + [input] + elsif input.is_a?(Enumerable) input else Array(input) From 0b45ffeadabb6728ef98d772a5e2bbf85464b4e2 Mon Sep 17 00:00:00 2001 From: Florian Weingarten Date: Thu, 24 Jul 2014 00:33:39 +0000 Subject: [PATCH 7/8] add more legacy tests --- lib/liquid/standardfilters.rb | 31 +++++++++++++++--------- test/integration/standard_filter_test.rb | 14 +++++++++++ 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/lib/liquid/standardfilters.rb b/lib/liquid/standardfilters.rb index 5818acc7..7dd75d62 100644 --- a/lib/liquid/standardfilters.rb +++ b/lib/liquid/standardfilters.rb @@ -92,7 +92,7 @@ module Liquid # Join elements of the array with certain character between them def join(input, glue = ' '.freeze) - Array(input).join(glue) + InputIterator.new(input).join(glue) end # Sort elements of the array @@ -110,7 +110,7 @@ module Liquid # Reverse the elements of an array def reverse(input) - ary = Array(input) + ary = InputIterator.new(input) ary.reverse end @@ -309,16 +309,23 @@ module Liquid include Enumerable def initialize(input) - @input = - if input.is_a?(Array) - input.flatten - elsif input.is_a?(Hash) - [input] - elsif input.is_a?(Enumerable) - input - else - Array(input) - end + @input = if input.is_a?(Array) + input.flatten + elsif input.is_a?(Hash) + [input] + elsif input.is_a?(Enumerable) + input + else + Array(input) + end + end + + def join(glue) + to_a.join(glue) + end + + def reverse + reverse_each.to_a end def each diff --git a/test/integration/standard_filter_test.rb b/test/integration/standard_filter_test.rb index 50f93c6a..a869a0cd 100644 --- a/test/integration/standard_filter_test.rb +++ b/test/integration/standard_filter_test.rb @@ -117,6 +117,10 @@ class StandardFiltersTest < Test::Unit::TestCase assert_equal [{"a" => 1}, {"a" => 2}, {"a" => 3}, {"a" => 4}], @filters.sort([{"a" => 4}, {"a" => 3}, {"a" => 1}, {"a" => 2}], "a") end + def test_legacy_sort_hash + assert_equal [{a:1, b:2}], @filters.sort({a:1, b:2}) + end + def test_numerical_vs_lexicographical_sort assert_equal [2, 10], @filters.sort([10, 2]) assert_equal [{"a" => 2}, {"a" => 10}], @filters.sort([{"a" => 10}, {"a" => 2}], "a") @@ -128,6 +132,10 @@ class StandardFiltersTest < Test::Unit::TestCase assert_equal [4,3,2,1], @filters.reverse([1,2,3,4]) end + def test_legacy_reverse_hash + assert_equal [{a:1, b:2}], @filters.reverse(a:1, b:2) + end + def test_map assert_equal [1,2,3,4], @filters.map([{"a" => 1}, {"a" => 2}, {"a" => 3}, {"a" => 4}], 'a') assert_template_result 'abc', "{{ ary | map:'foo' | map:'bar' }}", @@ -149,6 +157,12 @@ class StandardFiltersTest < Test::Unit::TestCase "thing" => { "foo" => [ { "bar" => 42 }, { "bar" => 17 } ] } end + def test_legacy_map_on_hashes_with_dynamic_key + template = "{% assign key = 'foo' %}{{ thing | map: key | map: 'bar' }}" + hash = { "foo" => { "bar" => 42 } } + assert_template_result "42", template, "thing" => hash + end + def test_flatten assert_equal [1,2,3,4], @filters.flatten([1,2,3,4]) assert_equal [1,2,3,4], @filters.flatten([[1,2,3,4]]) From eae24373e644e0051467753f5e24dad4dc5e8be5 Mon Sep 17 00:00:00 2001 From: Florian Weingarten Date: Thu, 24 Jul 2014 02:56:57 +0000 Subject: [PATCH 8/8] remove unnecessary flatten filter --- lib/liquid/standardfilters.rb | 5 ----- test/integration/standard_filter_test.rb | 10 ---------- 2 files changed, 15 deletions(-) diff --git a/lib/liquid/standardfilters.rb b/lib/liquid/standardfilters.rb index 7dd75d62..0a956fbf 100644 --- a/lib/liquid/standardfilters.rb +++ b/lib/liquid/standardfilters.rb @@ -127,11 +127,6 @@ module Liquid end end - # flatten the given input - def flatten(input) - Array(input).flatten - end - # Replace occurrences of a string with another def replace(input, string, replacement = ''.freeze) input.to_s.gsub(string, replacement.to_s) diff --git a/test/integration/standard_filter_test.rb b/test/integration/standard_filter_test.rb index a869a0cd..dc223d53 100644 --- a/test/integration/standard_filter_test.rb +++ b/test/integration/standard_filter_test.rb @@ -163,16 +163,6 @@ class StandardFiltersTest < Test::Unit::TestCase assert_template_result "42", template, "thing" => hash end - def test_flatten - assert_equal [1,2,3,4], @filters.flatten([1,2,3,4]) - assert_equal [1,2,3,4], @filters.flatten([[1,2,3,4]]) - assert_equal [1,2,3,4], @filters.flatten([[1],[2],[3],[4]]) - assert_equal [1], @filters.flatten(1) - - assert_template_result '1234', "{{ ary | flatten | flatten }}", - 'ary' => [[1],2,[3],4] - end - def test_sort_calls_to_liquid t = TestThing.new Liquid::Template.parse('{{ foo | sort: "whatever" }}').render("foo" => [t])