new liquid filter: shuffle (randomly sort an array)

did committed Feb 16, 2016
commit 19d1978dc6a3f1787f4a7ca0753adc4e7a34a0c6
Showing 2 changed files with 17 additions and 0 deletions
locomotive/steam/liquid/filters/misc.rb b/lib/locomotive/steam/liquid/filters/misc.rb +4 -0
@@ @@ -14,6 +14,10 @@ module Locomotive
array.at(position) if array.respond_to?(:at)
end
+ def shuffle(array)
+ array.to_a.shuffle
+ end
+
def default(input, value)
input.blank? ? value : input
end
spec/unit/liquid/filters/misc_spec.rb +13 -0
@@ @@ -119,4 +119,17 @@ describe Locomotive::Steam::Liquid::Filters::Misc do
end
+ describe '#shuffle' do
+
+ let(:array) { [1, 2, 3, 4] }
+
+ subject { shuffle(array) }
+
+ it 'returns an array in a random order' do
+ expect(subject.size).to eq 4
+ expect(subject).not_to eq([1, 2, 3, 4])
+ end
+
+ end
+
end