mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-07 06:02:27 +08:00
feat: add func ContainSubSlice
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
<div align="center" style="text-align: center;">
|
||||
|
||||

|
||||
[](https://github.com/duke-git/lancet/releases)
|
||||
[](https://github.com/duke-git/lancet/releases)
|
||||
[](https://pkg.go.dev/github.com/duke-git/lancet)
|
||||
[](https://goreportcard.com/report/github.com/duke-git/lancet)
|
||||
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||
@@ -394,7 +394,8 @@ func main() {
|
||||
|
||||
```go
|
||||
func Contain(slice interface{}, value interface{}) bool //check if the value is in the slice or not
|
||||
func Chunk(slice []interface{}, size int) [][]interface{} //creates an slice of elements split into groups the length of `size`.
|
||||
func ContainSubSlice(slice interface{}, subslice interface{}) bool //check if the slice contain subslice or not
|
||||
func Chunk(slice []interface{}, size int) [][]interface{} //creates an slice of elements split into groups the length of `size`
|
||||
func ConvertSlice(originalSlice interface{}, newSliceType reflect.Type) interface{} //convert originalSlice to newSliceType
|
||||
func Difference(slice1, slice2 interface{}) interface{} //creates an slice of whose element not included in the other given slice
|
||||
func DeleteByIndex(slice interface{}, start int, end ...int) (interface{}, error) //delete the element of slice from start index to end index - 1
|
||||
@@ -402,7 +403,7 @@ func Drop(slice interface{}, n int) interface{} //creates a slice with `n` eleme
|
||||
func Every(slice, function interface{}) bool //return true if all of the values in the slice pass the predicate function, function signature should be func(index int, value interface{}) bool
|
||||
func None(slice, function interface{}) bool // return true if all the values in the slice mismatch the criteria
|
||||
func Filter(slice, function interface{}) interface{} //filter slice, function signature should be func(index int, value interface{}) bool
|
||||
func Find(slice, function interface{}) (interface{}, bool) //iterates over elements of slice, returning the first one that passes a truth test on function.function signature should be func(index int, value interface{}) bool .
|
||||
func Find(slice, function interface{}) (interface{}, bool) //iterates over elements of slice, returning the first one that passes a truth test on function.function signature should be func(index int, value interface{}) bool
|
||||
func FlattenDeep(slice interface{}) interface{} //flattens slice recursive
|
||||
func ForEach(slice, function interface{}) //iterates over elements of slice and invokes function for each element, function signature should be func(index int, value interface{})
|
||||
func IntSlice(slice interface{}) ([]int, error) //convert value to int slice
|
||||
@@ -417,7 +418,7 @@ func SortByField(slice interface{}, field string, sortType ...string) error //so
|
||||
func Some(slice, function interface{}) bool //return true if any of the values in the list pass the predicate function, function signature should be func(index int, value interface{}) bool
|
||||
func StringSlice(slice interface{}) []string //convert value to string slice
|
||||
func Unique(slice interface{}) interface{} //remove duplicate elements in slice
|
||||
func Union(slices ...interface{}) interface{} //Union creates a slice of unique values, in order, from all given slices. using == for equality comparisons.
|
||||
func Union(slices ...interface{}) interface{} //Union creates a slice of unique values, in order, from all given slices. using == for equality comparisons
|
||||
func UpdateByIndex(slice interface{}, index int, value interface{}) (interface{}, error) //update the slice element at index.
|
||||
func Without(slice interface{}, values ...interface{}) interface{} //creates a slice excluding all given values
|
||||
func GroupBy(slice, function interface{}) (interface{}, interface{}) // groups slice into two categories
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
<div align="center" style="text-align: center;">
|
||||
|
||||

|
||||
[](https://github.com/duke-git/lancet/releases)
|
||||
[](https://github.com/duke-git/lancet/releases)
|
||||
[](https://pkg.go.dev/github.com/duke-git/lancet)
|
||||
[](https://goreportcard.com/report/github.com/duke-git/lancet)
|
||||
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||
@@ -395,6 +395,7 @@ func main() {
|
||||
|
||||
```go
|
||||
func Contain(slice interface{}, value interface{}) bool //判断slice是否包含value
|
||||
func ContainSubSlice(slice interface{}, subslice interface{}) bool //判断slice是否包含subslice
|
||||
func Chunk(slice []interface{}, size int) [][]interface{} //均分slice
|
||||
func ConvertSlice(originalSlice interface{}, newSliceType reflect.Type) interface{} //将originalSlice转换为 newSliceType
|
||||
func Difference(slice1, slice2 interface{}) interface{} //返回切片,其元素在slice1中,不在slice2中
|
||||
|
||||
@@ -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