-
Notifications
You must be signed in to change notification settings - Fork 8
/
filter.go
95 lines (81 loc) · 2.35 KB
/
filter.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package todotxt
import "strings"
// Predicate is a function that takes a task as input and returns a bool.
type Predicate func(Task) bool
// Filter filters the current TaskList for the given predicate, and returns a new TaskList. The original TaskList is not modified.
func (tasklist TaskList) Filter(predicate Predicate, predicates ...Predicate) TaskList {
combined := []Predicate{predicate}
combined = append(combined, predicates...)
var newList TaskList
for _, t := range tasklist {
for _, p := range combined {
if p(t) {
newList = append(newList, t)
break
}
}
}
return newList
}
// FilterNot returns a reversed filter for existing predicate.
func FilterNot(predicate Predicate) Predicate {
return func(t Task) bool {
return !predicate(t)
}
}
// FilterCompleted filters completed tasks.
func FilterCompleted(t Task) bool {
return t.Completed
}
// FilterNotCompleted filters tasks that are not completed.
func FilterNotCompleted(t Task) bool {
return !t.Completed
}
// FilterDueToday filters tasks that are due today.
func FilterDueToday(t Task) bool {
return t.IsDueToday()
}
// FilterOverdue filters tasks that are overdue.
func FilterOverdue(t Task) bool {
return t.IsOverdue()
}
// FilterHasDueDate filters tasks that have due date.
func FilterHasDueDate(t Task) bool {
return t.HasDueDate()
}
// FilterHasPriority filters tasks that have priority.
func FilterHasPriority(t Task) bool {
return t.HasPriority()
}
// FilterByPriority returns a filter for tasks that have the given priority.
// String comparison in the filters is case-insensitive.
func FilterByPriority(priority string) Predicate {
priority = strings.ToUpper(priority)
return func(t Task) bool {
return t.Priority == priority
}
}
// FilterByProject returns a filter for tasks that have the given project.
// String comparison in the filters is case-insensitive.
func FilterByProject(project string) Predicate {
return func(t Task) bool {
for _, p := range t.Projects {
if strings.EqualFold(p, project) {
return true
}
}
return false
}
}
// FilterByContext returns a filter for tasks that have the given context.
// String comparison in the filters is case-insensitive.
func FilterByContext(context string) Predicate {
return func(t Task) bool {
for _, c := range t.Contexts {
if strings.EqualFold(c, context) {
return true
}
}
return false
}
}