Skip to content

Commit

Permalink
checklist: add Limit and Len methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ernado committed Oct 6, 2018
1 parent 630b149 commit 75e8285
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
14 changes: 14 additions & 0 deletions checklist.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,17 @@ func (c *Checklist) Prune() {
}
c.Pairs = result
}

// Limit ensures maximum length of pairs, removing the pairs with least priority
// if needed.
func (c *Checklist) Limit(max int) {
if len(c.Pairs) <= max {
return
}
c.Pairs = c.Pairs[:max]
}

// Len returns pairs count.
func (c *Checklist) Len() int {
return len(c.Pairs)
}
30 changes: 30 additions & 0 deletions checklist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,33 @@ func TestChecklist_Prune(t *testing.T) {
t.Error("unexpected result length")
}
}

func TestChecklist_Limit(t *testing.T) {
c := Checklist{
Pairs: Pairs{
{
Priority: 100,
},
{
Priority: 99,
},
{
Priority: 98,
},
{
Priority: 97,
},
{
Priority: 96,
},
},
}
c.Limit(10)
if c.Len() != 5 {
t.Error("unexpected length")
}
c.Limit(3)
if c.Len() != 3 {
t.Error("unexpected length ")
}
}

0 comments on commit 75e8285

Please sign in to comment.