use pttodo.Todos.Like...()
This commit is contained in:
36
pttodo/todos.go
Normal file
36
pttodo/todos.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package pttodo
|
||||
|
||||
import "strings"
|
||||
|
||||
type Todos []Todo
|
||||
|
||||
func (todos Todos) LikeSearch(search string) Todos {
|
||||
return todos.Like(func(todo Todo) bool {
|
||||
return strings.Contains(
|
||||
strings.ToLower(todo.Todo),
|
||||
strings.ToLower(search),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
func (todos Todos) LikeTags(tags []string) Todos {
|
||||
return todos.Like(func(todo Todo) bool {
|
||||
matches := true
|
||||
for _, tag := range tags {
|
||||
str := strings.TrimLeft(tag, "-")
|
||||
want := !strings.HasPrefix(tag, "-")
|
||||
matches = matches && strings.Contains(todo.Tags, str) == want
|
||||
}
|
||||
return matches
|
||||
})
|
||||
}
|
||||
|
||||
func (todos Todos) Like(like func(Todo) bool) Todos {
|
||||
result := make(Todos, 0)
|
||||
for i := range todos {
|
||||
if like(todos[i]) {
|
||||
result = append(result, todos[i])
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
17
pttodo/todos_test.go
Normal file
17
pttodo/todos_test.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package pttodo
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestTodosLikeTags(t *testing.T) {
|
||||
todos := Todos{
|
||||
{Todo: "a", Tags: "x"},
|
||||
{Todo: "b", Tags: "x,y"},
|
||||
}
|
||||
|
||||
result := todos.LikeTags([]string{"x", "-y"})
|
||||
if len(result) != 1 {
|
||||
t.Error(result)
|
||||
} else if result[0].Todo != "a" {
|
||||
t.Error(result[0].Todo)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user