mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-18 10:20:43 -07:00
Merge pull request #600 from carsonreinke/filter-compact
Merge pull request 600
This commit is contained in:
@@ -10,6 +10,9 @@ Metrics/BlockNesting:
|
|||||||
Exclude:
|
Exclude:
|
||||||
- 'lib/liquid/block_body.rb'
|
- 'lib/liquid/block_body.rb'
|
||||||
|
|
||||||
|
Metrics/ModuleLength:
|
||||||
|
Enabled: false
|
||||||
|
|
||||||
Lint/AssignmentInCondition:
|
Lint/AssignmentInCondition:
|
||||||
Enabled: false
|
Enabled: false
|
||||||
|
|
||||||
|
|||||||
@@ -168,6 +168,20 @@ module Liquid
|
|||||||
end
|
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
|
# Replace occurrences of a string with another
|
||||||
def replace(input, string, replacement = ''.freeze)
|
def replace(input, string, replacement = ''.freeze)
|
||||||
input.to_s.gsub(string.to_s, replacement.to_s)
|
input.to_s.gsub(string.to_s, replacement.to_s)
|
||||||
@@ -399,6 +413,10 @@ module Liquid
|
|||||||
to_a.uniq(&block)
|
to_a.uniq(&block)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def compact
|
||||||
|
to_a.compact
|
||||||
|
end
|
||||||
|
|
||||||
def each
|
def each
|
||||||
@input.each do |e|
|
@input.each do |e|
|
||||||
yield(e.respond_to?(:to_liquid) ? e.to_liquid : e)
|
yield(e.respond_to?(:to_liquid) ? e.to_liquid : e)
|
||||||
|
|||||||
@@ -104,6 +104,27 @@ class FiltersTest < Minitest::Test
|
|||||||
assert_equal sorted[2].a, 'C'
|
assert_equal sorted[2].a, 'C'
|
||||||
end
|
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
|
def test_strip_html
|
||||||
@context['var'] = "<b>bla blub</a>"
|
@context['var'] = "<b>bla blub</a>"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user