Query filters
This commit is contained in:
parent
85b4abde6c
commit
9315829bc4
7 changed files with 167 additions and 37 deletions
24
util/util.go
24
util/util.go
|
@ -37,6 +37,30 @@ func InStringList(haystack []string, needle string) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
// InStringListAll returns true if all needles are contained in haystack
|
||||
func InStringListAll(haystack []string, needles []string) bool {
|
||||
matches := 0
|
||||
for _, s := range haystack {
|
||||
for _, needle := range needles {
|
||||
if s == needle {
|
||||
matches++
|
||||
}
|
||||
}
|
||||
}
|
||||
return matches == len(needles)
|
||||
}
|
||||
|
||||
// SplitNoEmpty splits a string using strings.Split, but filters out empty strings
|
||||
func SplitNoEmpty(s string, sep string) []string {
|
||||
res := make([]string, 0)
|
||||
for _, r := range strings.Split(s, sep) {
|
||||
if r != "" {
|
||||
res = append(res, r)
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// RandomString returns a random string with a given length
|
||||
func RandomString(length int) string {
|
||||
randomMutex.Lock() // Who would have thought that random.Intn() is not thread-safe?!
|
||||
|
|
|
@ -56,6 +56,19 @@ func TestInStringList(t *testing.T) {
|
|||
require.False(t, InStringList(s, "three"))
|
||||
}
|
||||
|
||||
func TestInStringListAll(t *testing.T) {
|
||||
s := []string{"one", "two", "three", "four"}
|
||||
require.True(t, InStringListAll(s, []string{"two", "four"}))
|
||||
require.False(t, InStringListAll(s, []string{"three", "five"}))
|
||||
}
|
||||
|
||||
func TestSplitNoEmpty(t *testing.T) {
|
||||
require.Equal(t, []string{}, SplitNoEmpty("", ","))
|
||||
require.Equal(t, []string{}, SplitNoEmpty(",,,", ","))
|
||||
require.Equal(t, []string{"tag1", "tag2"}, SplitNoEmpty("tag1,tag2", ","))
|
||||
require.Equal(t, []string{"tag1", "tag2"}, SplitNoEmpty("tag1,tag2,", ","))
|
||||
}
|
||||
|
||||
func TestExpandHome_WithTilde(t *testing.T) {
|
||||
require.Equal(t, os.Getenv("HOME")+"/this/is/a/path", ExpandHome("~/this/is/a/path"))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue