-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
filter.go
37 lines (30 loc) · 975 Bytes
/
filter.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
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package stringslice
import (
"strings"
"unicode"
)
// Filter applies the provided filter function and removes all items from the slice for which the filter function returns true.
// This function uses append and might cause
func Filter(values []string, filter func(string) bool) (ret []string) {
for _, value := range values {
if !filter(value) {
ret = append(ret, value)
}
}
if ret == nil {
return []string{}
}
return
}
// TrimEmptyFilter applies the strings.TrimFunc function and removes all empty strings
func TrimEmptyFilter(values []string, trim func(rune) bool) (ret []string) {
return Filter(values, func(value string) bool {
return strings.TrimFunc(value, trim) == ""
})
}
// TrimSpaceEmptyFilter applies the strings.TrimSpace function and removes all empty strings
func TrimSpaceEmptyFilter(values []string) []string {
return TrimEmptyFilter(values, unicode.IsSpace)
}