From 838509996072716c75ec03ac62f0a0005ea3267b Mon Sep 17 00:00:00 2001 From: Carson Reinke Date: Mon, 15 Jun 2015 14:14:28 -0400 Subject: [PATCH 1/3] Added "compact" filter --- lib/liquid/standardfilters.rb | 18 ++++++++++++++++++ test/integration/filter_test.rb | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/lib/liquid/standardfilters.rb b/lib/liquid/standardfilters.rb index 1f5a97c..2340a74 100644 --- a/lib/liquid/standardfilters.rb +++ b/lib/liquid/standardfilters.rb @@ -165,6 +165,20 @@ module Liquid end end end + + # Remove nils within an array + # provide optional property with which to check for nil + def compact(input, property = nil) + ary = InputIterator.new(input) + + if property.nil? + ary.compact + elsif ary.first.respond_to?(:[]) + ary.reject{ |a| a[property].nil? } + elsif ary.first.respond_to?(property) + ary.reject { |a| a.send(property).nil? } + end + end # Replace occurrences of a string with another def replace(input, string, replacement = ''.freeze) @@ -397,6 +411,10 @@ module Liquid to_a.uniq(&block) end + def compact + to_a.compact + end + def each @input.each do |e| yield(e.respond_to?(:to_liquid) ? e.to_liquid : e) diff --git a/test/integration/filter_test.rb b/test/integration/filter_test.rb index cf407bf..1e904ab 100644 --- a/test/integration/filter_test.rb +++ b/test/integration/filter_test.rb @@ -104,6 +104,27 @@ class FiltersTest < Minitest::Test assert_equal sorted[2].a, 'C' end + def test_compact + @context['words'] = ['a', nil, 'b', nil, 'c'] + @context['hashes'] = [{ 'a' => 'A' }, { 'a' => nil }, { 'a' => 'C' }] + @context['objects'] = [TestObject.new('A'), TestObject.new(nil), TestObject.new('C')] + + # Test strings + assert_equal ['a', 'b', 'c'], Variable.new("words | compact").render(@context) + + # Test hashes + sorted = Variable.new("hashes | compact: 'a'").render(@context) + assert_equal sorted[0]['a'], 'A' + assert_equal sorted[1]['a'], 'C' + assert_nil sorted[2] + + # Test objects + sorted = Variable.new("objects | compact: 'a'").render(@context) + assert_equal sorted[0].a, 'A' + assert_equal sorted[1].a, 'C' + assert_nil sorted[2] + end + def test_strip_html @context['var'] = "bla blub" From 5fbb312a67e1276f817cebad6885d00ca658e47f Mon Sep 17 00:00:00 2001 From: Carson Reinke Date: Mon, 15 Jun 2015 14:27:48 -0400 Subject: [PATCH 2/3] "Trailing whitespace detected." --- 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 2340a74..d4a31f3 100644 --- a/lib/liquid/standardfilters.rb +++ b/lib/liquid/standardfilters.rb @@ -165,7 +165,7 @@ module Liquid end end end - + # Remove nils within an array # provide optional property with which to check for nil def compact(input, property = nil) From 7c5d54aced95b6026b611ddb5364b8d27322c448 Mon Sep 17 00:00:00 2001 From: Carson Reinke Date: Mon, 15 Jun 2015 15:07:25 -0400 Subject: [PATCH 3/3] Ignore Rubocop Metrics/ModuleLength for now --- .rubocop.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.rubocop.yml b/.rubocop.yml index 914d53b..431a9d9 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -10,6 +10,9 @@ Metrics/BlockNesting: Exclude: - 'lib/liquid/block_body.rb' +Metrics/ModuleLength: + Enabled: false + Lint/AssignmentInCondition: Enabled: false