diff --git a/lib/jekyll/filters.rb b/lib/jekyll/filters.rb index bcdf80ff..d6f0fa03 100644 --- a/lib/jekyll/filters.rb +++ b/lib/jekyll/filters.rb @@ -259,6 +259,34 @@ module Jekyll end end + def pop(array, input = 1) + return array unless array.is_a?(Array) + new_ary = array.dup + new_ary.pop(input.to_i || 1) + new_ary + end + + def push(array, input) + return array unless array.is_a?(Array) + new_ary = array.dup + new_ary.push(input) + new_ary + end + + def shift(array, input = 1) + return array unless array.is_a?(Array) + new_ary = array.dup + new_ary.shift(input.to_i || 1) + new_ary + end + + def unshift(array, input) + return array unless array.is_a?(Array) + new_ary = array.dup + new_ary.unshift(input) + new_ary + end + # Convert an object into its String representation for debugging # # input - The Object to be converted diff --git a/test/test_filters.rb b/test/test_filters.rb index e4f1adf3..35ea6f0b 100644 --- a/test/test_filters.rb +++ b/test/test_filters.rb @@ -8,7 +8,7 @@ class TestFilters < Test::Unit::TestCase attr_accessor :site, :context def initialize(opts = {}) - @site = Jekyll::Site.new(Jekyll.configuration(opts)) + @site = Jekyll::Site.new(Jekyll.configuration(opts.merge('skip_config_files' => true))) @context = Liquid::Context.new({}, {}, { :site => @site }) end end @@ -223,5 +223,45 @@ class TestFilters < Test::Unit::TestCase end end + context "push filter" do + should "return a new array with the element pushed to the end" do + assert_equal %w{hi there bernie}, @filter.push(%w{hi there}, "bernie") + end + end + + context "pop filter" do + should "return a new array with the last element popped" do + assert_equal %w{hi there}, @filter.pop(%w{hi there bernie}) + end + + should "allow multiple els to be popped" do + assert_equal %w{hi there bert}, @filter.pop(%w{hi there bert and ernie}, 2) + end + + should "cast string inputs for # into nums" do + assert_equal %w{hi there bert}, @filter.pop(%w{hi there bert and ernie}, "2") + end + end + + context "shift filter" do + should "return a new array with the element removed from the front" do + assert_equal %w{a friendly greeting}, @filter.shift(%w{just a friendly greeting}) + end + + should "allow multiple els to be shifted" do + assert_equal %w{bert and ernie}, @filter.shift(%w{hi there bert and ernie}, 2) + end + + should "cast string inputs for # into nums" do + assert_equal %w{bert and ernie}, @filter.shift(%w{hi there bert and ernie}, "2") + end + end + + context "unshift filter" do + should "return a new array with the element put at the front" do + assert_equal %w{aloha there bernie}, @filter.unshift(%w{there bernie}, "aloha") + end + end + end end