Skip to content

Instantly share code, notes, and snippets.

@Jetroid
Created July 29, 2016 13:31
Show Gist options
  • Save Jetroid/31949b08e4aa1c2c811bdf70f5493a57 to your computer and use it in GitHub Desktop.
Save Jetroid/31949b08e4aa1c2c811bdf70f5493a57 to your computer and use it in GitHub Desktop.
A plugin to allow union of arrays with Jekyll.
module Jekyll
module UnionFilter
def union(input, array)
unless array.respond_to?(:to_ary)
array = Array.new
end
InputIterator.new(input).concat(array).uniq
end
class InputIterator
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
end
def concat(args)
to_a.concat(args)
end
def each
@input.each do |e|
yield(e.respond_to?(:to_liquid) ? e.to_liquid : e)
end
end
end
end
end
Liquid::Template.register_filter(Jekyll::UnionFilter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment