Skip to content

Commit

Permalink
tpl: Last now accepts 0 as limit
Browse files Browse the repository at this point in the history
Modified the if conditional because of which last threw an error if 0 was passed as limit. The function now returns an empty slice if it is called with 0 as limit. The behavior of first and last is now the same when 0 is passed as limit. Also added tests to test the new behavior.

Fixes gohugoio#6419
  • Loading branch information
BaibhaVatsa authored and bep committed Oct 11, 2019
1 parent 5f1aafa commit 0e75af7
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tpl/collections/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ func (ns *Namespace) Last(limit interface{}, seq interface{}) (interface{}, erro
return nil, err
}

if limitv < 1 {
if limitv < 0 {
return nil, errors.New("can't return negative/empty count of items from sequence")
}

Expand Down
2 changes: 2 additions & 0 deletions tpl/collections/collections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,8 @@ func TestLast(t *testing.T) {
{int64(2), []int{100, 200, 300}, []int{200, 300}},
{100, []int{100, 200}, []int{100, 200}},
{"1", []int{100, 200, 300}, []int{300}},
{"0", []int{100, 200, 300}, []int{}},
{"0", []string{"a", "b", "c"}, []string{}},
// errors
{int64(-1), []int{100, 200, 300}, false},
{"noint", []int{100, 200, 300}, false},
Expand Down

0 comments on commit 0e75af7

Please sign in to comment.