aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGrant Ammons <gammons@gmail.com>2016-05-02 08:09:48 -0400
committerGrant Ammons <gammons@gmail.com>2016-05-02 08:09:48 -0400
commit471b93141074222bdef537f08b6443e91fa0ea2f (patch)
tree5f431835ee8c31d8625328643ad6ab5421fd57c7
parentb91b9d073680a0371af825238af230e9618aea12 (diff)
Create filter, by archived or unarchived
-rw-r--r--todolist/app.go12
-rw-r--r--todolist/filter.go45
-rw-r--r--todolist/filter_test.go47
3 files changed, 98 insertions, 6 deletions
diff --git a/todolist/app.go b/todolist/app.go
index 598612c..2af3361 100644
--- a/todolist/app.go
+++ b/todolist/app.go
@@ -81,8 +81,8 @@ func (a *App) UnarchiveTodo(input string) {
}
func (a *App) ListTodos(input string) {
- //filtered := NewFilter(a.TodoStore.Todos()).filter()
- grouped := a.getGroups(input)
+ filtered := NewFilter(a.TodoStore.Todos()).Filter(input)
+ grouped := a.getGroups(input, filtered)
formatter := NewFormatter(grouped)
formatter.Print()
@@ -99,7 +99,7 @@ func (a *App) getId(input string) int {
}
}
-func (a *App) getGroups(input string) *GroupedTodos {
+func (a *App) getGroups(input string, todos []Todo) *GroupedTodos {
grouper := &Grouper{}
contextRegex, _ := regexp.Compile(`by c.*$`)
projectRegex, _ := regexp.Compile(`by p.*$`)
@@ -107,11 +107,11 @@ func (a *App) getGroups(input string) *GroupedTodos {
var grouped *GroupedTodos
if contextRegex.MatchString(input) {
- grouped = grouper.GroupByContext(a.TodoStore.Todos())
+ grouped = grouper.GroupByContext(todos)
} else if projectRegex.MatchString(input) {
- grouped = grouper.GroupByContext(a.TodoStore.Todos())
+ grouped = grouper.GroupByContext(todos)
} else {
- grouped = grouper.GroupByNothing(a.TodoStore.Todos())
+ grouped = grouper.GroupByNothing(todos)
}
return grouped
}
diff --git a/todolist/filter.go b/todolist/filter.go
new file mode 100644
index 0000000..eb4dbd7
--- /dev/null
+++ b/todolist/filter.go
@@ -0,0 +1,45 @@
+package todolist
+
+import "regexp"
+
+type TodoFilter struct {
+ Todos []Todo
+}
+
+func NewFilter(todos []Todo) *TodoFilter {
+ return &TodoFilter{Todos: todos}
+}
+
+func (f *TodoFilter) Filter(input string) []Todo {
+ f.Todos = f.filterArchived(input)
+ return f.Todos
+}
+
+func (f *TodoFilter) filterArchived(input string) []Todo {
+ r, _ := regexp.Compile(`l archived$`)
+ if r.MatchString(input) {
+ return f.getArchived()
+ } else {
+ return f.getUnarchived()
+ }
+}
+
+func (f *TodoFilter) getArchived() []Todo {
+ var ret []Todo
+ for _, todo := range f.Todos {
+ if todo.Archived == true {
+ ret = append(ret, todo)
+ }
+ }
+ return ret
+}
+
+func (f *TodoFilter) getUnarchived() []Todo {
+ var ret []Todo
+ for _, todo := range f.Todos {
+ if todo.Archived == false {
+ ret = append(ret, todo)
+ }
+ }
+ return ret
+}
diff --git a/todolist/filter_test.go b/todolist/filter_test.go
new file mode 100644
index 0000000..13e2b20
--- /dev/null
+++ b/todolist/filter_test.go
@@ -0,0 +1,47 @@
+package todolist
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestFilterArchived(t *testing.T) {
+ assert := assert.New(t)
+ store := &FileStore{FileLocation: "todos.json"}
+ store.Load()
+ filter := NewFilter(store.Todos())
+ archived := filter.filterArchived("l archived")
+ assert.Equal(1, len(archived))
+ assert.Equal(true, archived[0].Archived)
+}
+
+func TestFilterUnarchivedByDefault(t *testing.T) {
+ assert := assert.New(t)
+ store := &FileStore{FileLocation: "todos.json"}
+ store.Load()
+ filter := NewFilter(store.Todos())
+ unarchived := filter.filterArchived("l")
+ assert.Equal(1, len(unarchived))
+ assert.Equal(false, unarchived[0].Archived)
+}
+
+func TestGetArchived(t *testing.T) {
+ assert := assert.New(t)
+ store := &FileStore{FileLocation: "todos.json"}
+ store.Load()
+ filter := NewFilter(store.Todos())
+ archived := filter.getArchived()
+ assert.Equal(1, len(archived))
+ assert.Equal(true, archived[0].Archived)
+}
+
+func TestGetUnarchived(t *testing.T) {
+ assert := assert.New(t)
+ store := &FileStore{FileLocation: "todos.json"}
+ store.Load()
+ filter := NewFilter(store.Todos())
+ unarchived := filter.getUnarchived()
+ assert.Equal(1, len(unarchived))
+ assert.Equal(false, unarchived[0].Archived)
+}