initial public commit
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
package list
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
var cachedPatterns = map[string]*regexp.Regexp{}
|
||||
|
||||
// ExustInSlice checks whether a comparable element exists in a slice of the same type.
|
||||
func ExistInSlice[T comparable](item T, list []T) bool {
|
||||
if len(list) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, v := range list {
|
||||
if v == item {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// ExistInSliceWithRegex checks whether a string exists in a slice
|
||||
// either by direct match, or by a regular expression (eg. `^\w+$`).
|
||||
//
|
||||
// _Note: Only list items starting with '^' and ending with '$' are treated as regular expressions!_
|
||||
func ExistInSliceWithRegex(str string, list []string) bool {
|
||||
for _, field := range list {
|
||||
isRegex := strings.HasPrefix(field, "^") && strings.HasSuffix(field, "$")
|
||||
|
||||
if !isRegex {
|
||||
// check for direct match
|
||||
if str == field {
|
||||
return true
|
||||
}
|
||||
} else {
|
||||
// check for regex match
|
||||
pattern, ok := cachedPatterns[field]
|
||||
if !ok {
|
||||
var patternErr error
|
||||
pattern, patternErr = regexp.Compile(field)
|
||||
if patternErr != nil {
|
||||
continue
|
||||
}
|
||||
// "cache" the pattern to avoid compiling it every time
|
||||
cachedPatterns[field] = pattern
|
||||
}
|
||||
if pattern != nil && pattern.MatchString(str) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// ToInterfaceSlice converts a generic slice to slice of interfaces.
|
||||
func ToInterfaceSlice[T any](list []T) []any {
|
||||
result := make([]any, len(list))
|
||||
|
||||
for i := range list {
|
||||
result[i] = list[i]
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// NonzeroUniques returns only the nonzero unique values from a slice.
|
||||
func NonzeroUniques[T comparable](list []T) []T {
|
||||
result := []T{}
|
||||
existMap := map[T]bool{}
|
||||
|
||||
var zeroVal T
|
||||
|
||||
for _, val := range list {
|
||||
if !existMap[val] && val != zeroVal {
|
||||
existMap[val] = true
|
||||
result = append(result, val)
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// ToUniqueStringSlice casts `value` to a slice of non-zero unique strings.
|
||||
func ToUniqueStringSlice(value any) []string {
|
||||
strings := []string{}
|
||||
|
||||
switch val := value.(type) {
|
||||
case nil:
|
||||
// nothing to cast
|
||||
case []string:
|
||||
strings = val
|
||||
case string:
|
||||
if val == "" {
|
||||
break
|
||||
}
|
||||
|
||||
// check if it is a json encoded array of strings
|
||||
if err := json.Unmarshal([]byte(val), &strings); err != nil {
|
||||
// not a json array, just add the string as single array element
|
||||
strings = append(strings, val)
|
||||
}
|
||||
case json.Marshaler: // eg. JsonArray
|
||||
raw, _ := val.MarshalJSON()
|
||||
json.Unmarshal(raw, &strings)
|
||||
default:
|
||||
strings = cast.ToStringSlice(value)
|
||||
}
|
||||
|
||||
return NonzeroUniques(strings)
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
package list_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/pocketbase/pocketbase/tools/list"
|
||||
"github.com/pocketbase/pocketbase/tools/types"
|
||||
)
|
||||
|
||||
func TestExistInSliceString(t *testing.T) {
|
||||
scenarios := []struct {
|
||||
item string
|
||||
list []string
|
||||
expected bool
|
||||
}{
|
||||
{"", []string{""}, true},
|
||||
{"", []string{"1", "2", "test 123"}, false},
|
||||
{"test", []string{}, false},
|
||||
{"test", []string{"TEST"}, false},
|
||||
{"test", []string{"1", "2", "test 123"}, false},
|
||||
{"test", []string{"1", "2", "test"}, true},
|
||||
}
|
||||
|
||||
for i, scenario := range scenarios {
|
||||
result := list.ExistInSlice(scenario.item, scenario.list)
|
||||
if result != scenario.expected {
|
||||
if scenario.expected {
|
||||
t.Errorf("(%d) Expected to exist in the list", i)
|
||||
} else {
|
||||
t.Errorf("(%d) Expected NOT to exist in the list", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExistInSliceInt(t *testing.T) {
|
||||
scenarios := []struct {
|
||||
item int
|
||||
list []int
|
||||
expected bool
|
||||
}{
|
||||
{0, []int{}, false},
|
||||
{0, []int{0}, true},
|
||||
{4, []int{1, 2, 3}, false},
|
||||
{1, []int{1, 2, 3}, true},
|
||||
{-1, []int{0, 1, 2, 3}, false},
|
||||
{-1, []int{0, -1, -2, -3, -4}, true},
|
||||
}
|
||||
|
||||
for i, scenario := range scenarios {
|
||||
result := list.ExistInSlice(scenario.item, scenario.list)
|
||||
if result != scenario.expected {
|
||||
if scenario.expected {
|
||||
t.Errorf("(%d) Expected to exist in the list", i)
|
||||
} else {
|
||||
t.Errorf("(%d) Expected NOT to exist in the list", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExistInSliceWithRegex(t *testing.T) {
|
||||
scenarios := []struct {
|
||||
item string
|
||||
list []string
|
||||
expected bool
|
||||
}{
|
||||
{"", []string{``}, true},
|
||||
{"", []string{`^\W+$`}, false},
|
||||
{" ", []string{`^\W+$`}, true},
|
||||
{"test", []string{`^\invalid[+$`}, false}, // invalid regex
|
||||
{"test", []string{`^\W+$`, "test"}, true},
|
||||
{`^\W+$`, []string{`^\W+$`, "test"}, false}, // direct match shouldn't work for this case
|
||||
{`\W+$`, []string{`\W+$`, "test"}, true}, // direct match should work for this case because it is not an actual supported pattern format
|
||||
{"!?@", []string{`\W+$`, "test"}, false}, // the method requires the pattern elems to start with '^'
|
||||
{"!?@", []string{`^\W+`, "test"}, false}, // the method requires the pattern elems to end with '$'
|
||||
{"!?@", []string{`^\W+$`, "test"}, true},
|
||||
{"!?@test", []string{`^\W+$`, "test"}, false},
|
||||
}
|
||||
|
||||
for i, scenario := range scenarios {
|
||||
result := list.ExistInSliceWithRegex(scenario.item, scenario.list)
|
||||
if result != scenario.expected {
|
||||
if scenario.expected {
|
||||
t.Errorf("(%d) Expected the string to exist in the list", i)
|
||||
} else {
|
||||
t.Errorf("(%d) Expected the string NOT to exist in the list", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestToInterfaceSlice(t *testing.T) {
|
||||
scenarios := []struct {
|
||||
items []string
|
||||
}{
|
||||
{[]string{}},
|
||||
{[]string{""}},
|
||||
{[]string{"1", "test"}},
|
||||
{[]string{"test1", "test2", "test3"}},
|
||||
}
|
||||
|
||||
for i, scenario := range scenarios {
|
||||
result := list.ToInterfaceSlice(scenario.items)
|
||||
|
||||
if len(result) != len(scenario.items) {
|
||||
t.Errorf("(%d) Result list length doesn't match with the original list", i)
|
||||
}
|
||||
|
||||
for j, v := range result {
|
||||
if v != scenario.items[j] {
|
||||
t.Errorf("(%d:%d) Result list item should match with the original list item", i, j)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNonzeroUniquesString(t *testing.T) {
|
||||
scenarios := []struct {
|
||||
items []string
|
||||
expected []string
|
||||
}{
|
||||
{[]string{}, []string{}},
|
||||
{[]string{""}, []string{}},
|
||||
{[]string{"1", "test"}, []string{"1", "test"}},
|
||||
{[]string{"test1", "", "test2", "Test2", "test1", "test3"}, []string{"test1", "test2", "Test2", "test3"}},
|
||||
}
|
||||
|
||||
for i, scenario := range scenarios {
|
||||
result := list.NonzeroUniques(scenario.items)
|
||||
|
||||
if len(result) != len(scenario.expected) {
|
||||
t.Errorf("(%d) Result list length doesn't match with the expected list", i)
|
||||
}
|
||||
|
||||
for j, v := range result {
|
||||
if v != scenario.expected[j] {
|
||||
t.Errorf("(%d:%d) Result list item should match with the expected list item", i, j)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestToUniqueStringSlice(t *testing.T) {
|
||||
scenarios := []struct {
|
||||
value any
|
||||
expected []string
|
||||
}{
|
||||
{nil, []string{}},
|
||||
{"", []string{}},
|
||||
{[]any{}, []string{}},
|
||||
{[]int{}, []string{}},
|
||||
{"test", []string{"test"}},
|
||||
{[]int{1, 2, 3}, []string{"1", "2", "3"}},
|
||||
{[]any{0, 1, "test", ""}, []string{"0", "1", "test"}},
|
||||
{[]string{"test1", "test2", "test1"}, []string{"test1", "test2"}},
|
||||
{`["test1", "test2", "test2"]`, []string{"test1", "test2"}},
|
||||
{types.JsonArray{"test1", "test2", "test1"}, []string{"test1", "test2"}},
|
||||
}
|
||||
|
||||
for i, scenario := range scenarios {
|
||||
result := list.ToUniqueStringSlice(scenario.value)
|
||||
|
||||
if len(result) != len(scenario.expected) {
|
||||
t.Errorf("(%d) Result list length doesn't match with the expected list", i)
|
||||
}
|
||||
|
||||
for j, v := range result {
|
||||
if v != scenario.expected[j] {
|
||||
t.Errorf("(%d:%d) Result list item should match with the expected list item", i, j)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user