Merge pull request #1406 from tanhuaan/main
refactor: use slices.Contains to simplify code
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -95,12 +96,7 @@ func coalesce(v ...any) any {
|
|||||||
// Returns:
|
// Returns:
|
||||||
// - bool: True if all values are non-empty, false otherwise
|
// - bool: True if all values are non-empty, false otherwise
|
||||||
func all(v ...any) bool {
|
func all(v ...any) bool {
|
||||||
for _, val := range v {
|
return !slices.ContainsFunc(v, empty)
|
||||||
if empty(val) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// anyNonEmpty checks if at least one value in a list is non-empty.
|
// anyNonEmpty checks if at least one value in a list is non-empty.
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -49,12 +50,7 @@ func FileExists(filename string) bool {
|
|||||||
|
|
||||||
// Contains returns true if needle is contained in haystack
|
// Contains returns true if needle is contained in haystack
|
||||||
func Contains[T comparable](haystack []T, needle T) bool {
|
func Contains[T comparable](haystack []T, needle T) bool {
|
||||||
for _, s := range haystack {
|
return slices.Contains(haystack, needle)
|
||||||
if s == needle {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ContainsIP returns true if any one of the of prefixes contains the ip.
|
// ContainsIP returns true if any one of the of prefixes contains the ip.
|
||||||
|
|||||||
Reference in New Issue
Block a user