mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-09 23:22:28 +08:00
feat: add func ContainSubSlice
This commit is contained in:
@@ -17,7 +17,6 @@ import (
|
||||
|
||||
// Contain check if the value is in the iterable type or not
|
||||
func Contain(iterableType interface{}, value interface{}) bool {
|
||||
|
||||
v := reflect.ValueOf(iterableType)
|
||||
|
||||
switch kind := reflect.TypeOf(iterableType).Kind(); kind {
|
||||
@@ -47,6 +46,30 @@ func Contain(iterableType interface{}, value interface{}) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// ContainSubSlice check if the slice contain subslice or not
|
||||
func ContainSubSlice(slice interface{}, subslice interface{}) bool {
|
||||
super := sliceValue(slice)
|
||||
sub := sliceValue(subslice)
|
||||
|
||||
if super.Type().Elem().Kind() != sub.Type().Elem().Kind() {
|
||||
return false
|
||||
}
|
||||
|
||||
unique := make(map[interface{}]bool)
|
||||
for i := 0; i < super.Len(); i++ {
|
||||
v := super.Index(i).Interface()
|
||||
unique[v] = true
|
||||
}
|
||||
for i := 0; i < sub.Len(); i++ {
|
||||
v := sub.Index(i).Interface()
|
||||
if !unique[v] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// Chunk creates an slice of elements split into groups the length of `size`.
|
||||
func Chunk(slice []interface{}, size int) [][]interface{} {
|
||||
var res [][]interface{}
|
||||
|
||||
@@ -23,6 +23,15 @@ func TestContain(t *testing.T) {
|
||||
assert.Equal(false, Contain("abc", "d"))
|
||||
}
|
||||
|
||||
func TestContainSubSlice(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestContainSubSlice")
|
||||
assert.Equal(true, ContainSubSlice([]string{"a", "a", "b", "c"}, []string{"a", "a"}))
|
||||
assert.Equal(false, ContainSubSlice([]string{"a", "a", "b", "c"}, []string{"a", "d"}))
|
||||
|
||||
assert.Equal(true, ContainSubSlice([]int{1, 2, 3}, []int{1}))
|
||||
assert.Equal(false, ContainSubSlice([]int{1, 2, 3}, []string{"a"}))
|
||||
}
|
||||
|
||||
func TestChunk(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestChunk")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user