-
Notifications
You must be signed in to change notification settings - Fork 0
/
item_collection.go
77 lines (66 loc) · 1.95 KB
/
item_collection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package entities
import (
"time"
)
type ItemCollection []Manageable
type ItemPredicate func(item Manageable) bool
// Sort Protocol
func (c ItemCollection) Len() int { return len(c) }
func (c ItemCollection) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
func (c ItemCollection) Less(i, j int) bool { return c[i].GetId() < c[j].GetId() }
func (c ItemCollection) Filter(predicate ItemPredicate) ItemCollection {
result := make(ItemCollection, 0, len(c))
for _, item := range c {
if predicate(item) {
result = append(result, item)
}
}
return result
}
func (c ItemCollection) Exclude(predicate ItemPredicate) ItemCollection {
result := make(ItemCollection, 0, len(c))
for _, item := range c {
if !predicate(item) {
result = append(result, item)
}
}
return result
}
func (c ItemCollection) FilterByStatus(status string) ItemCollection {
return c.Filter(func(item Manageable) bool {
return item.GetStatus() == status
})
}
func (c ItemCollection) FilterPending() ItemCollection {
return c.Filter(func(item Manageable) bool {
return IsPending(item.GetStatus())
})
}
func (c ItemCollection) FilterByTag(tag string) ItemCollection {
return c.Filter(func(item Manageable) bool {
return item.HasTag(tag)
})
}
func (c ItemCollection) FilterByTags(tags ...string) ItemCollection {
return c.Filter(func(item Manageable) bool {
return item.HasAnyTag(tags...)
})
}
func (c ItemCollection) FilterByType(kind string) ItemCollection {
return c.Filter(func(item Manageable) bool {
return item.GetType() == kind
})
}
func (c ItemCollection) FilterByCreationDate(fromDate, toDate *time.Time) ItemCollection {
return c.Filter(func(item Manageable) bool {
createdAt := item.GetCreationDateTime()
if fromDate != nil && toDate != nil {
return fromDate.Before(createdAt) && toDate.After(createdAt)
} else if fromDate != nil {
return fromDate.Before(createdAt)
} else if toDate != nil {
return toDate.After(createdAt)
}
return true
})
}