From 1e23036b2d1e71009735f7b012743804e13822ca Mon Sep 17 00:00:00 2001 From: Florian Weingarten Date: Thu, 4 Jun 2015 22:54:19 -0400 Subject: [PATCH] Fix uniq filter with string input --- .rubocop_todo.yml | 8 ++++---- lib/liquid/standardfilters.rb | 12 +++++++++--- test/integration/standard_filter_test.rb | 1 + 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 451f50b..16c023f 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,11 +1,11 @@ # This configuration was generated by `rubocop --auto-gen-config` -# on 2015-06-04 13:07:29 -0400 using RuboCop version 0.31.0. +# on 2015-06-04 22:53:26 -0400 using RuboCop version 0.31.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 51 +# Offense count: 53 Metrics/AbcSize: Max: 58 @@ -18,7 +18,7 @@ Metrics/ClassLength: Metrics/CyclomaticComplexity: Max: 15 -# Offense count: 550 +# Offense count: 552 # Configuration parameters: AllowURI, URISchemes. Metrics/LineLength: Max: 294 @@ -31,7 +31,7 @@ Metrics/MethodLength: # Offense count: 1 # Configuration parameters: CountComments. Metrics/ModuleLength: - Max: 234 + Max: 235 # Offense count: 6 Metrics/PerceivedComplexity: diff --git a/lib/liquid/standardfilters.rb b/lib/liquid/standardfilters.rb index 7aab603..1f5a97c 100644 --- a/lib/liquid/standardfilters.rb +++ b/lib/liquid/standardfilters.rb @@ -138,10 +138,12 @@ module Liquid # Remove duplicate elements from an array # provide optional property with which to determine uniqueness def uniq(input, property = nil) + ary = InputIterator.new(input) + if property.nil? - input.uniq - elsif input.first.respond_to?(:[]) - input.uniq{ |a| a[property] } + ary.uniq + elsif ary.first.respond_to?(:[]) + ary.uniq{ |a| a[property] } end end @@ -391,6 +393,10 @@ module Liquid reverse_each.to_a end + def uniq(&block) + to_a.uniq(&block) + end + def each @input.each do |e| yield(e.respond_to?(:to_liquid) ? e.to_liquid : e) diff --git a/test/integration/standard_filter_test.rb b/test/integration/standard_filter_test.rb index 546a348..52954e2 100644 --- a/test/integration/standard_filter_test.rb +++ b/test/integration/standard_filter_test.rb @@ -170,6 +170,7 @@ class StandardFiltersTest < Minitest::Test end def test_uniq + assert_equal ["foo"], @filters.uniq("foo") assert_equal [1, 3, 2, 4], @filters.uniq([1, 1, 3, 2, 3, 1, 4, 3, 2, 1]) assert_equal [{ "a" => 1 }, { "a" => 3 }, { "a" => 2 }], @filters.uniq([{ "a" => 1 }, { "a" => 3 }, { "a" => 1 }, { "a" => 2 }], "a") testdrop = TestDrop.new