hugolib: Add GroupByExpireDate function

This commit is contained in:
Hanchen Wang 2016-05-11 10:09:43 -04:00 committed by Bjørn Erik Pedersen
parent 091915c75d
commit c1c8ecc9d6
2 changed files with 31 additions and 0 deletions

View file

@ -253,6 +253,19 @@ func (p Pages) GroupByPublishDate(format string, order ...string) (PagesGroup, e
return p.groupByDateField(sorter, formatter, order...)
}
// GroupByExpireDate groups by the given page's ExpireDate value in the given format and with the given order.
// Valid values for order is asc, desc, rev and reverse.
// For valid format strings, see https://golang.org/pkg/time/#Time.Format
func (p Pages) GroupByExpiryDate(format string, order ...string) (PagesGroup, error) {
sorter := func(p Pages) Pages {
return p.ByExpiryDate()
}
formatter := func(p *Page) string {
return p.ExpiryDate.Format(format)
}
return p.groupByDateField(sorter, formatter, order...)
}
// GroupByParamDate groups by a date set as a param on the page in the given format and with the given order.
// Valid values for order is asc, desc, rev and reverse.
// For valid format strings, see https://golang.org/pkg/time/#Time.Format

View file

@ -47,6 +47,7 @@ func preparePageGroupTestPages(t *testing.T) Pages {
p.Weight = s.weight
p.Date = cast.ToTime(s.date)
p.PublishDate = cast.ToTime(s.date)
p.ExpiryDate = cast.ToTime(s.date)
p.Params["custom_param"] = s.param
p.Params["custom_date"] = cast.ToTime(s.date)
pages = append(pages, p)
@ -369,6 +370,23 @@ func TestGroupByPublishDateWithEmptyPages(t *testing.T) {
}
}
func TestGroupByExpiryDate(t *testing.T) {
pages := preparePageGroupTestPages(t)
expect := PagesGroup{
{Key: "2012-04", Pages: Pages{pages[4], pages[2], pages[0]}},
{Key: "2012-03", Pages: Pages{pages[3]}},
{Key: "2012-01", Pages: Pages{pages[1]}},
}
groups, err := pages.GroupByExpiryDate("2006-01")
if err != nil {
t.Fatalf("Unable to make PagesGroup array: %s", err)
}
if !reflect.DeepEqual(groups, expect) {
t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
}
}
func TestGroupByParamDate(t *testing.T) {
pages := preparePageGroupTestPages(t)
expect := PagesGroup{