new liquid filter: default

This commit is contained in:
Didier Lafforgue 2012-03-26 18:12:16 +02:00
parent 90e1c4f438
commit 2329f26e4d
2 changed files with 14 additions and 4 deletions

View File

@ -33,6 +33,10 @@ module Locomotive
input.last
end
def default(input, value)
input.blank? ? value : input
end
end
::Liquid::Template.register_filter(Misc)

View File

@ -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