aboutsummaryrefslogtreecommitdiffstats
path: root/todolist/grouper.go
diff options
context:
space:
mode:
authorGrant Ammons <gammons@gmail.com>2016-04-25 14:35:43 -0400
committerGrant Ammons <gammons@gmail.com>2016-04-25 14:35:43 -0400
commita0f7203996439210b2a40663eeeeb8563ab03d68 (patch)
tree84f7bd055f5cc581fa2b4894206868c998512d1a /todolist/grouper.go
parentdde4f5330c2126af0e3ec17e4098653c6f551426 (diff)
Get grouping working correctly
Diffstat (limited to 'todolist/grouper.go')
-rw-r--r--todolist/grouper.go63
1 files changed, 63 insertions, 0 deletions
diff --git a/todolist/grouper.go b/todolist/grouper.go
new file mode 100644
index 0000000..25547bf
--- /dev/null
+++ b/todolist/grouper.go
@@ -0,0 +1,63 @@
+package todolist
+
+type Grouper struct{}
+
+type GroupedTodos struct {
+ Groups map[string][]Todo
+}
+
+func (g *Grouper) GroupByContext(todos []Todo) *GroupedTodos {
+ groups := map[string][]Todo{}
+
+ allContexts := []string{}
+
+ for _, todo := range todos {
+ allContexts = addIfNotThere(allContexts, todo.Contexts)
+ }
+
+ for _, todo := range todos {
+ for _, context := range todo.Contexts {
+ groups[context] = append(groups[context], todo)
+ }
+ }
+
+ return &GroupedTodos{Groups: groups}
+}
+
+func (g *Grouper) GroupByProject(todos []Todo) *GroupedTodos {
+ groups := map[string][]Todo{}
+
+ allProjects := []string{}
+
+ for _, todo := range todos {
+ allProjects = addIfNotThere(allProjects, todo.Projects)
+ }
+
+ for _, todo := range todos {
+ for _, project := range todo.Projects {
+ groups[project] = append(groups[project], todo)
+ }
+ }
+ return &GroupedTodos{Groups: groups}
+}
+
+func (g *Grouper) GroupByNothing(todos []Todo) *GroupedTodos {
+ groups := map[string][]Todo{}
+ groups["all"] = todos
+ return &GroupedTodos{Groups: groups}
+}
+
+func addIfNotThere(arr []string, items []string) []string {
+ for _, item := range items {
+ there := false
+ for _, arrItem := range arr {
+ if item == arrItem {
+ there = true
+ }
+ }
+ if !there {
+ arr = append(arr, item)
+ }
+ }
+ return arr
+}