Merge pull request #600 from carsonreinke/filter-compact

Merge pull request 600
This commit is contained in:
Justin Li
2015-06-19 11:38:14 -04:00
3 changed files with 42 additions and 0 deletions

View File

@@ -168,6 +168,20 @@ module Liquid
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)
input.to_s.gsub(string.to_s, replacement.to_s)
@@ -399,6 +413,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)