2022-06-14 14:23:33 +00:00
|
|
|
package utils
|
|
|
|
|
2022-12-18 11:51:20 +00:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2022-08-07 16:51:05 +00:00
|
|
|
// SliceEqual check if two slices are equal
|
2022-06-14 14:23:33 +00:00
|
|
|
func SliceEqual[T comparable](a, b []T) bool {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i, v := range a {
|
|
|
|
if v != b[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2022-06-23 13:19:01 +00:00
|
|
|
|
2022-08-07 16:51:05 +00:00
|
|
|
// SliceContains check if slice contains element
|
2022-06-23 13:19:01 +00:00
|
|
|
func SliceContains[T comparable](arr []T, v T) bool {
|
|
|
|
for _, vv := range arr {
|
|
|
|
if vv == v {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2022-07-27 09:08:29 +00:00
|
|
|
|
2024-02-23 07:37:40 +00:00
|
|
|
// SliceAllContains check if slice all contains elements
|
|
|
|
func SliceAllContains[T comparable](arr []T, vs ...T) bool {
|
|
|
|
vsMap := make(map[T]struct{})
|
|
|
|
for _, v := range arr {
|
|
|
|
vsMap[v] = struct{}{}
|
|
|
|
}
|
|
|
|
for _, v := range vs {
|
|
|
|
if _, ok := vsMap[v]; !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-08-07 16:51:05 +00:00
|
|
|
// SliceConvert convert slice to another type slice
|
2022-07-27 09:08:29 +00:00
|
|
|
func SliceConvert[S any, D any](srcS []S, convert func(src S) (D, error)) ([]D, error) {
|
2023-01-06 07:36:05 +00:00
|
|
|
res := make([]D, 0, len(srcS))
|
2022-08-07 16:51:05 +00:00
|
|
|
for i := range srcS {
|
2022-07-27 09:08:29 +00:00
|
|
|
dst, err := convert(srcS[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res = append(res, dst)
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
2022-11-28 05:45:25 +00:00
|
|
|
|
|
|
|
func MustSliceConvert[S any, D any](srcS []S, convert func(src S) D) []D {
|
2023-01-06 07:36:05 +00:00
|
|
|
res := make([]D, 0, len(srcS))
|
2022-11-28 05:45:25 +00:00
|
|
|
for i := range srcS {
|
|
|
|
dst := convert(srcS[i])
|
|
|
|
res = append(res, dst)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
2022-12-18 11:51:20 +00:00
|
|
|
|
|
|
|
func MergeErrors(errs ...error) error {
|
|
|
|
errStr := strings.Join(MustSliceConvert(errs, func(err error) string {
|
|
|
|
return err.Error()
|
|
|
|
}), "\n")
|
|
|
|
if errStr != "" {
|
|
|
|
return errors.New(errStr)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-06-23 09:36:40 +00:00
|
|
|
|
|
|
|
func SliceMeet[T1, T2 any](arr []T1, v T2, meet func(item T1, v T2) bool) bool {
|
|
|
|
for _, item := range arr {
|
|
|
|
if meet(item, v) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2023-08-11 06:23:30 +00:00
|
|
|
|
|
|
|
func SliceFilter[T any](arr []T, filter func(src T) bool) []T {
|
|
|
|
res := make([]T, 0, len(arr))
|
|
|
|
for _, src := range arr {
|
|
|
|
if filter(src) {
|
|
|
|
res = append(res, src)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
2024-02-23 07:37:40 +00:00
|
|
|
|
|
|
|
func SliceReplace[T any](arr []T, replace func(src T) T) {
|
|
|
|
for i, src := range arr {
|
|
|
|
arr[i] = replace(src)
|
|
|
|
}
|
|
|
|
}
|