-
Notifications
You must be signed in to change notification settings - Fork 2
/
filters.go
65 lines (60 loc) · 1.45 KB
/
filters.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
package processors
import (
"fmt"
"strings"
"github.com/alexferrari88/gohn/pkg/gohn"
)
// FilterOutWords filters items that contain the given words in the title or text.
// The argument title is a boolean that indicates if the filter
// should be applied to the title and not the text.
func FilterOutWords(words []string, title bool) gohn.ItemProcessor {
return func(item *gohn.Item) error {
if item == nil {
return nil
}
for _, word := range words {
if title && item.Title != nil {
if strings.Contains(strings.ToLower(*item.Title), strings.ToLower(word)) {
return fmt.Errorf("word %s found in title", word)
}
} else {
if item.Text == nil {
return nil
}
if strings.Contains(strings.ToLower(*item.Text), strings.ToLower(word)) {
return fmt.Errorf("Word found")
}
}
}
return nil
}
}
// FilterOutDeleted filters deleted items
func FilterOutDeleted() gohn.ItemProcessor {
return func(item *gohn.Item) error {
if item == nil {
return nil
}
if item.Deleted != nil && *item.Deleted {
return fmt.Errorf("Deleted item found")
}
return nil
}
}
// FilterOutUsers filters items that are not from the given user
func FilterOutUsers(users []string) gohn.ItemProcessor {
return func(item *gohn.Item) error {
if item == nil {
return nil
}
if item.By == nil {
return nil
}
for _, user := range users {
if *item.By == user {
return fmt.Errorf("User found")
}
}
return nil
}
}