Skip to content

Commit

Permalink
feat: add generic util function SliceHas (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoK29 authored Jun 30, 2023
1 parent 2536281 commit b439a24
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func RandomOne[T any](arr []T) T
func Unique[T ~string | comdef.XintOrFloat](list []T) []T
func IndexOf[T ~string | comdef.XintOrFloat](val T, list []T) int
// source at arrutil/check.go
func SliceHas[T comdef.ScalarType](slice []T, val T) bool
func IntsHas(ints []int, val int) bool
func Int64sHas(ints []int64, val int64) bool
func InStrings(elem string, ss []string) bool
Expand Down Expand Up @@ -164,6 +165,10 @@ arrutil.IntsHas([]int{2, 4, 5}, 2) // True
arrutil.Int64sHas([]int64{2, 4, 5}, 2) // True
arrutil.StringsHas([]string{"a", "b"}, "a") // True

arrutil.SliceHas([]int{2, 4, 5}, 2) // True
arrutil.SliceHas([]int64{2, 4, 5}, 2) // True
arrutil.SliceHas([]string{"a", "b"}, "a") // True

// list and val interface{}
arrutil.Contains(list, val)
arrutil.Contains([]uint32{9, 2, 3}, 9) // True
Expand Down
5 changes: 5 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func RandomOne[T any](arr []T) T
func Unique[T ~string | comdef.XintOrFloat](list []T) []T
func IndexOf[T ~string | comdef.XintOrFloat](val T, list []T) int
// source at arrutil/check.go
func SliceHas[T comdef.ScalarType](slice []T, val T) bool
func IntsHas(ints []int, val int) bool
func Int64sHas(ints []int64, val int64) bool
func InStrings(elem string, ss []string) bool
Expand Down Expand Up @@ -163,6 +164,10 @@ arrutil.IntsHas([]int{2, 4, 5}, 2) // True
arrutil.Int64sHas([]int64{2, 4, 5}, 2) // True
arrutil.StringsHas([]string{"a", "b"}, "a") // True

arrutil.SliceHas([]int{2, 4, 5}, 2) // True
arrutil.SliceHas([]int64{2, 4, 5}, 2) // True
arrutil.SliceHas([]string{"a", "b"}, "a") // True

// list and val interface{}
arrutil.Contains(list, val)
arrutil.Contains([]uint32{9, 2, 3}, 9) // True
Expand Down
11 changes: 11 additions & 0 deletions arrutil/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ import (
"github.com/gookit/goutil/mathutil"
)

// SliceHas check the slice contains the given value
func SliceHas[T comdef.ScalarType](slice []T, val T) bool {
for _, ele := range slice {
if ele == val {
return true
}
}

return false
}

// IntsHas check the []int contains the given value
func IntsHas(ints []int, val int) bool {
for _, ele := range ints {
Expand Down
14 changes: 14 additions & 0 deletions arrutil/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ import (
"github.com/gookit/goutil/testutil/assert"
)

func TestSliceHas(t *testing.T) {
ints := []int{2, 4, 5}
assert.True(t, arrutil.SliceHas(ints, 2))
assert.False(t, arrutil.SliceHas(ints, 3))

int64s := []int64{2, 4, 5}
assert.True(t, arrutil.SliceHas(int64s, 2))
assert.False(t, arrutil.SliceHas(int64s, 3))

strs := []string{"2", "4", "5"}
assert.True(t, arrutil.SliceHas(strs, "2"))
assert.False(t, arrutil.SliceHas(strs, "3"))
}

func TestIntsHas(t *testing.T) {
ints := []int{2, 4, 5}
assert.True(t, arrutil.IntsHas(ints, 2))
Expand Down

0 comments on commit b439a24

Please sign in to comment.