diff --git a/lib/locomotive/liquid/filters/misc.rb b/lib/locomotive/liquid/filters/misc.rb index f55dcf9a..a1c257df 100644 --- a/lib/locomotive/liquid/filters/misc.rb +++ b/lib/locomotive/liquid/filters/misc.rb @@ -33,6 +33,10 @@ module Locomotive input.last end + def default(input, value) + input.blank? ? value : input + end + end ::Liquid::Template.register_filter(Misc) diff --git a/spec/lib/locomotive/liquid/filters/misc_spec.rb b/spec/lib/locomotive/liquid/filters/misc_spec.rb index b2815d05..d8fcca49 100644 --- a/spec/lib/locomotive/liquid/filters/misc_spec.rb +++ b/spec/lib/locomotive/liquid/filters/misc_spec.rb @@ -4,7 +4,7 @@ describe Locomotive::Liquid::Filters::Misc do include Locomotive::Liquid::Filters::Misc - it 'should underscore an input' do + it 'underscores an input' do underscore('foo').should == 'foo' underscore('home page').should == 'home_page' underscore('My foo Bar').should == 'my_foo_bar' @@ -12,19 +12,19 @@ describe Locomotive::Liquid::Filters::Misc do underscore('foo/bar/index').should == 'foo_bar_index' end - it 'should dasherize an input' do + it 'dasherizes an input' do dasherize('foo').should == 'foo' dasherize('foo_bar').should == 'foo-bar' dasherize('foo/bar').should == 'foo-bar' dasherize('foo/bar/index').should == 'foo-bar-index' end - it 'should concat strings' do + it 'concats strings' do concat('foo', 'bar').should == 'foobar' concat('hello', 'foo', 'bar').should == 'hellofoobar' end - it 'should return the input string every n occurences' do + it 'returns the input string every n occurences' do modulo('foo', 0, 3).should == '' modulo('foo', 1, 3).should == '' modulo('foo', 2, 3).should == 'foo' @@ -33,4 +33,10 @@ describe Locomotive::Liquid::Filters::Misc do modulo('foo', 5, 3).should == 'foo' end + it 'returns default values if the input is empty' do + default('foo', 42).should == 'foo' + default('', 42).should == 42 + default(nil, 42).should == 42 + end + end