From 24ae47a12fedbc06c6b46200af4f12a3847d65f3 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Wed, 12 Jan 2022 10:13:13 +0800 Subject: [PATCH 1/4] remove v2 branch --- .github/workflows/codecov.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml index 49346f95..41140556 100644 --- a/.github/workflows/codecov.yml +++ b/.github/workflows/codecov.yml @@ -3,11 +3,11 @@ on: push: branches: - main - - v2 + # - v2 pull_request: branches: - main - - v2 + # - v2 jobs: build: runs-on: ubuntu-latest From 6890bbfe05bee641a7bc2f51263d7b190b8988be Mon Sep 17 00:00:00 2001 From: dudaodong Date: Wed, 12 Jan 2022 11:23:51 +0800 Subject: [PATCH 2/4] update: rename workflow --- .github/workflows/codecov.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml index 41140556..9c657fb2 100644 --- a/.github/workflows/codecov.yml +++ b/.github/workflows/codecov.yml @@ -1,4 +1,4 @@ -name: Test and coverage +name: test on: push: branches: From fd271fe176c86c27723f9e2a0dae7c53171f2dba Mon Sep 17 00:00:00 2001 From: dudaodong Date: Wed, 12 Jan 2022 11:27:09 +0800 Subject: [PATCH 3/4] add test passing badge --- README.md | 1 + README_zh-CN.md | 1 + 2 files changed, 2 insertions(+) diff --git a/README.md b/README.md index 0af279b5..d68f211e 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ [![Release](https://img.shields.io/badge/release-1.1.9-green.svg)](https://github.com/duke-git/lancet/releases) [![GoDoc](https://godoc.org/github.com//duke-git/lancet?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet)](https://goreportcard.com/report/github.com/duke-git/lancet) +[![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml) [![codecov](https://codecov.io/gh/duke-git/lancet/branch/main/graph/badge.svg?token=FC48T1F078)](https://codecov.io/gh/duke-git/lancet) [![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/duke-git/lancet/blob/main/LICENSE) diff --git a/README_zh-CN.md b/README_zh-CN.md index ec8312d0..20b48343 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -9,6 +9,7 @@ [![Release](https://img.shields.io/badge/release-1.1.9-green.svg)](https://github.com/duke-git/lancet/releases) [![GoDoc](https://godoc.org/github.com//duke-git/lancet?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet)](https://goreportcard.com/report/github.com/duke-git/lancet) +[![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml) [![codecov](https://codecov.io/gh/duke-git/lancet/branch/main/graph/badge.svg?token=FC48T1F078)](https://codecov.io/gh/duke-git/lancet) [![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/duke-git/lancet/blob/main/LICENSE) From e31fb2800357a0650079ceb86f82c37fae68560e Mon Sep 17 00:00:00 2001 From: dudaodong Date: Thu, 13 Jan 2022 11:00:27 +0800 Subject: [PATCH 4/4] feat: add func ContainSubSlice --- README.md | 9 +++++---- README_zh-CN.md | 3 ++- slice/slice.go | 25 ++++++++++++++++++++++++- slice/slice_test.go | 9 +++++++++ 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index d68f211e..b42bed20 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@
![Go version](https://img.shields.io/badge/go-%3E%3D1.16-9cf) -[![Release](https://img.shields.io/badge/release-1.1.9-green.svg)](https://github.com/duke-git/lancet/releases) +[![Release](https://img.shields.io/badge/release-1.1.10-green.svg)](https://github.com/duke-git/lancet/releases) [![GoDoc](https://godoc.org/github.com//duke-git/lancet?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet)](https://goreportcard.com/report/github.com/duke-git/lancet) [![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](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 diff --git a/README_zh-CN.md b/README_zh-CN.md index 20b48343..60a3fb49 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -6,7 +6,7 @@
![Go version](https://img.shields.io/badge/go-%3E%3D1.16-9cf) -[![Release](https://img.shields.io/badge/release-1.1.9-green.svg)](https://github.com/duke-git/lancet/releases) +[![Release](https://img.shields.io/badge/release-1.1.10-green.svg)](https://github.com/duke-git/lancet/releases) [![GoDoc](https://godoc.org/github.com//duke-git/lancet?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet)](https://goreportcard.com/report/github.com/duke-git/lancet) [![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](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中 diff --git a/slice/slice.go b/slice/slice.go index da075542..3af5f378 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -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{} diff --git a/slice/slice_test.go b/slice/slice_test.go index 9a7d6d20..cb8763b4 100644 --- a/slice/slice_test.go +++ b/slice/slice_test.go @@ -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")