aboutsummaryrefslogtreecommitdiffstats
path: root/todolist/grouper.go
blob: 25547bf9ebb417561e2658c12bfad2f8cd6032d7 (plain)
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
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
}