ref: c1c8ecc9d6df31830a64618cb04f79ae34a3335a
parent: 091915c75d20c07384936fbc968e04f959ea7578
author: Hanchen Wang <[email protected]>
date: Wed May 11 06:09:43 EDT 2016
hugolib: Add GroupByExpireDate function
--- a/hugolib/pageGroup.go
+++ b/hugolib/pageGroup.go
@@ -253,6 +253,19 @@
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
--- a/hugolib/pageGroup_test.go
+++ b/hugolib/pageGroup_test.go
@@ -47,6 +47,7 @@
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)
@@ -366,6 +367,23 @@
}
if groups != nil {
t.Errorf("PagesGroup isn't empty. It should be %#v, got %#v", nil, groups)
+ }
+}
+
+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)
}
}