Skip to content

Commit

Permalink
add odd? and even? to the loop tag (#232)
Browse files Browse the repository at this point in the history
* add `odd?` and `even?` to the loop tag

* use crystal native odd/even. update spec
  • Loading branch information
kates committed May 24, 2024
1 parent 10a5ee6 commit d82515a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
40 changes: 40 additions & 0 deletions spec/marten/template/tag/for/loop_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,44 @@ describe Marten::Template::Tag::For::Loop do
loop.revindex0.should eq 8
end
end

describe "#odd?" do
it "returns true if the index is odd" do
loop = Marten::Template::Tag::For::Loop.new(items_size: 10)
loop.index = 1
loop.odd?.should eq true

loop.index = 3
loop.odd?.should eq true
end

it "return false if the index is even" do
loop = Marten::Template::Tag::For::Loop.new(items_size: 10)
loop.index = 2
loop.odd?.should eq false

loop.index = 4
loop.odd?.should eq false
end
end

describe "#even?" do
it "returns true if the index is even" do
loop = Marten::Template::Tag::For::Loop.new(items_size: 10)
loop.index = 0
loop.even?.should eq true

loop.index = 2
loop.even?.should eq true
end

it "return false if the index is odd" do
loop = Marten::Template::Tag::For::Loop.new(items_size: 10)
loop.index = 1
loop.even?.should eq false

loop.index = 3
loop.even?.should eq false
end
end
end
8 changes: 8 additions & 0 deletions src/marten/template/tag/for/loop.cr
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ module Marten
def revindex0
@items_size - @index - 1
end

def odd?
@index.odd?
end

def even?
@index.even?
end
end
end
end
Expand Down

0 comments on commit d82515a

Please sign in to comment.