mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-08 06:32:28 +08:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
acf028cdcd | ||
|
|
31e43ec356 | ||
|
|
9f45e68fef | ||
|
|
d2df99a6f0 | ||
|
|
713c341831 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -6,4 +6,5 @@ fileutil/*.txt
|
|||||||
fileutil/*.zip
|
fileutil/*.zip
|
||||||
fileutil/*.link
|
fileutil/*.link
|
||||||
fileutil/unzip/*
|
fileutil/unzip/*
|
||||||
|
slice/testdata/*
|
||||||
cryptor/*.pem
|
cryptor/*.pem
|
||||||
@@ -4,7 +4,7 @@
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||

|

|
||||||
[](https://github.com/duke-git/lancet/releases)
|
[](https://github.com/duke-git/lancet/releases)
|
||||||
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
||||||
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
||||||
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||||
@@ -35,9 +35,9 @@ English | [简体中文](./README_zh-CN.md)
|
|||||||
go get github.com/duke-git/lancet/v2 // will install latest version of v2.x.x
|
go get github.com/duke-git/lancet/v2 // will install latest version of v2.x.x
|
||||||
```
|
```
|
||||||
|
|
||||||
2. <b>For users who use version below go1.18, you should install v1.x.x. now latest v1 is v1.2.9. </b>
|
2. <b>For users who use version below go1.18, you should install v1.x.x. now latest v1 is v1.3.0. </b>
|
||||||
```go
|
```go
|
||||||
go get github.com/duke-git/lancet@v1.2.9 // below go1.18, install latest version of v1.x.x
|
go get github.com/duke-git/lancet@v1.3.0 // below go1.18, install latest version of v1.x.x
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
@@ -396,6 +396,7 @@ import "github.com/duke-git/lancet/v2/slice"
|
|||||||
- [StringSlice](https://github.com/duke-git/lancet/blob/main/docs/slice.md#StringSlice)
|
- [StringSlice](https://github.com/duke-git/lancet/blob/main/docs/slice.md#StringSlice)
|
||||||
- [SymmetricDifference](https://github.com/duke-git/lancet/blob/main/docs/slice.md#SymmetricDifference)
|
- [SymmetricDifference](https://github.com/duke-git/lancet/blob/main/docs/slice.md#SymmetricDifference)
|
||||||
- [Unique](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Unique)
|
- [Unique](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Unique)
|
||||||
|
- [UniqueBy](https://github.com/duke-git/lancet/blob/main/docs/slice.md#UniqueBy)
|
||||||
- [Union](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Union)
|
- [Union](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Union)
|
||||||
- [UpdateAt](https://github.com/duke-git/lancet/blob/main/docs/slice.md#UpdateAt)
|
- [UpdateAt](https://github.com/duke-git/lancet/blob/main/docs/slice.md#UpdateAt)
|
||||||
- [Without](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Without)
|
- [Without](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Without)
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||

|

|
||||||
[](https://github.com/duke-git/lancet/releases)
|
[](https://github.com/duke-git/lancet/releases)
|
||||||
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
||||||
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
||||||
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||||
@@ -35,9 +35,9 @@
|
|||||||
go get github.com/duke-git/lancet/v2 //安装v2最新版本v2.x.x
|
go get github.com/duke-git/lancet/v2 //安装v2最新版本v2.x.x
|
||||||
```
|
```
|
||||||
|
|
||||||
2. <b>使用go1.18以下版本的用户,必须安装v1.x.x。目前最新的v1版本是v1.2.9。</b>
|
2. <b>使用go1.18以下版本的用户,必须安装v1.x.x。目前最新的v1版本是v1.3.0。</b>
|
||||||
```go
|
```go
|
||||||
go get github.com/duke-git/lancet@v1.2.9 // 使用go1.18以下版本, 必须安装v1.x.x版本
|
go get github.com/duke-git/lancet@v1.3.0 // 使用go1.18以下版本, 必须安装v1.x.x版本
|
||||||
```
|
```
|
||||||
|
|
||||||
## 用法
|
## 用法
|
||||||
@@ -391,6 +391,7 @@ import "github.com/duke-git/lancet/v2/slice"
|
|||||||
- [StringSlice](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#StringSlice)
|
- [StringSlice](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#StringSlice)
|
||||||
- [SymmetricDifference](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#SymmetricDifference)
|
- [SymmetricDifference](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#SymmetricDifference)
|
||||||
- [Unique](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Unique)
|
- [Unique](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Unique)
|
||||||
|
- [UniqueBy](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#UniqueBy)
|
||||||
- [Union](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Union)
|
- [Union](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Union)
|
||||||
- [UpdateAt](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#UpdateAt)
|
- [UpdateAt](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#UpdateAt)
|
||||||
- [Without](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Without)
|
- [Without](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Without)
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ func (q *ArrayQueue[T]) Data() []T {
|
|||||||
return items
|
return items
|
||||||
}
|
}
|
||||||
|
|
||||||
// Size return length of queue data
|
// Size return number of elements in queue
|
||||||
func (q *ArrayQueue[T]) Size() int {
|
func (q *ArrayQueue[T]) Size() int {
|
||||||
return q.size
|
return q.size
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ import (
|
|||||||
- [StringSlice](#StringSlice)
|
- [StringSlice](#StringSlice)
|
||||||
- [SymmetricDifference](#SymmetricDifference)
|
- [SymmetricDifference](#SymmetricDifference)
|
||||||
- [Unique](#Unique)
|
- [Unique](#Unique)
|
||||||
|
- [UniqueBy](#UniqueBy)
|
||||||
- [Union](#Union)
|
- [Union](#Union)
|
||||||
- [UpdateAt](#UpdateAt)
|
- [UpdateAt](#UpdateAt)
|
||||||
- [Without](#Without)
|
- [Without](#Without)
|
||||||
@@ -1086,7 +1087,33 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Union">Unique</span>
|
### <span id="UniqueBy">UniqueBy</span>
|
||||||
|
<p>Call iteratee func with every item of slice, then remove duplicated.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func UniqueBy[T any](slice []T, iteratee func(item T) T) []T
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
res := slice.UniqueBy([]int{1, 2, 3, 4, 5, 6}, func(val int) int {
|
||||||
|
return val % 4
|
||||||
|
})
|
||||||
|
fmt.Println(res) //[]int{1, 2, 3, 0}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Union">Union</span>
|
||||||
<p>Creates a slice of unique values, in order, from all given slices. using == for equality comparisons.</p>
|
<p>Creates a slice of unique values, in order, from all given slices. using == for equality comparisons.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ import (
|
|||||||
- [StringSlice](#StringSlice)
|
- [StringSlice](#StringSlice)
|
||||||
- [SymmetricDifference](#SymmetricDifference)
|
- [SymmetricDifference](#SymmetricDifference)
|
||||||
- [Unique](#Unique)
|
- [Unique](#Unique)
|
||||||
|
- [UniqueBy](#UniqueBy)
|
||||||
- [Union](#Union)
|
- [Union](#Union)
|
||||||
- [UpdateAt](#UpdateAt)
|
- [UpdateAt](#UpdateAt)
|
||||||
- [Without](#Without)
|
- [Without](#Without)
|
||||||
@@ -1085,7 +1086,33 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Union">Unique</span>
|
### <span id="UniqueBy">UniqueBy</span>
|
||||||
|
<p>对切片的每个元素调用iteratee函数,然后删除重复元素</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func UniqueBy[T any](slice []T, iteratee func(item T) T) []T
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
res := slice.UniqueBy([]int{1, 2, 3, 4, 5, 6}, func(val int) int {
|
||||||
|
return val % 4
|
||||||
|
})
|
||||||
|
fmt.Println(res) //[]int{1, 2, 3, 0}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Union">Union</span>
|
||||||
<p>从所有给定的切片按顺序创建一个唯一值切片,使用==进行相等比较</p>
|
<p>从所有给定的切片按顺序创建一个唯一值切片,使用==进行相等比较</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|||||||
@@ -582,6 +582,21 @@ func Unique[T any](slice []T) []T {
|
|||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UniqueBy call iteratee func with every item of slice, then remove duplicated.
|
||||||
|
func UniqueBy[T any](slice []T, iteratee func(item T) T) []T {
|
||||||
|
if len(slice) == 0 {
|
||||||
|
return []T{}
|
||||||
|
}
|
||||||
|
|
||||||
|
var res []T
|
||||||
|
for _, v := range slice {
|
||||||
|
val := iteratee(v)
|
||||||
|
res = append(res, val)
|
||||||
|
}
|
||||||
|
|
||||||
|
return Unique(res)
|
||||||
|
}
|
||||||
|
|
||||||
// Union creates a slice of unique values, in order, from all given slices. using == for equality comparisons.
|
// Union creates a slice of unique values, in order, from all given slices. using == for equality comparisons.
|
||||||
func Union[T any](slices ...[]T) []T {
|
func Union[T any](slices ...[]T) []T {
|
||||||
if len(slices) == 0 {
|
if len(slices) == 0 {
|
||||||
|
|||||||
@@ -61,11 +61,8 @@ func TestCompact(t *testing.T) {
|
|||||||
func TestConcat(t *testing.T) {
|
func TestConcat(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "Concat")
|
assert := internal.NewAssert(t, "Concat")
|
||||||
|
|
||||||
// assert.Equal([]int{0}, Concat([]int{}, 0))
|
|
||||||
// assert.Equal([]int{1, 2, 3, 4, 5}, Concat([]int{1, 2, 3}, 4, 5))
|
|
||||||
assert.Equal([]int{1, 2, 3, 4, 5}, Concat([]int{1, 2, 3}, []int{4, 5}))
|
assert.Equal([]int{1, 2, 3, 4, 5}, Concat([]int{1, 2, 3}, []int{4, 5}))
|
||||||
assert.Equal([]int{1, 2, 3, 4, 5}, Concat([]int{1, 2, 3}, []int{4}, []int{5}))
|
assert.Equal([]int{1, 2, 3, 4, 5}, Concat([]int{1, 2, 3}, []int{4}, []int{5}))
|
||||||
// assert.Equal([]int{1, 2, 3, 4, 5}, Concat([]int{1, 2, 3}, []int{4}, 5))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEqual(t *testing.T) {
|
func TestEqual(t *testing.T) {
|
||||||
@@ -79,6 +76,13 @@ func TestEqual(t *testing.T) {
|
|||||||
assert.Equal(false, Equal(slice1, slice3))
|
assert.Equal(false, Equal(slice1, slice3))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// go test -fuzz=Fuzz -fuzztime=10s .
|
||||||
|
func FuzzEqual(f *testing.F) {
|
||||||
|
f.Fuzz(func(t *testing.T, a, b []byte) {
|
||||||
|
Equal(a, b)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestEqualWith(t *testing.T) {
|
func TestEqualWith(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestEqualWith")
|
assert := internal.NewAssert(t, "TestEqualWith")
|
||||||
|
|
||||||
@@ -395,6 +399,15 @@ func TestUnique(t *testing.T) {
|
|||||||
assert.Equal([]string{"a", "b", "c"}, Unique([]string{"a", "a", "b", "c"}))
|
assert.Equal([]string{"a", "b", "c"}, Unique([]string{"a", "a", "b", "c"}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUniqueBy(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestUniqueBy")
|
||||||
|
|
||||||
|
actual := UniqueBy([]int{1, 2, 3, 4, 5, 6}, func(val int) int {
|
||||||
|
return val % 4
|
||||||
|
})
|
||||||
|
assert.Equal([]int{1, 2, 3, 0}, actual)
|
||||||
|
}
|
||||||
|
|
||||||
func TestUnion(t *testing.T) {
|
func TestUnion(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestUnion")
|
assert := internal.NewAssert(t, "TestUnion")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user