diff --git a/lib/jekyll/filters.rb b/lib/jekyll/filters.rb index 6ee1b524..67d74a02 100644 --- a/lib/jekyll/filters.rb +++ b/lib/jekyll/filters.rb @@ -158,7 +158,6 @@ module Jekyll input.to_json end - # Group an array of items by a property # # input - the inputted Enumerable @@ -179,6 +178,18 @@ module Jekyll end end + # Filter an array of objects + # + # input - the object array + # key - key within each object to filter by + # value - desired value + # + # Returns the filtered array of objects + def where(input, key, value) + return input unless input.class == Array + input.select { |object| object[key] == value } + end + private def time(input) case input diff --git a/test/test_filters.rb b/test/test_filters.rb index 42b0a030..411ceed3 100644 --- a/test/test_filters.rb +++ b/test/test_filters.rb @@ -16,6 +16,11 @@ class TestFilters < Test::Unit::TestCase @filter = JekyllFilter.new({"source" => source_dir, "destination" => dest_dir}) @sample_time = Time.utc(2013, 03, 27, 11, 22, 33) @time_as_string = "September 11, 2001 12:46:30 -0000" + @array_of_objects = [ + { "color" => "red", "size" => "large" }, + { "color" => "red", "size" => "medium" }, + { "color" => "blue", "size" => "medium" } + ] end should "textilize with simple string" do @@ -109,6 +114,11 @@ class TestFilters < Test::Unit::TestCase assert_equal "[1,2]", @filter.jsonify([1, 2]) assert_equal "[{\"name\":\"Jack\"},{\"name\":\"Smith\"}]", @filter.jsonify([{:name => 'Jack'}, {:name => 'Smith'}]) end + + should "proper filter objects using where" do + assert_equal "some string", @filter.where("some string", nil, nil) + assert_equal 2, @filter.where(@array_of_objects, "color", "red").length + end end context "group_by filter" do