mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-10 15:52:27 +08:00
Compare commits
47 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
205fedb197 | ||
|
|
4c864da62d | ||
|
|
263ab7e316 | ||
|
|
809b7a53df | ||
|
|
61c43daabb | ||
|
|
18914ee2cd | ||
|
|
0a8058956f | ||
|
|
a044da7d2f | ||
|
|
f8b785c4cb | ||
|
|
82c8a04c35 | ||
|
|
280ecb5cef | ||
|
|
ec27ad4c40 | ||
|
|
d66f92cd68 | ||
|
|
d8ed692651 | ||
|
|
a16de97d1d | ||
|
|
6f458e4367 | ||
|
|
37c7508ad0 | ||
|
|
acb5844b15 | ||
|
|
76f4eeea16 | ||
|
|
5466a23019 | ||
|
|
5692982dd1 | ||
|
|
c39c8914fb | ||
|
|
29bdca1bd2 | ||
|
|
eb66d038ac | ||
|
|
a99ada5ee1 | ||
|
|
a87faf5453 | ||
|
|
ab6fec2f69 | ||
|
|
7b290989f5 | ||
|
|
5722c724e6 | ||
|
|
f84584ca04 | ||
|
|
80cbbdc787 | ||
|
|
be148e07ba | ||
|
|
d36ab5cc3a | ||
|
|
2b17329094 | ||
|
|
f869a0a670 | ||
|
|
be000a4bd6 | ||
|
|
81efa800ea | ||
|
|
a783de57a8 | ||
|
|
a622959a78 | ||
|
|
f709dd53ce | ||
|
|
ee9e9625e2 | ||
|
|
84da7d4f27 | ||
|
|
089fd4e13c | ||
|
|
6c40e02324 | ||
|
|
a270b1b634 | ||
|
|
260fb795d3 | ||
|
|
8c036f830c |
85
README.md
85
README.md
@@ -4,12 +4,13 @@
|
|||||||
<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)
|
||||||
[](https://codecov.io/gh/duke-git/lancet)
|
[](https://codecov.io/gh/duke-git/lancet)
|
||||||
[](https://github.com/duke-git/lancet/blob/main/LICENSE)
|
[](https://github.com/duke-git/lancet/blob/main/LICENSE)
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -18,7 +19,7 @@
|
|||||||
Lancet is a comprehensive, efficient, and reusable util function library of go. Inspired by the java apache common package and lodash.js.
|
Lancet is a comprehensive, efficient, and reusable util function library of go. Inspired by the java apache common package and lodash.js.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
English | [简体中文](./README_zh-CN.md)
|
English | [简体中文](./README_zh-CN.md) | [Website](https://uvdream.github.io/lancet-docs/en/)
|
||||||
|
|
||||||
## Feature
|
## Feature
|
||||||
|
|
||||||
@@ -28,16 +29,19 @@ English | [简体中文](./README_zh-CN.md)
|
|||||||
- 🌍 Unit test for every exported function.
|
- 🌍 Unit test for every exported function.
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
### Note:
|
### Note:
|
||||||
|
|
||||||
1. <b>For users who use go1.18 and above, it is recommended to install lancet v2.x.x. Cause v2.x.x rewrite all functions with generics of go1.18.</b>
|
1. <b>For users who use go1.18 and above, it is recommended to install lancet v2.x.x. Cause v2.x.x rewrite all functions with generics of go1.18.</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
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.3.2. </b>
|
2. <b>For users who use version below go1.18, you should install v1.x.x. now latest v1 is v1.3.4. </b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
go get github.com/duke-git/lancet@v1.3.2 // below go1.18, install latest version of v1.x.x
|
go get github.com/duke-git/lancet@v1.3.3 // below go1.18, install latest version of v1.x.x
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
@@ -68,13 +72,17 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
## API Documentation
|
## API Documentation
|
||||||
|
|
||||||
## [lancet API doc](https://uvdream.github.io/lancet-docs/) Thanks [@UvDream](https://github.com/UvDream) for contributing.
|
## [lancet API doc](https://uvdream.github.io/lancet-docs/) Thanks [@UvDream](https://github.com/UvDream) for contributing.
|
||||||
|
|
||||||
### 1. Algorithm package implements some basic algorithm. eg. sort, search.
|
### 1. Algorithm package implements some basic algorithm. eg. sort, search.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/algorithm"
|
import "github.com/duke-git/lancet/v2/algorithm"
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Function list:
|
#### Function list:
|
||||||
|
|
||||||
- [BubbleSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#BubbleSort)
|
- [BubbleSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#BubbleSort)
|
||||||
- [CountSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#CountSort)
|
- [CountSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#CountSort)
|
||||||
- [HeapSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#HeapSort)
|
- [HeapSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#HeapSort)
|
||||||
@@ -88,14 +96,14 @@ import "github.com/duke-git/lancet/v2/algorithm"
|
|||||||
- [LinearSearch](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#LinearSearch)
|
- [LinearSearch](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#LinearSearch)
|
||||||
- [LRUCache](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#LRUCache)
|
- [LRUCache](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#LRUCache)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### 2. Concurrency package contain some functions to support concurrent programming. eg, goroutine, channel, async.
|
### 2. Concurrency package contain some functions to support concurrent programming. eg, goroutine, channel, async.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/concurrency"
|
import "github.com/duke-git/lancet/v2/concurrency"
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Function list:
|
#### Function list:
|
||||||
|
|
||||||
- [NewChannel](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#NewChannel)
|
- [NewChannel](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#NewChannel)
|
||||||
- [Bridge](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Bridge)
|
- [Bridge](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Bridge)
|
||||||
- [FanIn](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#FanIn)
|
- [FanIn](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#FanIn)
|
||||||
@@ -107,13 +115,14 @@ import "github.com/duke-git/lancet/v2/concurrency"
|
|||||||
- [Take](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Take)
|
- [Take](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Take)
|
||||||
- [Tee](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Tee)
|
- [Tee](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Tee)
|
||||||
|
|
||||||
|
|
||||||
### 3. Condition package contains some functions for conditional judgment. eg. And, Or, TernaryOperator...
|
### 3. Condition package contains some functions for conditional judgment. eg. And, Or, TernaryOperator...
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/condition"
|
import "github.com/duke-git/lancet/v2/condition"
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Function list:
|
#### Function list:
|
||||||
|
|
||||||
- [Bool](https://github.com/duke-git/lancet/blob/main/docs/condition.md#Bool)
|
- [Bool](https://github.com/duke-git/lancet/blob/main/docs/condition.md#Bool)
|
||||||
- [And](https://github.com/duke-git/lancet/blob/main/docs/condition.md#And)
|
- [And](https://github.com/duke-git/lancet/blob/main/docs/condition.md#And)
|
||||||
- [Or](https://github.com/duke-git/lancet/blob/main/docs/condition.md#Or)
|
- [Or](https://github.com/duke-git/lancet/blob/main/docs/condition.md#Or)
|
||||||
@@ -127,7 +136,9 @@ import "github.com/duke-git/lancet/v2/condition"
|
|||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/convertor"
|
import "github.com/duke-git/lancet/v2/convertor"
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Function list:
|
#### Function list:
|
||||||
|
|
||||||
- [ColorHexToRGB](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#ColorHexToRGB)
|
- [ColorHexToRGB](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#ColorHexToRGB)
|
||||||
- [ColorRGBToHex](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#ColorRGBToHex)
|
- [ColorRGBToHex](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#ColorRGBToHex)
|
||||||
- [ToBool](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#ToBool)
|
- [ToBool](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#ToBool)
|
||||||
@@ -152,6 +163,7 @@ import "github.com/duke-git/lancet/v2/cryptor"
|
|||||||
```
|
```
|
||||||
|
|
||||||
#### Function list:
|
#### Function list:
|
||||||
|
|
||||||
- [AesEcbEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#AesEcbEncrypt)
|
- [AesEcbEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#AesEcbEncrypt)
|
||||||
- [AesEcbDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#AesEcbDecrypt)
|
- [AesEcbDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#AesEcbDecrypt)
|
||||||
- [AesCbcEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#AesCbcEncrypt)
|
- [AesCbcEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#AesCbcEncrypt)
|
||||||
@@ -187,11 +199,12 @@ import "github.com/duke-git/lancet/v2/cryptor"
|
|||||||
|
|
||||||
### 6. Datetime package supports date and time format and compare.
|
### 6. Datetime package supports date and time format and compare.
|
||||||
|
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/datetime"
|
import "github.com/duke-git/lancet/v2/datetime"
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Function list:
|
#### Function list:
|
||||||
|
|
||||||
- [AddDay](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddDay)
|
- [AddDay](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddDay)
|
||||||
- [AddHour](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddHour)
|
- [AddHour](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddHour)
|
||||||
- [AddMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddMinute)
|
- [AddMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddMinute)
|
||||||
@@ -223,11 +236,8 @@ import "github.com/duke-git/lancet/v2/datetime"
|
|||||||
- [ToFormatForTpl](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToFormatForTpl)
|
- [ToFormatForTpl](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToFormatForTpl)
|
||||||
- [ToIso8601](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToIso8601)
|
- [ToIso8601](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToIso8601)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### 7. Datastructure package constains some common data structure. eg. list, linklist, stack, queue, set, tree, graph.
|
### 7. Datastructure package constains some common data structure. eg. list, linklist, stack, queue, set, tree, graph.
|
||||||
|
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import list "github.com/duke-git/lancet/v2/datastructure/list"
|
import list "github.com/duke-git/lancet/v2/datastructure/list"
|
||||||
import link "github.com/duke-git/lancet/v2/datastructure/link"
|
import link "github.com/duke-git/lancet/v2/datastructure/link"
|
||||||
@@ -238,7 +248,9 @@ import tree "github.com/duke-git/lancet/v2/datastructure/tree"
|
|||||||
import heap "github.com/duke-git/lancet/v2/datastructure/heap"
|
import heap "github.com/duke-git/lancet/v2/datastructure/heap"
|
||||||
import hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
|
import hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Function list:
|
#### Function list:
|
||||||
|
|
||||||
- [List](https://github.com/duke-git/lancet/blob/main/docs/datastructure/list.md)
|
- [List](https://github.com/duke-git/lancet/blob/main/docs/datastructure/list.md)
|
||||||
- [Linklist](https://github.com/duke-git/lancet/blob/main/docs/datastructure/linklist.md)
|
- [Linklist](https://github.com/duke-git/lancet/blob/main/docs/datastructure/linklist.md)
|
||||||
- [Stack](https://github.com/duke-git/lancet/blob/main/docs/datastructure/stack.md)
|
- [Stack](https://github.com/duke-git/lancet/blob/main/docs/datastructure/stack.md)
|
||||||
@@ -248,7 +260,6 @@ import hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
|
|||||||
- [Heap](https://github.com/duke-git/lancet/blob/main/docs/datastructure/heap.md)
|
- [Heap](https://github.com/duke-git/lancet/blob/main/docs/datastructure/heap.md)
|
||||||
- [HashMap](https://github.com/duke-git/lancet/blob/main/docs/datastructure/hashmap.md)
|
- [HashMap](https://github.com/duke-git/lancet/blob/main/docs/datastructure/hashmap.md)
|
||||||
|
|
||||||
|
|
||||||
### 8. Fileutil package implements some basic functions for file operations.
|
### 8. Fileutil package implements some basic functions for file operations.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -278,7 +289,9 @@ import "github.com/duke-git/lancet/v2/fileutil"
|
|||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/formatter"
|
import "github.com/duke-git/lancet/v2/formatter"
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Function list:
|
#### Function list:
|
||||||
|
|
||||||
- [Comma](https://github.com/duke-git/lancet/blob/main/docs/formatter.md#Comma)
|
- [Comma](https://github.com/duke-git/lancet/blob/main/docs/formatter.md#Comma)
|
||||||
|
|
||||||
### 10. Function package can control the flow of function execution and support part of functional programming
|
### 10. Function package can control the flow of function execution and support part of functional programming
|
||||||
@@ -288,6 +301,7 @@ import "github.com/duke-git/lancet/v2/function"
|
|||||||
```
|
```
|
||||||
|
|
||||||
#### Function list:
|
#### Function list:
|
||||||
|
|
||||||
- [After](https://github.com/duke-git/lancet/blob/main/docs/function.md#After)
|
- [After](https://github.com/duke-git/lancet/blob/main/docs/function.md#After)
|
||||||
- [Before](https://github.com/duke-git/lancet/blob/main/docs/function.md#Before)
|
- [Before](https://github.com/duke-git/lancet/blob/main/docs/function.md#Before)
|
||||||
- [Curry](https://github.com/duke-git/lancet/blob/main/docs/function.md#Curry)
|
- [Curry](https://github.com/duke-git/lancet/blob/main/docs/function.md#Curry)
|
||||||
@@ -297,7 +311,6 @@ import "github.com/duke-git/lancet/v2/function"
|
|||||||
- [Pipeline](https://github.com/duke-git/lancet/blob/main/docs/function.md#Pipeline)
|
- [Pipeline](https://github.com/duke-git/lancet/blob/main/docs/function.md#Pipeline)
|
||||||
- [Watcher](https://github.com/duke-git/lancet/blob/main/docs/function.md#Watcher)
|
- [Watcher](https://github.com/duke-git/lancet/blob/main/docs/function.md#Watcher)
|
||||||
|
|
||||||
|
|
||||||
### 11. Maputil package includes some functions to manipulate map.
|
### 11. Maputil package includes some functions to manipulate map.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -305,6 +318,7 @@ import "github.com/duke-git/lancet/v2/maputil"
|
|||||||
```
|
```
|
||||||
|
|
||||||
#### Function list:
|
#### Function list:
|
||||||
|
|
||||||
- [ForEach](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ForEach)
|
- [ForEach](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ForEach)
|
||||||
- [Filter](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Filter)
|
- [Filter](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Filter)
|
||||||
- [Intersect](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Intersect)
|
- [Intersect](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Intersect)
|
||||||
@@ -314,7 +328,6 @@ import "github.com/duke-git/lancet/v2/maputil"
|
|||||||
- [Values](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Values)
|
- [Values](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Values)
|
||||||
- [IsDisjoint](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#IsDisjoint)
|
- [IsDisjoint](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#IsDisjoint)
|
||||||
|
|
||||||
|
|
||||||
### 12. Mathutil package implements some functions for math calculation.
|
### 12. Mathutil package implements some functions for math calculation.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -322,6 +335,7 @@ import "github.com/duke-git/lancet/v2/mathutil"
|
|||||||
```
|
```
|
||||||
|
|
||||||
#### Function list:
|
#### Function list:
|
||||||
|
|
||||||
- [Average](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Average)
|
- [Average](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Average)
|
||||||
- [Exponent](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Exponent)
|
- [Exponent](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Exponent)
|
||||||
- [Fibonacci](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Fibonacci)
|
- [Fibonacci](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Fibonacci)
|
||||||
@@ -335,7 +349,6 @@ import "github.com/duke-git/lancet/v2/mathutil"
|
|||||||
- [RoundToString](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#RoundToString)
|
- [RoundToString](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#RoundToString)
|
||||||
- [TruncRound](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#TruncRound)
|
- [TruncRound](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#TruncRound)
|
||||||
|
|
||||||
|
|
||||||
### 13. Netutil package contains functions to get net information and send http request.
|
### 13. Netutil package contains functions to get net information and send http request.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -343,6 +356,7 @@ import "github.com/duke-git/lancet/v2/netutil"
|
|||||||
```
|
```
|
||||||
|
|
||||||
#### Function list:
|
#### Function list:
|
||||||
|
|
||||||
- [ConvertMapToQueryString](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#ConvertMapToQueryString)
|
- [ConvertMapToQueryString](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#ConvertMapToQueryString)
|
||||||
- [EncodeUrl](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#EncodeUrl)
|
- [EncodeUrl](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#EncodeUrl)
|
||||||
- [GetInternalIp](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetInternalIp)
|
- [GetInternalIp](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetInternalIp)
|
||||||
@@ -358,11 +372,11 @@ import "github.com/duke-git/lancet/v2/netutil"
|
|||||||
- [DecodeResponse](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#DecodeResponse)
|
- [DecodeResponse](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#DecodeResponse)
|
||||||
- [StructToUrlValues](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#StructToUrlValues)
|
- [StructToUrlValues](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#StructToUrlValues)
|
||||||
|
|
||||||
- [HttpGet<sup>Deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpGet)
|
- [HttpGet<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpGet)
|
||||||
- [HttpDelete<sup>Deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpDelete)
|
- [HttpDelete<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpDelete)
|
||||||
- [HttpPost<sup>Deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpPost)
|
- [HttpPost<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpPost)
|
||||||
- [HttpPut<sup>Deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpPut)
|
- [HttpPut<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpPut)
|
||||||
- [HttpPatch<sup>Deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpPatch)
|
- [HttpPatch<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpPatch)
|
||||||
- [ParseHttpResponse](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#ParseHttpResponse)
|
- [ParseHttpResponse](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#ParseHttpResponse)
|
||||||
|
|
||||||
### 14. Random package implements some basic functions to generate random int and string.
|
### 14. Random package implements some basic functions to generate random int and string.
|
||||||
@@ -372,9 +386,14 @@ import "github.com/duke-git/lancet/v2/random"
|
|||||||
```
|
```
|
||||||
|
|
||||||
#### Function list:
|
#### Function list:
|
||||||
|
|
||||||
- [RandBytes](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandBytes)
|
- [RandBytes](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandBytes)
|
||||||
- [RandInt](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandInt)
|
- [RandInt](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandInt)
|
||||||
- [RandString](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandString)
|
- [RandString](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandString)
|
||||||
|
- [RandUpper](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandUpper)
|
||||||
|
- [RandLower](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandLower)
|
||||||
|
- [RandNumeral](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandNumeral)
|
||||||
|
- [RandNumeralOrLetter](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandNumeralOrLetter)
|
||||||
- [UUIdV4](https://github.com/duke-git/lancet/blob/main/docs/random.md#UUIdV4)
|
- [UUIdV4](https://github.com/duke-git/lancet/blob/main/docs/random.md#UUIdV4)
|
||||||
|
|
||||||
### 15. Retry package is for executing a function repeatedly until it was successful or canceled by the context.
|
### 15. Retry package is for executing a function repeatedly until it was successful or canceled by the context.
|
||||||
@@ -384,6 +403,7 @@ import "github.com/duke-git/lancet/v2/retry"
|
|||||||
```
|
```
|
||||||
|
|
||||||
#### Function list:
|
#### Function list:
|
||||||
|
|
||||||
- [Context](https://github.com/duke-git/lancet/blob/main/docs/retry.md#Context)
|
- [Context](https://github.com/duke-git/lancet/blob/main/docs/retry.md#Context)
|
||||||
- [Retry](https://github.com/duke-git/lancet/blob/main/docs/retry.md#Retry)
|
- [Retry](https://github.com/duke-git/lancet/blob/main/docs/retry.md#Retry)
|
||||||
- [RetryFunc](https://github.com/duke-git/lancet/blob/main/docs/retry.md#RetryFunc)
|
- [RetryFunc](https://github.com/duke-git/lancet/blob/main/docs/retry.md#RetryFunc)
|
||||||
@@ -397,6 +417,7 @@ import "github.com/duke-git/lancet/v2/slice"
|
|||||||
```
|
```
|
||||||
|
|
||||||
#### Function list:
|
#### Function list:
|
||||||
|
|
||||||
- [AppendIfAbsent](https://github.com/duke-git/lancet/blob/main/docs/slice.md#AppendIfAbsent)
|
- [AppendIfAbsent](https://github.com/duke-git/lancet/blob/main/docs/slice.md#AppendIfAbsent)
|
||||||
- [Contain](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Contain)
|
- [Contain](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Contain)
|
||||||
- [ContainSubSlice](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ContainSubSlice)
|
- [ContainSubSlice](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ContainSubSlice)
|
||||||
@@ -404,6 +425,7 @@ import "github.com/duke-git/lancet/v2/slice"
|
|||||||
- [Compact](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Compact)
|
- [Compact](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Compact)
|
||||||
- [Concat](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Concat)
|
- [Concat](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Concat)
|
||||||
- [Count](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Count)
|
- [Count](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Count)
|
||||||
|
- [CountBy](https://github.com/duke-git/lancet/blob/main/docs/slice.md#CountBy)
|
||||||
- [Difference](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Difference)
|
- [Difference](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Difference)
|
||||||
- [DifferenceBy](https://github.com/duke-git/lancet/blob/main/docs/slice.md#DifferenceBy)
|
- [DifferenceBy](https://github.com/duke-git/lancet/blob/main/docs/slice.md#DifferenceBy)
|
||||||
- [DifferenceWith](https://github.com/duke-git/lancet/blob/main/docs/slice.md#DifferenceWith)
|
- [DifferenceWith](https://github.com/duke-git/lancet/blob/main/docs/slice.md#DifferenceWith)
|
||||||
@@ -420,31 +442,38 @@ import "github.com/duke-git/lancet/v2/slice"
|
|||||||
- [ForEach](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ForEach)
|
- [ForEach](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ForEach)
|
||||||
- [GroupBy](https://github.com/duke-git/lancet/blob/main/docs/slice.md#GroupBy)
|
- [GroupBy](https://github.com/duke-git/lancet/blob/main/docs/slice.md#GroupBy)
|
||||||
- [GroupWith](https://github.com/duke-git/lancet/blob/main/docs/slice.md#GroupWith)
|
- [GroupWith](https://github.com/duke-git/lancet/blob/main/docs/slice.md#GroupWith)
|
||||||
- [IntSlice](https://github.com/duke-git/lancet/blob/main/docs/slice.md#IntSlice)
|
- [IntSlice<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/slice.md#IntSlice)
|
||||||
- [InterfaceSlice](https://github.com/duke-git/lancet/blob/main/docs/slice.md#InterfaceSlice)
|
- [InterfaceSlice<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/slice.md#InterfaceSlice)
|
||||||
- [Intersection](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Intersection)
|
- [Intersection](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Intersection)
|
||||||
- [InsertAt](https://github.com/duke-git/lancet/blob/main/docs/slice.md#InsertAt)
|
- [InsertAt](https://github.com/duke-git/lancet/blob/main/docs/slice.md#InsertAt)
|
||||||
- [IndexOf](https://github.com/duke-git/lancet/blob/main/docs/slice.md#IndexOf)
|
- [IndexOf](https://github.com/duke-git/lancet/blob/main/docs/slice.md#IndexOf)
|
||||||
- [LastIndexOf](https://github.com/duke-git/lancet/blob/main/docs/slice.md#LastIndexOf)
|
- [LastIndexOf](https://github.com/duke-git/lancet/blob/main/docs/slice.md#LastIndexOf)
|
||||||
- [Map](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Map)
|
- [Map](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Map)
|
||||||
|
- [Merge](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Merge)
|
||||||
- [Reverse](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Reverse)
|
- [Reverse](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Reverse)
|
||||||
- [Reduce](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Reduce)
|
- [Reduce](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Reduce)
|
||||||
- [Replace](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Replace)
|
- [Replace](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Replace)
|
||||||
- [ReplaceAll](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ReplaceAll)
|
- [ReplaceAll](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ReplaceAll)
|
||||||
|
- [Repeat](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Repeat)
|
||||||
- [Shuffle](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Shuffle)
|
- [Shuffle](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Shuffle)
|
||||||
- [SortByField](https://github.com/duke-git/lancet/blob/main/docs/slice.md#SortByField)
|
- [Sort](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Sort)
|
||||||
|
- [SortBy](https://github.com/duke-git/lancet/blob/main/docs/slice.md#SortBy)
|
||||||
|
- [SortByField<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/slice.md#SortByField)
|
||||||
- [Some](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Some)
|
- [Some](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Some)
|
||||||
- [StringSlice](https://github.com/duke-git/lancet/blob/main/docs/slice.md#StringSlice)
|
- [StringSlice<sup>deprecated</sup>](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)
|
||||||
- [ToSlice](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ToSlice)
|
- [ToSlice](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ToSlice)
|
||||||
- [ToSlicePointer](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ToSlicePointer)
|
- [ToSlicePointer](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ToSlicePointer)
|
||||||
- [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)
|
- [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)
|
||||||
|
- [UnionBy](https://github.com/duke-git/lancet/blob/main/docs/slice.md#UnionBy)
|
||||||
- [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)
|
||||||
|
- [KeyBy](https://github.com/duke-git/lancet/blob/main/docs/slice.md#KeyBy)
|
||||||
|
|
||||||
### 17. Strutil package contains some functions to manipulate string.
|
### 17. Strutil package contains some functions to manipulate string.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/strutil"
|
import "github.com/duke-git/lancet/v2/strutil"
|
||||||
```
|
```
|
||||||
@@ -474,7 +503,9 @@ import "github.com/duke-git/lancet/v2/strutil"
|
|||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/system"
|
import "github.com/duke-git/lancet/v2/system"
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Function list:
|
#### Function list:
|
||||||
|
|
||||||
- [IsWindows](https://github.com/duke-git/lancet/blob/main/docs/system.md#IsWindows)
|
- [IsWindows](https://github.com/duke-git/lancet/blob/main/docs/system.md#IsWindows)
|
||||||
- [IsLinux](https://github.com/duke-git/lancet/blob/main/docs/system.md#IsLinux)
|
- [IsLinux](https://github.com/duke-git/lancet/blob/main/docs/system.md#IsLinux)
|
||||||
- [IsMac](https://github.com/duke-git/lancet/blob/main/docs/system.md#IsMac)
|
- [IsMac](https://github.com/duke-git/lancet/blob/main/docs/system.md#IsMac)
|
||||||
@@ -520,14 +551,16 @@ import "github.com/duke-git/lancet/v2/validator"
|
|||||||
- [IsUrl](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsUrl)
|
- [IsUrl](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsUrl)
|
||||||
- [IsWeakPassword](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsWeakPassword)
|
- [IsWeakPassword](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsWeakPassword)
|
||||||
- [IsZeroValue](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsZeroValue)
|
- [IsZeroValue](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsZeroValue)
|
||||||
|
|
||||||
### 20. xerror package implements helpers for errors.
|
### 20. xerror package implements helpers for errors.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/xerror"
|
import "github.com/duke-git/lancet/v2/xerror"
|
||||||
```
|
```
|
||||||
#### Function list:
|
|
||||||
- [Unwrap](https://github.com/duke-git/lancet/blob/main/docs/xerror.md#Unwrap)
|
|
||||||
|
|
||||||
|
#### Function list:
|
||||||
|
|
||||||
|
- [Unwrap](https://github.com/duke-git/lancet/blob/main/docs/xerror.md#Unwrap)
|
||||||
|
|
||||||
## How to Contribute
|
## How to Contribute
|
||||||
|
|
||||||
|
|||||||
@@ -4,12 +4,13 @@
|
|||||||
<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)
|
||||||
[](https://codecov.io/gh/duke-git/lancet)
|
[](https://codecov.io/gh/duke-git/lancet)
|
||||||
[](https://github.com/duke-git/lancet/blob/main/LICENSE)
|
[](https://github.com/duke-git/lancet/blob/main/LICENSE)
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -17,8 +18,7 @@
|
|||||||
lancet(柳叶刀)是一个全面、高效、可复用的go语言工具函数库。 lancet受到了java apache common包和lodash.js的启发。
|
lancet(柳叶刀)是一个全面、高效、可复用的go语言工具函数库。 lancet受到了java apache common包和lodash.js的启发。
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
简体中文 | [English](./README.md)
|
简体中文 | [English](./README.md) | [文档](https://uvdream.github.io/lancet-docs)
|
||||||
|
|
||||||
|
|
||||||
## 特性
|
## 特性
|
||||||
|
|
||||||
@@ -28,16 +28,19 @@
|
|||||||
- 🌍 所有导出函数单元测试覆盖率 100%
|
- 🌍 所有导出函数单元测试覆盖率 100%
|
||||||
|
|
||||||
## 安装
|
## 安装
|
||||||
|
|
||||||
### Note:
|
### Note:
|
||||||
|
|
||||||
1. <b>对于使用 go1.18 及以上的用户,建议安装 v2.x.x。 因为 v2.x.x 用 go1.18 的泛型重写了大部分函数。</b>
|
1. <b>对于使用 go1.18 及以上的用户,建议安装 v2.x.x。 因为 v2.x.x 用 go1.18 的泛型重写了大部分函数。</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
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.3.2。</b>
|
2. <b>使用 go1.18 以下版本的用户,必须安装 v1.x.x。目前最新的 v1 版本是 v1.3.4。</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
go get github.com/duke-git/lancet@v1.3.2 // 使用go1.18以下版本, 必须安装v1.x.x版本
|
go get github.com/duke-git/lancet@v1.3.3 // 使用go1.18以下版本, 必须安装v1.x.x版本
|
||||||
```
|
```
|
||||||
|
|
||||||
## 用法
|
## 用法
|
||||||
@@ -68,13 +71,17 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
## API 文档
|
## API 文档
|
||||||
|
|
||||||
## [lancet API doc](https://uvdream.github.io/lancet-docs/) 感谢[@UvDream](https://github.com/UvDream)整理
|
## [lancet API doc](https://uvdream.github.io/lancet-docs/) 感谢[@UvDream](https://github.com/UvDream)整理
|
||||||
|
|
||||||
### 1. algorithm 算法包实现一些基本算法。eg. sort, search.
|
### 1. algorithm 算法包实现一些基本算法。eg. sort, search.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/algorithm"
|
import "github.com/duke-git/lancet/v2/algorithm"
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Function list:
|
#### Function list:
|
||||||
|
|
||||||
- [BubbleSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#BubbleSort)
|
- [BubbleSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#BubbleSort)
|
||||||
- [CountSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#CountSort)
|
- [CountSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#CountSort)
|
||||||
- [HeapSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#HeapSort)
|
- [HeapSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#HeapSort)
|
||||||
@@ -88,13 +95,14 @@ import "github.com/duke-git/lancet/v2/algorithm"
|
|||||||
- [LinearSearch](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LinearSearch)
|
- [LinearSearch](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LinearSearch)
|
||||||
- [LRUCache](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LRUCache)
|
- [LRUCache](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LRUCache)
|
||||||
|
|
||||||
|
|
||||||
### 2. concurrency 并发包包含一些支持并发编程的功能。例如:goroutine, channel, async 等。
|
### 2. concurrency 并发包包含一些支持并发编程的功能。例如:goroutine, channel, async 等。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/concurrency"
|
import "github.com/duke-git/lancet/v2/concurrency"
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Function list:
|
#### Function list:
|
||||||
|
|
||||||
- [NewChannel](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#NewChannel)
|
- [NewChannel](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#NewChannel)
|
||||||
- [Bridge](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Bridge)
|
- [Bridge](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Bridge)
|
||||||
- [FanIn](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#FanIn)
|
- [FanIn](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#FanIn)
|
||||||
@@ -106,13 +114,14 @@ import "github.com/duke-git/lancet/v2/concurrency"
|
|||||||
- [Take](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Take)
|
- [Take](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Take)
|
||||||
- [Tee](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Tee)
|
- [Tee](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Tee)
|
||||||
|
|
||||||
|
|
||||||
### 3. condition 条件包含一些用于条件判断的函数。eg. And, Or, TernaryOperator...
|
### 3. condition 条件包含一些用于条件判断的函数。eg. And, Or, TernaryOperator...
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/condition"
|
import "github.com/duke-git/lancet/v2/condition"
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Function list:
|
#### Function list:
|
||||||
|
|
||||||
- [Bool](https://github.com/duke-git/lancet/blob/main/docs/condition_zh-CN.md#Bool)
|
- [Bool](https://github.com/duke-git/lancet/blob/main/docs/condition_zh-CN.md#Bool)
|
||||||
- [And](https://github.com/duke-git/lancet/blob/main/docs/condition_zh-CN.md#And)
|
- [And](https://github.com/duke-git/lancet/blob/main/docs/condition_zh-CN.md#And)
|
||||||
- [Or](https://github.com/duke-git/lancet/blob/main/docs/condition_zh-CN.md#Or)
|
- [Or](https://github.com/duke-git/lancet/blob/main/docs/condition_zh-CN.md#Or)
|
||||||
@@ -126,7 +135,9 @@ import "github.com/duke-git/lancet/v2/condition"
|
|||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/convertor"
|
import "github.com/duke-git/lancet/v2/convertor"
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 函数列表:
|
#### 函数列表:
|
||||||
|
|
||||||
- [ColorHexToRGB](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#ColorHexToRGB)
|
- [ColorHexToRGB](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#ColorHexToRGB)
|
||||||
- [ColorRGBToHex](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#ColorRGBToHex)
|
- [ColorRGBToHex](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#ColorRGBToHex)
|
||||||
- [ToBool](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#ToBool)
|
- [ToBool](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#ToBool)
|
||||||
@@ -151,6 +162,7 @@ import "github.com/duke-git/lancet/v2/cryptor"
|
|||||||
```
|
```
|
||||||
|
|
||||||
#### 函数列表:
|
#### 函数列表:
|
||||||
|
|
||||||
- [AesEcbEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#AesEcbEncrypt)
|
- [AesEcbEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#AesEcbEncrypt)
|
||||||
- [AesEcbDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#AesEcbDecrypt)
|
- [AesEcbDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#AesEcbDecrypt)
|
||||||
- [AesCbcEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#AesCbcEncrypt)
|
- [AesCbcEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#AesCbcEncrypt)
|
||||||
@@ -186,11 +198,12 @@ import "github.com/duke-git/lancet/v2/cryptor"
|
|||||||
|
|
||||||
### 6. datetime 日期时间处理包,格式化日期,比较日期。
|
### 6. datetime 日期时间处理包,格式化日期,比较日期。
|
||||||
|
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/datetime"
|
import "github.com/duke-git/lancet/v2/datetime"
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 函数列表:
|
#### 函数列表:
|
||||||
|
|
||||||
- [AddDay](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddDay)
|
- [AddDay](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddDay)
|
||||||
- [AddHour](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddHour)
|
- [AddHour](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddHour)
|
||||||
- [AddMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddMinute)
|
- [AddMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddMinute)
|
||||||
@@ -222,10 +235,8 @@ import "github.com/duke-git/lancet/v2/datetime"
|
|||||||
- [ToFormatForTpl](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToFormatForTpl)
|
- [ToFormatForTpl](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToFormatForTpl)
|
||||||
- [ToIso8601](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToIso8601)
|
- [ToIso8601](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToIso8601)
|
||||||
|
|
||||||
|
|
||||||
### 7. datastructure 包含一些普通的数据结构实现。例如:list, linklist, stack, queue, set, tree, graph.
|
### 7. datastructure 包含一些普通的数据结构实现。例如:list, linklist, stack, queue, set, tree, graph.
|
||||||
|
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import list "github.com/duke-git/lancet/v2/datastructure/list"
|
import list "github.com/duke-git/lancet/v2/datastructure/list"
|
||||||
import link "github.com/duke-git/lancet/v2/datastructure/link"
|
import link "github.com/duke-git/lancet/v2/datastructure/link"
|
||||||
@@ -236,7 +247,9 @@ import tree "github.com/duke-git/lancet/v2/datastructure/tree"
|
|||||||
import heap "github.com/duke-git/lancet/v2/datastructure/heap"
|
import heap "github.com/duke-git/lancet/v2/datastructure/heap"
|
||||||
import hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
|
import hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Function list:
|
#### Function list:
|
||||||
|
|
||||||
- [List](https://github.com/duke-git/lancet/blob/main/docs/datastructure/list_zh-CN.md)
|
- [List](https://github.com/duke-git/lancet/blob/main/docs/datastructure/list_zh-CN.md)
|
||||||
- [Linklist](https://github.com/duke-git/lancet/blob/main/docs/datastructure/linklist_zh-CN.md)
|
- [Linklist](https://github.com/duke-git/lancet/blob/main/docs/datastructure/linklist_zh-CN.md)
|
||||||
- [Stack](https://github.com/duke-git/lancet/blob/main/docs/datastructure/stack_zh-CN.md)
|
- [Stack](https://github.com/duke-git/lancet/blob/main/docs/datastructure/stack_zh-CN.md)
|
||||||
@@ -246,7 +259,6 @@ import hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
|
|||||||
- [Heap](https://github.com/duke-git/lancet/blob/main/docs/datastructure/heap.md)
|
- [Heap](https://github.com/duke-git/lancet/blob/main/docs/datastructure/heap.md)
|
||||||
- [HashMap](https://github.com/duke-git/lancet/blob/main/docs/datastructure/hashmap.md)
|
- [HashMap](https://github.com/duke-git/lancet/blob/main/docs/datastructure/hashmap.md)
|
||||||
|
|
||||||
|
|
||||||
### 8. fileutil 包支持文件基本操作。
|
### 8. fileutil 包支持文件基本操作。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -276,9 +288,10 @@ import "github.com/duke-git/lancet/v2/fileutil"
|
|||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/formatter"
|
import "github.com/duke-git/lancet/v2/formatter"
|
||||||
```
|
```
|
||||||
#### 函数列表:
|
|
||||||
- [Comma](https://github.com/duke-git/lancet/blob/main/docs/formatter_zh-CN.md#Comma)
|
|
||||||
|
|
||||||
|
#### 函数列表:
|
||||||
|
|
||||||
|
- [Comma](https://github.com/duke-git/lancet/blob/main/docs/formatter_zh-CN.md#Comma)
|
||||||
|
|
||||||
### 10. function 函数包控制函数执行流程,包含部分函数式编程。
|
### 10. function 函数包控制函数执行流程,包含部分函数式编程。
|
||||||
|
|
||||||
@@ -287,6 +300,7 @@ import "github.com/duke-git/lancet/v2/function"
|
|||||||
```
|
```
|
||||||
|
|
||||||
#### 函数列表:
|
#### 函数列表:
|
||||||
|
|
||||||
- [After](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#After)
|
- [After](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#After)
|
||||||
- [Before](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Before)
|
- [Before](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Before)
|
||||||
- [Curry](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Curry)
|
- [Curry](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Curry)
|
||||||
@@ -296,7 +310,6 @@ import "github.com/duke-git/lancet/v2/function"
|
|||||||
- [Pipeline](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Pipeline)
|
- [Pipeline](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Pipeline)
|
||||||
- [Watcher](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Watcher)
|
- [Watcher](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Watcher)
|
||||||
|
|
||||||
|
|
||||||
### 11. maputil 包包括一些操作 map 的函数.
|
### 11. maputil 包包括一些操作 map 的函数.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -304,6 +317,7 @@ import "github.com/duke-git/lancet/v2/maputil"
|
|||||||
```
|
```
|
||||||
|
|
||||||
#### 函数列表:
|
#### 函数列表:
|
||||||
|
|
||||||
- [ForEach](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ForEach)
|
- [ForEach](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ForEach)
|
||||||
- [Filter](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Filter)
|
- [Filter](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Filter)
|
||||||
- [Intersect](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Intersect)
|
- [Intersect](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Intersect)
|
||||||
@@ -320,6 +334,7 @@ import "github.com/duke-git/lancet/v2/mathutil"
|
|||||||
```
|
```
|
||||||
|
|
||||||
#### Function list:
|
#### Function list:
|
||||||
|
|
||||||
- [Average](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Average)
|
- [Average](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Average)
|
||||||
- [Exponent](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Exponent)
|
- [Exponent](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Exponent)
|
||||||
- [Fibonacci](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Fibonacci)
|
- [Fibonacci](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Fibonacci)
|
||||||
@@ -340,6 +355,7 @@ import "github.com/duke-git/lancet/v2/netutil"
|
|||||||
```
|
```
|
||||||
|
|
||||||
#### 函数列表:
|
#### 函数列表:
|
||||||
|
|
||||||
- [ConvertMapToQueryString](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#ConvertMapToQueryString)
|
- [ConvertMapToQueryString](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#ConvertMapToQueryString)
|
||||||
- [GetInternalIp](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#GetInternalIp)
|
- [GetInternalIp](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#GetInternalIp)
|
||||||
- [EncodeUrl](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#EncodeUrl)
|
- [EncodeUrl](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#EncodeUrl)
|
||||||
@@ -355,11 +371,11 @@ import "github.com/duke-git/lancet/v2/netutil"
|
|||||||
- [DecodeResponse](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#DecodeResponse)
|
- [DecodeResponse](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#DecodeResponse)
|
||||||
- [StructToUrlValues](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#StructToUrlValues)
|
- [StructToUrlValues](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#StructToUrlValues)
|
||||||
|
|
||||||
- [HttpGet<sup>Deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpGet)
|
- [HttpGet<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpGet)
|
||||||
- [HttpDelete<sup>Deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpDelete)
|
- [HttpDelete<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpDelete)
|
||||||
- [HttpPost<sup>Deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPost)
|
- [HttpPost<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPost)
|
||||||
- [HttpPut<sup>Deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPut)
|
- [HttpPut<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPut)
|
||||||
- [HttpPatch<sup>Deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPatch)
|
- [HttpPatch<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPatch)
|
||||||
- [ParseHttpResponse](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#ParseHttpResponse)
|
- [ParseHttpResponse](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#ParseHttpResponse)
|
||||||
|
|
||||||
### 14. random 随机数生成器包,可以生成随机[]bytes, int, string。
|
### 14. random 随机数生成器包,可以生成随机[]bytes, int, string。
|
||||||
@@ -369,10 +385,16 @@ import "github.com/duke-git/lancet/v2/random"
|
|||||||
```
|
```
|
||||||
|
|
||||||
#### 函数列表:
|
#### 函数列表:
|
||||||
|
|
||||||
- [RandBytes](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandBytes)
|
- [RandBytes](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandBytes)
|
||||||
- [RandInt](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandInt)
|
- [RandInt](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandInt)
|
||||||
- [RandString](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandString)
|
- [RandString](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandString)
|
||||||
|
- [RandUpper](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandUpper)
|
||||||
|
- [RandLower](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandLower)
|
||||||
|
- [RandNumeral](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandNumeral)
|
||||||
|
- [RandNumeralOrLetter](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandNumeralOrLetter)
|
||||||
- [UUIdV4](https://github.com/duke-git/lancet/blob/main/docs/random.md#UUIdV4)
|
- [UUIdV4](https://github.com/duke-git/lancet/blob/main/docs/random.md#UUIdV4)
|
||||||
|
|
||||||
### 15. retry 重试执行函数直到函数运行成功或被 context cancel。
|
### 15. retry 重试执行函数直到函数运行成功或被 context cancel。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -380,13 +402,13 @@ import "github.com/duke-git/lancet/v2/retry"
|
|||||||
```
|
```
|
||||||
|
|
||||||
#### 函数列表:
|
#### 函数列表:
|
||||||
|
|
||||||
- [Context](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#Context)
|
- [Context](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#Context)
|
||||||
- [Retry](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#Retry)
|
- [Retry](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#Retry)
|
||||||
- [RetryFunc](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#RetryFunc)
|
- [RetryFunc](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#RetryFunc)
|
||||||
- [RetryDuration](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#RetryDuration)
|
- [RetryDuration](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#RetryDuration)
|
||||||
- [RetryTimes](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#RetryTimes)
|
- [RetryTimes](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#RetryTimes)
|
||||||
|
|
||||||
|
|
||||||
### 16. slice 包包含操作切片的方法集合。
|
### 16. slice 包包含操作切片的方法集合。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -394,6 +416,7 @@ import "github.com/duke-git/lancet/v2/slice"
|
|||||||
```
|
```
|
||||||
|
|
||||||
#### 函数列表:
|
#### 函数列表:
|
||||||
|
|
||||||
- [AppendIfAbsent](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#AppendIfAbsent)
|
- [AppendIfAbsent](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#AppendIfAbsent)
|
||||||
- [Contain](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Contain)
|
- [Contain](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Contain)
|
||||||
- [ContainSubSlice](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ContainSubSlice)
|
- [ContainSubSlice](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ContainSubSlice)
|
||||||
@@ -401,6 +424,7 @@ import "github.com/duke-git/lancet/v2/slice"
|
|||||||
- [Compact](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Compact)
|
- [Compact](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Compact)
|
||||||
- [Concat](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Concat)
|
- [Concat](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Concat)
|
||||||
- [Count](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Count)
|
- [Count](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Count)
|
||||||
|
- [CountBy](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#CountBy)
|
||||||
- [Difference](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Difference)
|
- [Difference](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Difference)
|
||||||
- [DifferenceBy](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#DifferenceBy)
|
- [DifferenceBy](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#DifferenceBy)
|
||||||
- [DifferenceWith](https://github.com/duke-git/lancet/blob/main/docs/slice.md#DifferenceWith)
|
- [DifferenceWith](https://github.com/duke-git/lancet/blob/main/docs/slice.md#DifferenceWith)
|
||||||
@@ -415,30 +439,35 @@ import "github.com/duke-git/lancet/v2/slice"
|
|||||||
- [ForEach](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ForEach)
|
- [ForEach](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ForEach)
|
||||||
- [GroupBy](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#GroupBy)
|
- [GroupBy](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#GroupBy)
|
||||||
- [GroupWith](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#GroupWith)
|
- [GroupWith](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#GroupWith)
|
||||||
- [IntSlice](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#IntSlice)
|
- [IntSlice<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#IntSlice)
|
||||||
- [InterfaceSlice](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#InterfaceSlice)
|
- [InterfaceSlice<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#InterfaceSlice)
|
||||||
- [Intersection](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Intersection)
|
- [Intersection](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Intersection)
|
||||||
- [InsertAt](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#InsertAt)
|
- [InsertAt](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#InsertAt)
|
||||||
- [IndexOf](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#IndexOf)
|
- [IndexOf](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#IndexOf)
|
||||||
- [LastIndexOf](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#LastIndexOf)
|
- [LastIndexOf](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#LastIndexOf)
|
||||||
- [Map](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Map)
|
- [Map](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Map)
|
||||||
|
- [Merge](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Merge)
|
||||||
- [Reverse](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Reverse)
|
- [Reverse](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Reverse)
|
||||||
- [Reduce](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Reduce)
|
- [Reduce](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Reduce)
|
||||||
- [Replace](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Replace)
|
- [Replace](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Replace)
|
||||||
- [ReplaceAll](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ReplaceAll)
|
- [ReplaceAll](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ReplaceAll)
|
||||||
|
- [Repeat](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Repeat)
|
||||||
- [Shuffle](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Shuffle)
|
- [Shuffle](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Shuffle)
|
||||||
- [SortByField](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#SortByField)
|
- [Sort](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Sort)
|
||||||
|
- [SortBy](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#SortBy)
|
||||||
|
- [SortByField<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#SortByField)
|
||||||
- [Some](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Some)
|
- [Some](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Some)
|
||||||
- [StringSlice](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#StringSlice)
|
- [StringSlice<sup>deprecated</sup>](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)
|
||||||
- [ToSlice](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ToSlice)
|
- [ToSlice](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ToSlice)
|
||||||
- [ToSlicePointer](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ToSlicePointer)
|
- [ToSlicePointer](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ToSlicePointer)
|
||||||
- [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)
|
- [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)
|
||||||
|
- [UniqueBy](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#UniqueBy)
|
||||||
- [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)
|
||||||
|
- [KeyBy](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#KeyBy)
|
||||||
|
|
||||||
### 17. strutil 包含处理字符串的相关函数。
|
### 17. strutil 包含处理字符串的相关函数。
|
||||||
|
|
||||||
@@ -466,7 +495,6 @@ import "github.com/duke-git/lancet/v2/strutil"
|
|||||||
- [Wrap](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Wrap)
|
- [Wrap](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Wrap)
|
||||||
- [Unwrap](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Unwrap)
|
- [Unwrap](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Unwrap)
|
||||||
|
|
||||||
|
|
||||||
### 18. system 包含 os, runtime, shell command 相关函数。
|
### 18. system 包含 os, runtime, shell command 相关函数。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -474,6 +502,7 @@ import "github.com/duke-git/lancet/v2/system"
|
|||||||
```
|
```
|
||||||
|
|
||||||
#### 函数列表:
|
#### 函数列表:
|
||||||
|
|
||||||
- [IsWindows](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN.md#IsWindows)
|
- [IsWindows](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN.md#IsWindows)
|
||||||
- [IsLinux](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN.md#IsLinux)
|
- [IsLinux](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN.md#IsLinux)
|
||||||
- [IsMac](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN.md#IsMac)
|
- [IsMac](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN.md#IsMac)
|
||||||
@@ -489,6 +518,7 @@ import "github.com/duke-git/lancet/v2/system"
|
|||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/validator"
|
import "github.com/duke-git/lancet/v2/validator"
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 函数列表:
|
#### 函数列表:
|
||||||
|
|
||||||
- [ContainChinese](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#ContainChinese)
|
- [ContainChinese](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#ContainChinese)
|
||||||
@@ -524,7 +554,9 @@ import "github.com/duke-git/lancet/v2/validator"
|
|||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/xerror"
|
import "github.com/duke-git/lancet/v2/xerror"
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 函数列表:
|
#### 函数列表:
|
||||||
|
|
||||||
- [Unwrap](https://github.com/duke-git/lancet/blob/main/docs/xerror_zh-CN.md#Unwrap)
|
- [Unwrap](https://github.com/duke-git/lancet/blob/main/docs/xerror_zh-CN.md#Unwrap)
|
||||||
|
|
||||||
## 如何贡献代码
|
## 如何贡献代码
|
||||||
|
|||||||
15
SECURITY.md
Normal file
15
SECURITY.md
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
# Security Policy
|
||||||
|
|
||||||
|
## Supported Versions
|
||||||
|
Here is the lancet version and compatibility with go language version.
|
||||||
|
|
||||||
|
| Version | Supported |
|
||||||
|
| ------- | ------------------|
|
||||||
|
| 2.x.x | +go v1.18 |
|
||||||
|
| 1.x.x | +go v1.12 |
|
||||||
|
|
||||||
|
|
||||||
|
## Reporting a Vulnerability
|
||||||
|
|
||||||
|
For now, there is no public website to report a vulnerability, If you find security issue in lancet, you can send it to me via my email `lanliddd.2007@163.com`.
|
||||||
|
we can discuss it. I am appreciate if someone can create a public page for reporting vulnerability.
|
||||||
@@ -90,34 +90,50 @@ func ToChannel[T any](array []T) <-chan T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ToString convert value to string
|
// ToString convert value to string
|
||||||
|
// for number, string, []byte, will convert to string
|
||||||
|
// for other type (slice, map, array, struct) will call json.Marshal
|
||||||
func ToString(value any) string {
|
func ToString(value any) string {
|
||||||
result := ""
|
|
||||||
if value == nil {
|
if value == nil {
|
||||||
return result
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
v := reflect.ValueOf(value)
|
|
||||||
|
|
||||||
switch value.(type) {
|
switch value.(type) {
|
||||||
case float32, float64:
|
case float32:
|
||||||
result = strconv.FormatFloat(v.Float(), 'f', -1, 64)
|
return strconv.FormatFloat(float64(value.(float32)), 'f', -1, 32)
|
||||||
return result
|
case float64:
|
||||||
case int, int8, int16, int32, int64:
|
return strconv.FormatFloat(value.(float64), 'f', -1, 64)
|
||||||
result = strconv.FormatInt(v.Int(), 10)
|
case int:
|
||||||
return result
|
return strconv.FormatInt(int64(value.(int)), 10)
|
||||||
case uint, uint8, uint16, uint32, uint64:
|
case int8:
|
||||||
result = strconv.FormatUint(v.Uint(), 10)
|
return strconv.FormatInt(int64(value.(int8)), 10)
|
||||||
return result
|
case int16:
|
||||||
|
return strconv.FormatInt(int64(value.(int16)), 10)
|
||||||
|
case int32:
|
||||||
|
return strconv.FormatInt(int64(value.(int32)), 10)
|
||||||
|
case int64:
|
||||||
|
return strconv.FormatInt(value.(int64), 10)
|
||||||
|
case uint:
|
||||||
|
return strconv.FormatUint(uint64(value.(uint)), 10)
|
||||||
|
case uint8:
|
||||||
|
return strconv.FormatUint(uint64(value.(uint8)), 10)
|
||||||
|
case uint16:
|
||||||
|
return strconv.FormatUint(uint64(value.(uint16)), 10)
|
||||||
|
case uint32:
|
||||||
|
return strconv.FormatUint(uint64(value.(uint32)), 10)
|
||||||
|
case uint64:
|
||||||
|
return strconv.FormatUint(value.(uint64), 10)
|
||||||
case string:
|
case string:
|
||||||
result = v.String()
|
return value.(string)
|
||||||
return result
|
|
||||||
case []byte:
|
case []byte:
|
||||||
result = string(v.Bytes())
|
return string(value.([]byte))
|
||||||
return result
|
|
||||||
default:
|
default:
|
||||||
newValue, _ := json.Marshal(value)
|
newValue, _ := json.Marshal(value)
|
||||||
result = string(newValue)
|
return string(newValue)
|
||||||
return result
|
|
||||||
|
// todo: maybe we should't supprt other type convertion
|
||||||
|
// v := reflect.ValueOf(value)
|
||||||
|
// log.Panicf("Unsupported data type: %s ", v.String())
|
||||||
|
// return ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -137,9 +137,10 @@ func TestToString(t *testing.T) {
|
|||||||
"", "",
|
"", "",
|
||||||
"0", "1", "-1",
|
"0", "1", "-1",
|
||||||
"123", "123", "123", "123", "123", "123", "123",
|
"123", "123", "123", "123", "123", "123", "123",
|
||||||
"12.3", "12.300000190734863",
|
"12.3", "12.3",
|
||||||
"true", "false",
|
"true", "false",
|
||||||
"[1,2,3]", "{\"a\":1,\"b\":2,\"c\":3}", "{\"Name\":\"TestStruct\"}", "hello"}
|
"[1,2,3]", "{\"a\":1,\"b\":2,\"c\":3}", "{\"Name\":\"TestStruct\"}", "hello",
|
||||||
|
}
|
||||||
|
|
||||||
for i := 0; i < len(cases); i++ {
|
for i := 0; i < len(cases); i++ {
|
||||||
actual := ToString(cases[i])
|
actual := ToString(cases[i])
|
||||||
|
|||||||
@@ -4,22 +4,59 @@ package datastructure
|
|||||||
type Set[T comparable] map[T]struct{}
|
type Set[T comparable] map[T]struct{}
|
||||||
|
|
||||||
// NewSet return a instance of set
|
// NewSet return a instance of set
|
||||||
func NewSet[T comparable](values ...T) Set[T] {
|
func NewSet[T comparable](items ...T) Set[T] {
|
||||||
set := make(Set[T])
|
set := make(Set[T])
|
||||||
set.Add(values...)
|
set.Add(items...)
|
||||||
return set
|
return set
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add value to set
|
// NewSetFromSlice create a set from slice
|
||||||
func (s Set[T]) Add(values ...T) {
|
func NewSetFromSlice[T comparable](items []T) Set[T] {
|
||||||
for _, v := range values {
|
set := make(Set[T])
|
||||||
|
for _, item := range items {
|
||||||
|
set.Add(item)
|
||||||
|
}
|
||||||
|
return set
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add items to set
|
||||||
|
func (s Set[T]) Add(items ...T) {
|
||||||
|
for _, v := range items {
|
||||||
s[v] = struct{}{}
|
s[v] = struct{}{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Contain checks if set contains value or not
|
// AddIfNotExist checks if item exists in the set,
|
||||||
func (s Set[T]) Contain(value T) bool {
|
// it adds the item to set and returns true if it does not exist in the set,
|
||||||
_, ok := s[value]
|
// or else it does nothing and returns false.
|
||||||
|
func (s Set[T]) AddIfNotExist(item T) bool {
|
||||||
|
if !s.Contain(item) {
|
||||||
|
if _, ok := s[item]; !ok {
|
||||||
|
s[item] = struct{}{}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddIfNotExistBy checks if item exists in the set and pass the `checker` function
|
||||||
|
// it adds the item to set and returns true if it does not exists in the set and
|
||||||
|
// function `checker` returns true, or else it does nothing and returns false.
|
||||||
|
func (s Set[T]) AddIfNotExistBy(item T, checker func(element T) bool) bool {
|
||||||
|
if !s.Contain(item) {
|
||||||
|
if checker(item) {
|
||||||
|
if _, ok := s[item]; !ok {
|
||||||
|
s[item] = struct{}{}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Contain checks if set contains item or not
|
||||||
|
func (s Set[T]) Contain(item T) bool {
|
||||||
|
_, ok := s[item]
|
||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,9 +78,9 @@ func (s Set[T]) Clone() Set[T] {
|
|||||||
return set
|
return set
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete value of set
|
// Delete item of set
|
||||||
func (s Set[T]) Delete(values ...T) {
|
func (s Set[T]) Delete(items ...T) {
|
||||||
for _, v := range values {
|
for _, v := range items {
|
||||||
delete(s, v)
|
delete(s, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -58,7 +95,7 @@ func (s Set[T]) Equal(other Set[T]) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Iterate call function by every element of set
|
// Iterate call function by every element of set
|
||||||
func (s Set[T]) Iterate(fn func(value T)) {
|
func (s Set[T]) Iterate(fn func(item T)) {
|
||||||
for v := range s {
|
for v := range s {
|
||||||
fn(v)
|
fn(v)
|
||||||
}
|
}
|
||||||
@@ -76,13 +113,13 @@ func (s Set[T]) Size() int {
|
|||||||
|
|
||||||
// Values return all values of set
|
// Values return all values of set
|
||||||
func (s Set[T]) Values() []T {
|
func (s Set[T]) Values() []T {
|
||||||
values := make([]T, 0, 0)
|
result := make([]T, 0, len(s))
|
||||||
|
|
||||||
s.Iterate(func(value T) {
|
s.Iterate(func(value T) {
|
||||||
values = append(values, value)
|
result = append(result, value)
|
||||||
})
|
})
|
||||||
|
|
||||||
return values
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Union creates a new set contain all element of set s and other
|
// Union creates a new set contain all element of set s and other
|
||||||
|
|||||||
@@ -6,6 +6,19 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/internal"
|
"github.com/duke-git/lancet/v2/internal"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestSet_NewSetFromSlice(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestSet_NewSetFromSlice")
|
||||||
|
|
||||||
|
s1 := NewSetFromSlice([]int{1, 2, 2, 3})
|
||||||
|
assert.Equal(3, s1.Size())
|
||||||
|
assert.Equal(true, s1.Contain(1))
|
||||||
|
assert.Equal(true, s1.Contain(2))
|
||||||
|
assert.Equal(true, s1.Contain(3))
|
||||||
|
|
||||||
|
s2 := NewSetFromSlice([]int{})
|
||||||
|
assert.Equal(0, s2.Size())
|
||||||
|
}
|
||||||
|
|
||||||
func TestSet_Add(t *testing.T) {
|
func TestSet_Add(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestSet_Add")
|
assert := internal.NewAssert(t, "TestSet_Add")
|
||||||
|
|
||||||
@@ -17,6 +30,38 @@ func TestSet_Add(t *testing.T) {
|
|||||||
assert.Equal(true, set.Equal(expected))
|
assert.Equal(true, set.Equal(expected))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSet_AddIfNotExist(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestSet_AddIfNotExist")
|
||||||
|
|
||||||
|
set := NewSet[int]()
|
||||||
|
set.Add(1, 2, 3)
|
||||||
|
|
||||||
|
assert.Equal(false, set.AddIfNotExist(1))
|
||||||
|
assert.Equal(true, set.AddIfNotExist(4))
|
||||||
|
assert.Equal(NewSet(1, 2, 3, 4), set)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSet_AddIfNotExistBy(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestSet_AddIfNotExistBy")
|
||||||
|
|
||||||
|
set := NewSet[int]()
|
||||||
|
set.Add(1, 2)
|
||||||
|
|
||||||
|
ok := set.AddIfNotExistBy(3, func(val int) bool {
|
||||||
|
return val%2 != 0
|
||||||
|
})
|
||||||
|
|
||||||
|
notOk := set.AddIfNotExistBy(4, func(val int) bool {
|
||||||
|
return val%2 != 0
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.Equal(true, ok)
|
||||||
|
assert.Equal(false, notOk)
|
||||||
|
|
||||||
|
assert.Equal(true, set.Contain(3))
|
||||||
|
assert.Equal(false, set.Contain(4))
|
||||||
|
}
|
||||||
|
|
||||||
func TestSet_Contain(t *testing.T) {
|
func TestSet_Contain(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestSet_Contain")
|
assert := internal.NewAssert(t, "TestSet_Contain")
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
|
|
||||||
- [BubbleSort](#BubbleSort)
|
- [BubbleSort](#BubbleSort)
|
||||||
- [InsertionSort](#InsertionSort)
|
- [InsertionSort](#InsertionSort)
|
||||||
- [SelectionSort](#SelectionSort)
|
- [SelectionSort](#SelectionSort)
|
||||||
@@ -31,7 +32,6 @@ import (
|
|||||||
- [CountSort](#CountSort)
|
- [CountSort](#CountSort)
|
||||||
- [BinarySearch](#BinarySearch)
|
- [BinarySearch](#BinarySearch)
|
||||||
- [BinaryIterativeSearch](#BinaryIterativeSearch)
|
- [BinaryIterativeSearch](#BinaryIterativeSearch)
|
||||||
|
|
||||||
- [LinearSearch](#LinearSearch)
|
- [LinearSearch](#LinearSearch)
|
||||||
- [LRUCache](#LRUCache)
|
- [LRUCache](#LRUCache)
|
||||||
|
|
||||||
|
|||||||
@@ -29,7 +29,6 @@ import (
|
|||||||
- [QuickSort](#QuickSort)
|
- [QuickSort](#QuickSort)
|
||||||
- [HeapSort](#HeapSort)
|
- [HeapSort](#HeapSort)
|
||||||
- [MergeSort](#MergeSort)
|
- [MergeSort](#MergeSort)
|
||||||
|
|
||||||
- [CountSort](#CountSort)
|
- [CountSort](#CountSort)
|
||||||
- [BinarySearch](#BinarySearch)
|
- [BinarySearch](#BinarySearch)
|
||||||
- [BinaryIterativeSearch](#BinaryIterativeSearch)
|
- [BinaryIterativeSearch](#BinaryIterativeSearch)
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ import (
|
|||||||
- [ToBytes](#ToBytes)
|
- [ToBytes](#ToBytes)
|
||||||
- [ToChar](#ToChar)
|
- [ToChar](#ToChar)
|
||||||
- [ToChannel](#ToChannel)
|
- [ToChannel](#ToChannel)
|
||||||
|
|
||||||
- [ToFloat](#ToFloat)
|
- [ToFloat](#ToFloat)
|
||||||
- [ToInt](#ToInt)
|
- [ToInt](#ToInt)
|
||||||
- [ToJson](#ToJson)
|
- [ToJson](#ToJson)
|
||||||
@@ -395,6 +394,32 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ToString">ToString</span>
|
||||||
|
|
||||||
|
<p>ToString convert value to string, for number, string, []byte, will convert to string. For other type (slice, map, array, struct) will call json.Marshal</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ToString(value any) string
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/convertor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Printf("%q", convertor.ToString(1)) //"1"
|
||||||
|
fmt.Printf("%q", convertor.ToString(1.1)) //"1.1"
|
||||||
|
fmt.Printf("%q", convertor.ToString([]int{1, 2, 3})) //"[1,2,3]"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="StructToMap">StructToMap</span>
|
### <span id="StructToMap">StructToMap</span>
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ import (
|
|||||||
- [ToBytes](#ToBytes)
|
- [ToBytes](#ToBytes)
|
||||||
- [ToChar](#ToChar)
|
- [ToChar](#ToChar)
|
||||||
- [ToChannel](#ToChannel)
|
- [ToChannel](#ToChannel)
|
||||||
|
|
||||||
- [ToFloat](#ToFloat)
|
- [ToFloat](#ToFloat)
|
||||||
- [ToInt](#ToInt)
|
- [ToInt](#ToInt)
|
||||||
- [ToJson](#ToJson)
|
- [ToJson](#ToJson)
|
||||||
@@ -401,7 +400,7 @@ func main() {
|
|||||||
|
|
||||||
### <span id="ToString">ToString</span>
|
### <span id="ToString">ToString</span>
|
||||||
|
|
||||||
<p>将interface转成字符串</p>
|
<p>将值转换为字符串,对于数字、字符串、[]byte,将转换为字符串。 对于其他类型(切片、映射、数组、结构)将调用 json.Marshal</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
|||||||
@@ -47,7 +47,6 @@ import (
|
|||||||
- [HmacSha1](#HmacSha1)
|
- [HmacSha1](#HmacSha1)
|
||||||
- [HmacSha256](#HmacSha256)
|
- [HmacSha256](#HmacSha256)
|
||||||
- [HmacSha512](#HmacSha512)
|
- [HmacSha512](#HmacSha512)
|
||||||
|
|
||||||
- [Md5String](#Md5String)
|
- [Md5String](#Md5String)
|
||||||
- [Md5File](#Md5File)
|
- [Md5File](#Md5File)
|
||||||
- [Sha1](#Sha1)
|
- [Sha1](#Sha1)
|
||||||
|
|||||||
@@ -33,7 +33,6 @@ import (
|
|||||||
- [AesOfbDecrypt](#AesOfbDecrypt)
|
- [AesOfbDecrypt](#AesOfbDecrypt)
|
||||||
- [Base64StdEncode](#Base64StdEncode)
|
- [Base64StdEncode](#Base64StdEncode)
|
||||||
- [Base64StdDecode](#Base64StdDecode)
|
- [Base64StdDecode](#Base64StdDecode)
|
||||||
|
|
||||||
- [DesEcbEncrypt](#DesEcbEncrypt)
|
- [DesEcbEncrypt](#DesEcbEncrypt)
|
||||||
- [DesEcbDecrypt](#DesEcbDecrypt)
|
- [DesEcbDecrypt](#DesEcbDecrypt)
|
||||||
- [DesCbcEncrypt](#DesCbcEncrypt)
|
- [DesCbcEncrypt](#DesCbcEncrypt)
|
||||||
@@ -43,7 +42,6 @@ import (
|
|||||||
- [DesCfbDecrypt](#DesCfbDecrypt)
|
- [DesCfbDecrypt](#DesCfbDecrypt)
|
||||||
- [DesOfbEncrypt](#DesOfbEncrypt)
|
- [DesOfbEncrypt](#DesOfbEncrypt)
|
||||||
- [DesOfbDecrypt](#DesOfbDecrypt)
|
- [DesOfbDecrypt](#DesOfbDecrypt)
|
||||||
|
|
||||||
- [HmacMd5](#HmacMd5)
|
- [HmacMd5](#HmacMd5)
|
||||||
- [HmacSha1](#HmacSha1)
|
- [HmacSha1](#HmacSha1)
|
||||||
- [HmacSha256](#HmacSha256)
|
- [HmacSha256](#HmacSha256)
|
||||||
|
|||||||
@@ -22,8 +22,11 @@ import (
|
|||||||
## Index
|
## Index
|
||||||
|
|
||||||
- [NewSet](#NewSet)
|
- [NewSet](#NewSet)
|
||||||
|
- [NewSetFromSlice](#NewSetFromSlice)
|
||||||
- [Values](#Values)
|
- [Values](#Values)
|
||||||
- [Add](#Add)
|
- [Add](#Add)
|
||||||
|
- [AddIfNotExist](#AddIfNotExist)
|
||||||
|
- [AddIfNotExistBy](#AddIfNotExistBy)
|
||||||
- [Delete](#Delete)
|
- [Delete](#Delete)
|
||||||
- [Contain](#Contain)
|
- [Contain](#Contain)
|
||||||
- [ContainAll](#ContainAll)
|
- [ContainAll](#ContainAll)
|
||||||
@@ -34,7 +37,6 @@ import (
|
|||||||
- [IsEmpty](#IsEmpty)
|
- [IsEmpty](#IsEmpty)
|
||||||
- [Union](#Union)
|
- [Union](#Union)
|
||||||
- [Intersection](#Intersection)
|
- [Intersection](#Intersection)
|
||||||
|
|
||||||
- [SymmetricDifference](#SymmetricDifference)
|
- [SymmetricDifference](#SymmetricDifference)
|
||||||
- [Minus](#Minus)
|
- [Minus](#Minus)
|
||||||
|
|
||||||
@@ -45,13 +47,13 @@ import (
|
|||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
### <span id="NewSet">NewSet</span>
|
### <span id="NewSet">NewSet</span>
|
||||||
<p>Make a Set instance</p>
|
<p>Create a set instance</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
type Set[T comparable] map[T]bool
|
type Set[T comparable] map[T]bool
|
||||||
func NewSet[T comparable](values ...T) Set[T]
|
func NewSet[T comparable](items ...T) Set[T]
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -70,6 +72,30 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="NewSetFromSlice">NewSetFromSlice</span>
|
||||||
|
<p>Create a set from slice</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func NewSetFromSlice[T comparable](items []T) Set[T]
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
set "github.com/duke-git/lancet/v2/datastructure/set"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
st := set.NewSetFromSlice([]int{1, 2, 2, 3})
|
||||||
|
fmt.Println(st.Values()) //1,2,3
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Values">Values</span>
|
### <span id="Values">Values</span>
|
||||||
@@ -100,12 +126,12 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
### <span id="Add">Add</span>
|
### <span id="Add">Add</span>
|
||||||
<p>Add value to set</p>
|
<p>Add items to set</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func (s Set[T]) Add(values ...T)
|
func (s Set[T]) Add(items ...T)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -126,14 +152,83 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="AddIfNotExist">AddIfNotExist</span>
|
||||||
### <span id="Delete">Delete</span>
|
<p>AddIfNotExist checks if item exists in the set, it adds the item to set and returns true if it does not exist in the set, or else it does nothing and returns false.</p>
|
||||||
<p>Delete value in set</p>
|
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func (s Set[T]) Delete(values ...T)
|
func (s Set[T]) AddIfNotExist(item T) bool
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
set "github.com/duke-git/lancet/v2/datastructure/set"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
st := set.NewSet[int]()
|
||||||
|
st.Add(1, 2, 3)
|
||||||
|
|
||||||
|
r1 := st.AddIfNotExist(1)
|
||||||
|
r2 := st.AddIfNotExist(4)
|
||||||
|
|
||||||
|
fmt.Println(r1) // false
|
||||||
|
fmt.Println(r2) // true
|
||||||
|
fmt.Println(st.Values()) // 1,2,3,4
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="AddIfNotExistBy">AddIfNotExistBy</span>
|
||||||
|
<p>AddIfNotExistBy checks if item exists in the set and pass the `checker` function it adds the item to set and returns true if it does not exists in the set and function `checker` returns true, or else it does nothing and returns false.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (s Set[T]) AddIfNotExistBy(item T, checker func(element T) bool) bool
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
set "github.com/duke-git/lancet/v2/datastructure/set"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
st := set.NewSet[int]()
|
||||||
|
st.Add(1, 2)
|
||||||
|
|
||||||
|
ok := st.AddIfNotExistBy(3, func(val int) bool {
|
||||||
|
return val%2 != 0
|
||||||
|
})
|
||||||
|
fmt.Println(ok) // true
|
||||||
|
|
||||||
|
|
||||||
|
notOk := st.AddIfNotExistBy(4, func(val int) bool {
|
||||||
|
return val%2 != 0
|
||||||
|
})
|
||||||
|
fmt.Println(notOk) // false
|
||||||
|
|
||||||
|
fmt.Println(st.Values()) // 1, 2, 3
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Delete">Delete</span>
|
||||||
|
<p>Delete item in set</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (s Set[T]) Delete(items ...T)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -157,12 +252,12 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
### <span id="Contain">Contain</span>
|
### <span id="Contain">Contain</span>
|
||||||
<p>Check if value is in set or not</p>
|
<p>Check if item is in set or not</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func (s Set[T]) Contain(value T) bool
|
func (s Set[T]) Contain(item T) bool
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -309,7 +404,7 @@ func main() {
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func (s Set[T]) Iterate(fn func(value T))
|
func (s Set[T]) Iterate(fn func(item T))
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -324,8 +419,8 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
set1 := set.NewSet(1, 2, 3)
|
set1 := set.NewSet(1, 2, 3)
|
||||||
arr := []int{}
|
arr := []int{}
|
||||||
set.Iterate(func(value int) {
|
set.Iterate(func(item int) {
|
||||||
arr = append(arr, value)
|
arr = append(arr, item)
|
||||||
})
|
})
|
||||||
|
|
||||||
fmt.Println(arr) //1,2,3
|
fmt.Println(arr) //1,2,3
|
||||||
|
|||||||
@@ -22,8 +22,11 @@ import (
|
|||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
- [NewSet](#NewSet)
|
- [NewSet](#NewSet)
|
||||||
|
- [NewSetFromSlice](#NewSetFromSlice)
|
||||||
- [Values](#Values)
|
- [Values](#Values)
|
||||||
- [Add](#Add)
|
- [Add](#Add)
|
||||||
|
- [AddIfNotExist](#AddIfNotExist)
|
||||||
|
- [AddIfNotExistBy](#AddIfNotExistBy)
|
||||||
- [Delete](#Delete)
|
- [Delete](#Delete)
|
||||||
- [Contain](#Contain)
|
- [Contain](#Contain)
|
||||||
- [ContainAll](#ContainAll)
|
- [ContainAll](#ContainAll)
|
||||||
@@ -34,7 +37,6 @@ import (
|
|||||||
- [IsEmpty](#IsEmpty)
|
- [IsEmpty](#IsEmpty)
|
||||||
- [Union](#Union)
|
- [Union](#Union)
|
||||||
- [Intersection](#Intersection)
|
- [Intersection](#Intersection)
|
||||||
|
|
||||||
- [SymmetricDifference](#SymmetricDifference)
|
- [SymmetricDifference](#SymmetricDifference)
|
||||||
- [Minus](#Minus)
|
- [Minus](#Minus)
|
||||||
|
|
||||||
@@ -51,7 +53,7 @@ import (
|
|||||||
|
|
||||||
```go
|
```go
|
||||||
type Set[T comparable] map[T]bool
|
type Set[T comparable] map[T]bool
|
||||||
func NewSet[T comparable](values ...T) Set[T]
|
func NewSet[T comparable](items ...T) Set[T]
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
@@ -71,6 +73,31 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="NewSetFromSlice">NewSetFromSlice</span>
|
||||||
|
<p>基于切片创建集合</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func NewSetFromSlice[T comparable](items []T) Set[T]
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
set "github.com/duke-git/lancet/v2/datastructure/set"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
st := set.NewSetFromSlice([]int{1, 2, 2, 3})
|
||||||
|
fmt.Println(st.Values()) //1,2,3
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Values">Values</span>
|
### <span id="Values">Values</span>
|
||||||
<p>获取集合中所有元素的切片</p>
|
<p>获取集合中所有元素的切片</p>
|
||||||
@@ -105,7 +132,7 @@ func main() {
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func (s Set[T]) Add(values ...T)
|
func (s Set[T]) Add(items ...T)
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
@@ -126,6 +153,76 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="AddIfNotExist">AddIfNotExist</span>
|
||||||
|
<p>如果集合中不存在元素,则添加该元素返回true, 如果集合中存在元素, 不做任何操作,返回false</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (s Set[T]) AddIfNotExist(item T) bool
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
set "github.com/duke-git/lancet/v2/datastructure/set"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
st := set.NewSet[int]()
|
||||||
|
st.Add(1, 2, 3)
|
||||||
|
|
||||||
|
r1 := st.AddIfNotExist(1)
|
||||||
|
r2 := st.AddIfNotExist(4)
|
||||||
|
|
||||||
|
fmt.Println(r1) // false
|
||||||
|
fmt.Println(r2) // true
|
||||||
|
fmt.Println(st.Values()) // 1,2,3,4
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="AddIfNotExistBy">AddIfNotExistBy</span>
|
||||||
|
<p>根据checker函数判断元素是否在集合中,如果集合中不存在元素且checker返回true,则添加该元素返回true, 否则不做任何操作,返回false</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (s Set[T]) AddIfNotExistBy(item T, checker func(element T) bool) bool
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
set "github.com/duke-git/lancet/v2/datastructure/set"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
st := set.NewSet[int]()
|
||||||
|
st.Add(1, 2)
|
||||||
|
|
||||||
|
ok := st.AddIfNotExistBy(3, func(val int) bool {
|
||||||
|
return val%2 != 0
|
||||||
|
})
|
||||||
|
fmt.Println(ok) // true
|
||||||
|
|
||||||
|
|
||||||
|
notOk := st.AddIfNotExistBy(4, func(val int) bool {
|
||||||
|
return val%2 != 0
|
||||||
|
})
|
||||||
|
fmt.Println(notOk) // false
|
||||||
|
|
||||||
|
fmt.Println(st.Values()) // 1, 2, 3
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Delete">Delete</span>
|
### <span id="Delete">Delete</span>
|
||||||
<p>删除集合中元素</p>
|
<p>删除集合中元素</p>
|
||||||
@@ -133,7 +230,7 @@ func main() {
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func (s Set[T]) Delete(values ...T)
|
func (s Set[T]) Delete(items ...T)
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
@@ -162,7 +259,7 @@ func main() {
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func (s Set[T]) Contain(value T) bool
|
func (s Set[T]) Contain(item T) bool
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
@@ -309,7 +406,7 @@ func main() {
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func (s Set[T]) Iterate(fn func(value T))
|
func (s Set[T]) Iterate(fn func(item T))
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
@@ -324,8 +421,8 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
set1 := set.NewSet(1, 2, 3)
|
set1 := set.NewSet(1, 2, 3)
|
||||||
arr := []int{}
|
arr := []int{}
|
||||||
set.Iterate(func(value int) {
|
set.Iterate(func(item int) {
|
||||||
arr = append(arr, value)
|
arr = append(arr, item)
|
||||||
})
|
})
|
||||||
|
|
||||||
fmt.Println(arr) //1,2,3
|
fmt.Println(arr) //1,2,3
|
||||||
@@ -420,9 +517,6 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="SymmetricDifference">SymmetricDifference</span>
|
### <span id="SymmetricDifference">SymmetricDifference</span>
|
||||||
<p>返回一个集合,其中元素在第一个集合或第二个集合中,且不同时存在于两个集合中</p>
|
<p>返回一个集合,其中元素在第一个集合或第二个集合中,且不同时存在于两个集合中</p>
|
||||||
|
|
||||||
|
|||||||
@@ -29,7 +29,6 @@ import (
|
|||||||
- [PreOrderTraverse](#BSTree_PreOrderTraverse)
|
- [PreOrderTraverse](#BSTree_PreOrderTraverse)
|
||||||
- [InOrderTraverse](#BSTree_InOrderTraverse)
|
- [InOrderTraverse](#BSTree_InOrderTraverse)
|
||||||
- [PostOrderTraverse](#BSTree_PostOrderTraverse)
|
- [PostOrderTraverse](#BSTree_PostOrderTraverse)
|
||||||
|
|
||||||
- [LevelOrderTraverse](#BSTree_LevelOrderTraverse)
|
- [LevelOrderTraverse](#BSTree_LevelOrderTraverse)
|
||||||
- [Depth](#BSTree_Depth)
|
- [Depth](#BSTree_Depth)
|
||||||
- [HasSubTree](#BSTree_HasSubTree)
|
- [HasSubTree](#BSTree_HasSubTree)
|
||||||
|
|||||||
@@ -29,7 +29,6 @@ import (
|
|||||||
- [PreOrderTraverse](#BSTree_PreOrderTraverse)
|
- [PreOrderTraverse](#BSTree_PreOrderTraverse)
|
||||||
- [InOrderTraverse](#BSTree_InOrderTraverse)
|
- [InOrderTraverse](#BSTree_InOrderTraverse)
|
||||||
- [PostOrderTraverse](#BSTree_PostOrderTraverse)
|
- [PostOrderTraverse](#BSTree_PostOrderTraverse)
|
||||||
|
|
||||||
- [LevelOrderTraverse](#BSTree_LevelOrderTraverse)
|
- [LevelOrderTraverse](#BSTree_LevelOrderTraverse)
|
||||||
- [Depth](#BSTree_Depth)
|
- [Depth](#BSTree_Depth)
|
||||||
- [HasSubTree](#BSTree_HasSubTree)
|
- [HasSubTree](#BSTree_HasSubTree)
|
||||||
|
|||||||
@@ -42,7 +42,6 @@ import (
|
|||||||
- [GetNightTimestamp](#GetNightTimestamp)
|
- [GetNightTimestamp](#GetNightTimestamp)
|
||||||
- [FormatTimeToStr](#FormatTimeToStr)
|
- [FormatTimeToStr](#FormatTimeToStr)
|
||||||
- [FormatStrToTime](#FormatStrToTime)
|
- [FormatStrToTime](#FormatStrToTime)
|
||||||
|
|
||||||
- [NewUnixNow](#NewUnixNow)
|
- [NewUnixNow](#NewUnixNow)
|
||||||
- [NewUnix](#NewUnix)
|
- [NewUnix](#NewUnix)
|
||||||
- [NewFormat](#NewFormat)
|
- [NewFormat](#NewFormat)
|
||||||
|
|||||||
@@ -41,7 +41,6 @@ import (
|
|||||||
- [GetNightTimestamp](#GetNightTimestamp)
|
- [GetNightTimestamp](#GetNightTimestamp)
|
||||||
- [FormatTimeToStr](#FormatTimeToStr)
|
- [FormatTimeToStr](#FormatTimeToStr)
|
||||||
- [FormatStrToTime](#FormatStrToTime)
|
- [FormatStrToTime](#FormatStrToTime)
|
||||||
|
|
||||||
- [NewUnixNow](#NewUnixNow)
|
- [NewUnixNow](#NewUnixNow)
|
||||||
- [NewUnix](#NewUnix)
|
- [NewUnix](#NewUnix)
|
||||||
- [NewFormat](#NewFormat)
|
- [NewFormat](#NewFormat)
|
||||||
|
|||||||
@@ -28,7 +28,6 @@ import (
|
|||||||
- [IsExist](#IsExist)
|
- [IsExist](#IsExist)
|
||||||
- [IsLink](#IsLink)
|
- [IsLink](#IsLink)
|
||||||
- [IsDir](#IsDir)
|
- [IsDir](#IsDir)
|
||||||
|
|
||||||
- [ListFileNames](#ListFileNames)
|
- [ListFileNames](#ListFileNames)
|
||||||
- [RemoveFile](#RemoveFile)
|
- [RemoveFile](#RemoveFile)
|
||||||
- [ReadFileToString](#ReadFileToString)
|
- [ReadFileToString](#ReadFileToString)
|
||||||
|
|||||||
@@ -28,7 +28,6 @@ import (
|
|||||||
- [IsExist](#IsExist)
|
- [IsExist](#IsExist)
|
||||||
- [IsLink](#IsLink)
|
- [IsLink](#IsLink)
|
||||||
- [IsDir](#IsDir)
|
- [IsDir](#IsDir)
|
||||||
|
|
||||||
- [ListFileNames](#ListFileNames)
|
- [ListFileNames](#ListFileNames)
|
||||||
- [RemoveFile](#RemoveFile)
|
- [RemoveFile](#RemoveFile)
|
||||||
- [ReadFileToString](#ReadFileToString)
|
- [ReadFileToString](#ReadFileToString)
|
||||||
|
|||||||
@@ -28,7 +28,6 @@ import (
|
|||||||
- [MaxBy](#MaxBy)
|
- [MaxBy](#MaxBy)
|
||||||
- [Min](#Min)
|
- [Min](#Min)
|
||||||
- [MinBy](#MaxBy)
|
- [MinBy](#MaxBy)
|
||||||
|
|
||||||
- [Percent](#Percent)
|
- [Percent](#Percent)
|
||||||
- [RoundToFloat](#RoundToFloat)
|
- [RoundToFloat](#RoundToFloat)
|
||||||
- [RoundToString](#RoundToString)
|
- [RoundToString](#RoundToString)
|
||||||
|
|||||||
@@ -28,7 +28,6 @@ import (
|
|||||||
- [MaxBy](#MaxBy)
|
- [MaxBy](#MaxBy)
|
||||||
- [Min](#Min)
|
- [Min](#Min)
|
||||||
- [MinBy](#MaxBy)
|
- [MinBy](#MaxBy)
|
||||||
|
|
||||||
- [Percent](#Percent)
|
- [Percent](#Percent)
|
||||||
- [RoundToFloat](#RoundToFloat)
|
- [RoundToFloat](#RoundToFloat)
|
||||||
- [RoundToString](#RoundToString)
|
- [RoundToString](#RoundToString)
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ import (
|
|||||||
## Index
|
## Index
|
||||||
- [ConvertMapToQueryString](#ConvertMapToQueryString)
|
- [ConvertMapToQueryString](#ConvertMapToQueryString)
|
||||||
- [EncodeUrl](#EncodeUrl)
|
- [EncodeUrl](#EncodeUrl)
|
||||||
|
|
||||||
- [GetInternalIp](#GetInternalIp)
|
- [GetInternalIp](#GetInternalIp)
|
||||||
- [GetIps](#GetIps)
|
- [GetIps](#GetIps)
|
||||||
- [GetMacAddrs](#GetMacAddrs)
|
- [GetMacAddrs](#GetMacAddrs)
|
||||||
@@ -38,7 +37,6 @@ import (
|
|||||||
- [SendRequest](#SendRequest)
|
- [SendRequest](#SendRequest)
|
||||||
- [DecodeResponse](#DecodeResponse)
|
- [DecodeResponse](#DecodeResponse)
|
||||||
- [StructToUrlValues](#StructToUrlValues)
|
- [StructToUrlValues](#StructToUrlValues)
|
||||||
|
|
||||||
- [HttpGet<sup>Deprecated</sup>](#HttpGet)
|
- [HttpGet<sup>Deprecated</sup>](#HttpGet)
|
||||||
- [HttpDelete<sup>Deprecated</sup>](#HttpDelete)
|
- [HttpDelete<sup>Deprecated</sup>](#HttpDelete)
|
||||||
- [HttpPost<sup>Deprecated</sup>](#HttpPost)
|
- [HttpPost<sup>Deprecated</sup>](#HttpPost)
|
||||||
|
|||||||
@@ -30,7 +30,6 @@ import (
|
|||||||
- [GetMacAddrs](#GetMacAddrs)
|
- [GetMacAddrs](#GetMacAddrs)
|
||||||
- [GetPublicIpInfo](#GetPublicIpInfo)
|
- [GetPublicIpInfo](#GetPublicIpInfo)
|
||||||
- [GetRequestPublicIp](#GetRequestPublicIp)
|
- [GetRequestPublicIp](#GetRequestPublicIp)
|
||||||
|
|
||||||
- [IsPublicIP](#IsPublicIP)
|
- [IsPublicIP](#IsPublicIP)
|
||||||
- [IsInternalIP](#IsInternalIP)
|
- [IsInternalIP](#IsInternalIP)
|
||||||
- [HttpRequest](#HttpRequest)
|
- [HttpRequest](#HttpRequest)
|
||||||
@@ -38,13 +37,11 @@ import (
|
|||||||
- [SendRequest](#SendRequest)
|
- [SendRequest](#SendRequest)
|
||||||
- [DecodeResponse](#DecodeResponse)
|
- [DecodeResponse](#DecodeResponse)
|
||||||
- [StructToUrlValues](#StructToUrlValues)
|
- [StructToUrlValues](#StructToUrlValues)
|
||||||
|
|
||||||
- [HttpGet<sup>Deprecated</sup>](#HttpGet)
|
- [HttpGet<sup>Deprecated</sup>](#HttpGet)
|
||||||
- [HttpDelete<sup>Deprecated</sup>](#HttpDelete)
|
- [HttpDelete<sup>Deprecated</sup>](#HttpDelete)
|
||||||
- [HttpPost<sup>Deprecated</sup>](#HttpPost)
|
- [HttpPost<sup>Deprecated</sup>](#HttpPost)
|
||||||
- [HttpPut<sup>Deprecated</sup>](#HttpPut)
|
- [HttpPut<sup>Deprecated</sup>](#HttpPut)
|
||||||
- [HttpPatch<sup>Deprecated</sup>](#HttpPatch)
|
- [HttpPatch<sup>Deprecated</sup>](#HttpPatch)
|
||||||
|
|
||||||
- [ParseHttpResponse](#ParseHttpResponse)
|
- [ParseHttpResponse](#ParseHttpResponse)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|||||||
127
docs/random.md
127
docs/random.md
@@ -1,4 +1,5 @@
|
|||||||
# Random
|
# Random
|
||||||
|
|
||||||
Package random implements some basic functions to generate random int and string.
|
Package random implements some basic functions to generate random int and string.
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -7,10 +8,10 @@ Package random implements some basic functions to generate random int and string
|
|||||||
|
|
||||||
- [https://github.com/duke-git/lancet/blob/main/random/random.go](https://github.com/duke-git/lancet/blob/main/random/random.go)
|
- [https://github.com/duke-git/lancet/blob/main/random/random.go](https://github.com/duke-git/lancet/blob/main/random/random.go)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Usage:
|
## Usage:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/v2/random"
|
"github.com/duke-git/lancet/v2/random"
|
||||||
@@ -20,17 +21,22 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
|
|
||||||
- [RandBytes](#RandBytes)
|
- [RandBytes](#RandBytes)
|
||||||
- [RandInt](#RandInt)
|
- [RandInt](#RandInt)
|
||||||
- [RandString](#RandString)
|
- [RandString](#RandString)
|
||||||
|
- [RandUpper](#RandUpper)
|
||||||
|
- [RandLower](#RandLower)
|
||||||
|
- [RandNumeral](#RandNumeral)
|
||||||
|
- [RandNumeralOrLetter](#RandNumeralOrLetter)
|
||||||
- [UUIdV4](#UUIdV4)
|
- [UUIdV4](#UUIdV4)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
### <span id="RandBytes">RandBytes</span>
|
### <span id="RandBytes">RandBytes</span>
|
||||||
|
|
||||||
<p>Generate random byte slice.</p>
|
<p>Generate random byte slice.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -38,6 +44,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func RandBytes(length int) []byte
|
func RandBytes(length int) []byte
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -54,8 +61,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="RandInt">RandInt</span>
|
### <span id="RandInt">RandInt</span>
|
||||||
|
|
||||||
<p>Generate random int between min and max, may contain min, not max.</p>
|
<p>Generate random int between min and max, may contain min, not max.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -63,6 +70,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func RandInt(min, max int) int
|
func RandInt(min, max int) int
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -79,16 +87,16 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="RandString">RandString</span>
|
||||||
|
|
||||||
|
<p>Generate random given length string. only contains letter (a-zA-Z)</p>
|
||||||
### <span id="RandString">RandInt</span>
|
|
||||||
<p>Generate random given length string.</p>
|
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func RandString(length int) string
|
func RandString(length int) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -101,14 +109,116 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
randStr := random.RandString(6)
|
randStr := random.RandString(6)
|
||||||
fmt.Println(randStr)
|
fmt.Println(randStr) //pGWsze
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="RandUpper">RandUpper</span>
|
||||||
|
|
||||||
|
<p>Generate a random upper case string</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func RandUpper(length int) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/random"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
randStr := random.RandString(6)
|
||||||
|
fmt.Println(randStr) //PACWGF
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="RandLower">RandLower</span>
|
||||||
|
|
||||||
|
<p>Generate a random lower case string</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func RandLower(length int) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/random"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
randStr := random.RandLower(6)
|
||||||
|
fmt.Println(randStr) //siqbew
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="RandNumeral">RandNumeral</span>
|
||||||
|
|
||||||
|
<p>Generate a random numeral string</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func RandNumeral(length int) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/random"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
randStr := random.RandNumeral(6)
|
||||||
|
fmt.Println(randStr) //035172
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="RandNumeralOrLetter">RandNumeralOrLetter</span>
|
||||||
|
|
||||||
|
<p>generate a random numeral or letter string</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func RandNumeralOrLetter(length int) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/random"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
randStr := random.RandNumeralOrLetter(6)
|
||||||
|
fmt.Println(randStr) //0aW7cQ
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="UUIdV4">UUIdV4</span>
|
### <span id="UUIdV4">UUIdV4</span>
|
||||||
|
|
||||||
<p>Generate a random UUID of version 4 according to RFC 4122.</p>
|
<p>Generate a random UUID of version 4 according to RFC 4122.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -116,6 +226,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func UUIdV4() (string, error)
|
func UUIdV4() (string, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -134,5 +245,3 @@ func main() {
|
|||||||
fmt.Println(uuid)
|
fmt.Println(uuid)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Random
|
# Random
|
||||||
|
|
||||||
random 随机数生成器包,可以生成随机[]bytes, int, string。
|
random 随机数生成器包,可以生成随机[]bytes, int, string。
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -7,10 +8,10 @@ random随机数生成器包,可以生成随机[]bytes, int, string。
|
|||||||
|
|
||||||
- [https://github.com/duke-git/lancet/blob/main/random/random.go](https://github.com/duke-git/lancet/blob/main/random/random.go)
|
- [https://github.com/duke-git/lancet/blob/main/random/random.go](https://github.com/duke-git/lancet/blob/main/random/random.go)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 用法:
|
## 用法:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/v2/random"
|
"github.com/duke-git/lancet/v2/random"
|
||||||
@@ -20,18 +21,22 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
- [RandBytes](#RandBytes)
|
- [RandBytes](#RandBytes)
|
||||||
- [RandInt](#RandInt)
|
- [RandInt](#RandInt)
|
||||||
- [RandString](#RandString)
|
- [RandString](#RandString)
|
||||||
|
- [RandUpper](#RandUpper)
|
||||||
|
- [RandLower](#RandLower)
|
||||||
|
- [RandNumeral](#RandNumeral)
|
||||||
|
- [RandNumeralOrLetter](#RandNumeralOrLetter)
|
||||||
- [UUIdV4](#UUIdV4)
|
- [UUIdV4](#UUIdV4)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 文档
|
## 文档
|
||||||
|
|
||||||
|
|
||||||
### <span id="RandBytes">RandBytes</span>
|
### <span id="RandBytes">RandBytes</span>
|
||||||
|
|
||||||
<p>生成随机字节切片</p>
|
<p>生成随机字节切片</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -39,6 +44,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func RandBytes(length int) []byte
|
func RandBytes(length int) []byte
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -55,8 +61,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="RandInt">RandInt</span>
|
### <span id="RandInt">RandInt</span>
|
||||||
|
|
||||||
<p>生成随机int, 范围[min, max)</p>
|
<p>生成随机int, 范围[min, max)</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -64,6 +70,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func RandInt(min, max int) int
|
func RandInt(min, max int) int
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -80,16 +87,16 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="RandString">RandString</span>
|
||||||
|
|
||||||
|
<p>生成给定长度的随机字符串,只包含字母(a-zA-Z)</p>
|
||||||
### <span id="RandString">RandInt</span>
|
|
||||||
<p>生成随机给定长度的随机字符串</p>
|
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func RandString(length int) string
|
func RandString(length int) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -102,13 +109,116 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
randStr := random.RandString(6)
|
randStr := random.RandString(6)
|
||||||
fmt.Println(randStr)
|
fmt.Println(randStr) //pGWsze
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="RandUpper">RandUpper</span>
|
||||||
|
|
||||||
|
<p>生成给定长度的随机大写字母字符串</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func RandUpper(length int) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/random"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
randStr := random.RandString(6)
|
||||||
|
fmt.Println(randStr) //PACWGF
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="RandLower">RandLower</span>
|
||||||
|
|
||||||
|
<p>生成给定长度的随机小写字母字符串</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func RandLower(length int) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/random"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
randStr := random.RandLower(6)
|
||||||
|
fmt.Println(randStr) //siqbew
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="RandNumeral">RandNumeral</span>
|
||||||
|
|
||||||
|
<p>生成给定长度的随机数字字符串</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func RandNumeral(length int) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/random"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
randStr := random.RandNumeral(6)
|
||||||
|
fmt.Println(randStr) //035172
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="RandNumeralOrLetter">RandNumeralOrLetter</span>
|
||||||
|
|
||||||
|
<p>生成给定长度的随机字符串(数字+字母)</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func RandNumeralOrLetter(length int) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/random"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
randStr := random.RandNumeralOrLetter(6)
|
||||||
|
fmt.Println(randStr) //0aW7cQ
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="UUIdV4">UUIdV4</span>
|
### <span id="UUIdV4">UUIdV4</span>
|
||||||
|
|
||||||
<p>生成UUID v4字符串</p>
|
<p>生成UUID v4字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -116,6 +226,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func UUIdV4() (string, error)
|
func UUIdV4() (string, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -134,5 +245,3 @@ func main() {
|
|||||||
fmt.Println(uuid)
|
fmt.Println(uuid)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
461
docs/slice.md
461
docs/slice.md
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -36,7 +36,6 @@ import (
|
|||||||
- [SnakeCase](#SnakeCase)
|
- [SnakeCase](#SnakeCase)
|
||||||
- [SplitEx](#SplitEx)
|
- [SplitEx](#SplitEx)
|
||||||
- [Wrap](#Wrap)
|
- [Wrap](#Wrap)
|
||||||
|
|
||||||
- [Unwrap](#Unwrap)
|
- [Unwrap](#Unwrap)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -36,7 +36,6 @@ import (
|
|||||||
- [SnakeCase](#SnakeCase)
|
- [SnakeCase](#SnakeCase)
|
||||||
- [SplitEx](#SplitEx)
|
- [SplitEx](#SplitEx)
|
||||||
- [Wrap](#Wrap)
|
- [Wrap](#Wrap)
|
||||||
|
|
||||||
- [Unwrap](#Unwrap)
|
- [Unwrap](#Unwrap)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -34,7 +34,6 @@ import (
|
|||||||
- [IsCreditCard](#IsCreditCard)
|
- [IsCreditCard](#IsCreditCard)
|
||||||
- [IsDns](#IsDns)
|
- [IsDns](#IsDns)
|
||||||
- [IsEmail](#IsEmail)
|
- [IsEmail](#IsEmail)
|
||||||
|
|
||||||
- [IsEmptyString](#IsEmptyString)
|
- [IsEmptyString](#IsEmptyString)
|
||||||
- [IsFloatStr](#IsFloatStr)
|
- [IsFloatStr](#IsFloatStr)
|
||||||
- [IsNumberStr](#IsNumberStr)
|
- [IsNumberStr](#IsNumberStr)
|
||||||
|
|||||||
@@ -34,7 +34,6 @@ import (
|
|||||||
- [IsCreditCard](#IsCreditCard)
|
- [IsCreditCard](#IsCreditCard)
|
||||||
- [IsDns](#IsDns)
|
- [IsDns](#IsDns)
|
||||||
- [IsEmail](#IsEmail)
|
- [IsEmail](#IsEmail)
|
||||||
|
|
||||||
- [IsEmptyString](#IsEmptyString)
|
- [IsEmptyString](#IsEmptyString)
|
||||||
- [IsFloatStr](#IsFloatStr)
|
- [IsFloatStr](#IsFloatStr)
|
||||||
- [IsNumberStr](#IsNumberStr)
|
- [IsNumberStr](#IsNumberStr)
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"archive/zip"
|
"archive/zip"
|
||||||
"bufio"
|
"bufio"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@@ -213,6 +214,7 @@ func Zip(fpath string, destPath string) error {
|
|||||||
|
|
||||||
// UnZip unzip the file and save it to destPath
|
// UnZip unzip the file and save it to destPath
|
||||||
func UnZip(zipFile string, destPath string) error {
|
func UnZip(zipFile string, destPath string) error {
|
||||||
|
|
||||||
zipReader, err := zip.OpenReader(zipFile)
|
zipReader, err := zip.OpenReader(zipFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -220,7 +222,12 @@ func UnZip(zipFile string, destPath string) error {
|
|||||||
defer zipReader.Close()
|
defer zipReader.Close()
|
||||||
|
|
||||||
for _, f := range zipReader.File {
|
for _, f := range zipReader.File {
|
||||||
path := filepath.Join(destPath, f.Name)
|
//issue#62: fix ZipSlip bug
|
||||||
|
path, err := safeFilepathJoin(destPath, f.Name)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if f.FileInfo().IsDir() {
|
if f.FileInfo().IsDir() {
|
||||||
os.MkdirAll(path, os.ModePerm)
|
os.MkdirAll(path, os.ModePerm)
|
||||||
} else {
|
} else {
|
||||||
@@ -249,6 +256,17 @@ func UnZip(zipFile string, destPath string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func safeFilepathJoin(path1, path2 string) (string, error) {
|
||||||
|
relPath, err := filepath.Rel(".", path2)
|
||||||
|
if err != nil || strings.HasPrefix(relPath, "..") {
|
||||||
|
return "", fmt.Errorf("(zipslip) filepath is unsafe %q: %v", path2, err)
|
||||||
|
}
|
||||||
|
if path1 == "" {
|
||||||
|
path1 = "."
|
||||||
|
}
|
||||||
|
return filepath.Join(path1, filepath.Join("/", relPath)), nil
|
||||||
|
}
|
||||||
|
|
||||||
// IsLink checks if a file is symbol link or not
|
// IsLink checks if a file is symbol link or not
|
||||||
func IsLink(path string) bool {
|
func IsLink(path string) bool {
|
||||||
fi, err := os.Lstat(path)
|
fi, err := os.Lstat(path)
|
||||||
|
|||||||
174
iterator/iterator.go
Normal file
174
iterator/iterator.go
Normal file
@@ -0,0 +1,174 @@
|
|||||||
|
// Copyright 2022 dudaodong@gmail.com. All rights resulterved.
|
||||||
|
// Use of this source code is governed by MIT license
|
||||||
|
|
||||||
|
// Package iterator provides a way to iterate over values stored in containers.
|
||||||
|
// note:
|
||||||
|
// 1. Full feature iterator is complicated, this pacakge is just a experiment to explore how iterators could work in Go.
|
||||||
|
// 2. The functionality of this package is very simple and limited, may not meet the actual dev needs.
|
||||||
|
// 3. It is currently under development, unstable, and will not be completed for some time in the future.
|
||||||
|
// So, based on above factors, you may not use it in production. but, anyone is welcome to improve it.
|
||||||
|
// Hope that Go can support iterator in future. see https://github.com/golang/go/discussions/54245 and https://github.com/golang/go/discussions/56413
|
||||||
|
package iterator
|
||||||
|
|
||||||
|
import "github.com/duke-git/lancet/v2/lancetconstraints"
|
||||||
|
|
||||||
|
// Iterator supports iterating over a sequence of values of type `E`.
|
||||||
|
type Iterator[T any] interface {
|
||||||
|
// Next checks if there is a next value in the iteration or not
|
||||||
|
HasNext() bool
|
||||||
|
// Next returns the next value in the iteration if there is one,
|
||||||
|
// and reports whether the returned value is valid.
|
||||||
|
// Once Next returns ok==false, the iteration is over,
|
||||||
|
// and all subsequent calls will return ok==false.
|
||||||
|
Next() (item T, ok bool)
|
||||||
|
}
|
||||||
|
|
||||||
|
// StopIterator is an interface for stopping Iterator.
|
||||||
|
type StopIterator[T any] interface {
|
||||||
|
Iterator[T]
|
||||||
|
|
||||||
|
// Stop indicates that the iterator will no longer be used.
|
||||||
|
// After a call to Stop, future calls to Next may panic.
|
||||||
|
// Stop may be called multiple times;
|
||||||
|
// all calls after the first will have no effect.
|
||||||
|
Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteIter is an Iter that implements a Delete method.
|
||||||
|
type DeleteIterator[T any] interface {
|
||||||
|
Iterator[T]
|
||||||
|
|
||||||
|
// Delete deletes the current iterator element;
|
||||||
|
// that is, the one returned by the last call to Next.
|
||||||
|
// Delete should panic if called before Next or after
|
||||||
|
// Next returns false.
|
||||||
|
Delete()
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetIterator is an Iter that implements a Set method.
|
||||||
|
type SetIterator[T any] interface {
|
||||||
|
Iterator[T]
|
||||||
|
|
||||||
|
// Set replaces the current iterator element with v.
|
||||||
|
// Set should panic if called before Next or after
|
||||||
|
// Next returns false.
|
||||||
|
Set(v T)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PrevIterator is an iterator with a Prev method.
|
||||||
|
type PrevIterator[T any] interface {
|
||||||
|
Iterator[T]
|
||||||
|
|
||||||
|
// Prev moves the iterator to the previous position.
|
||||||
|
// After calling Prev, Next will return the value at
|
||||||
|
// that position in the container. For example, after
|
||||||
|
// it.Next() returning (v, true)
|
||||||
|
// it.Prev()
|
||||||
|
// another call to it.Next will again return (v, true).
|
||||||
|
// Calling Prev before calling Next may panic.
|
||||||
|
// Calling Prev after Next returns false will move
|
||||||
|
// to the last element, or, if there are no elements,
|
||||||
|
// to the iterator's initial state.
|
||||||
|
Prev()
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Functions that create an Iterator from some other type. //
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
// FromSlice returns an iterator over a slice of data.
|
||||||
|
func FromSlice[T any](slice []T) Iterator[T] {
|
||||||
|
return &sliceIterator[T]{slice: slice, index: -1}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToSlice[T any](iter Iterator[T]) []T {
|
||||||
|
result := []T{}
|
||||||
|
for item, ok := iter.Next(); ok; item, ok = iter.Next() {
|
||||||
|
result = append(result, item)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
type sliceIterator[T any] struct {
|
||||||
|
slice []T
|
||||||
|
index int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (iter *sliceIterator[T]) HasNext() bool {
|
||||||
|
return iter.index < len(iter.slice)-1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (iter *sliceIterator[T]) Next() (T, bool) {
|
||||||
|
iter.index++
|
||||||
|
ok := iter.index >= 0 && iter.index < len(iter.slice)
|
||||||
|
var item T
|
||||||
|
if ok {
|
||||||
|
item = iter.slice[iter.index]
|
||||||
|
}
|
||||||
|
return item, ok
|
||||||
|
|
||||||
|
// if len(iter.slice) == 0 {
|
||||||
|
// var zero T
|
||||||
|
// return zero, false
|
||||||
|
// }
|
||||||
|
// iter.index++
|
||||||
|
// item := iter.slice[0]
|
||||||
|
// iter.slice = iter.slice[1:]
|
||||||
|
// return item, true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prev implements PrevIterator.
|
||||||
|
func (iter *sliceIterator[T]) Prev() {
|
||||||
|
if iter.index == -1 {
|
||||||
|
panic("Next function should be called Prev")
|
||||||
|
}
|
||||||
|
if iter.HasNext() {
|
||||||
|
iter.index--
|
||||||
|
} else {
|
||||||
|
iter.index = len(iter.slice) - 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set implements SetIterator.
|
||||||
|
func (iter *sliceIterator[T]) Set(value T) {
|
||||||
|
if iter.index == -1 {
|
||||||
|
panic("Next function should be called Set")
|
||||||
|
}
|
||||||
|
if iter.index >= len(iter.slice) || len(iter.slice) == 0 {
|
||||||
|
panic("No element in current iterator")
|
||||||
|
}
|
||||||
|
iter.slice[iter.index] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
// FromRange creates a iterator which returns the numeric range between start inclusive and end
|
||||||
|
// exclusive by the step size. start should be less than end, step shoud be positive.
|
||||||
|
func FromRange[T lancetconstraints.Number](start, end, step T) Iterator[T] {
|
||||||
|
if end < start {
|
||||||
|
panic("RangeIterator: start should be before end")
|
||||||
|
} else if step <= 0 {
|
||||||
|
panic("RangeIterator: step should be positive")
|
||||||
|
}
|
||||||
|
|
||||||
|
return &rangeIterator[T]{start: start, end: end, step: step}
|
||||||
|
}
|
||||||
|
|
||||||
|
type rangeIterator[T lancetconstraints.Number] struct {
|
||||||
|
start, end, step T
|
||||||
|
}
|
||||||
|
|
||||||
|
func (iter *rangeIterator[T]) HasNext() bool {
|
||||||
|
if iter.start >= iter.end {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (iter *rangeIterator[T]) Next() (T, bool) {
|
||||||
|
if iter.start >= iter.end {
|
||||||
|
var zero T
|
||||||
|
return zero, false
|
||||||
|
}
|
||||||
|
num := iter.start
|
||||||
|
iter.start += iter.step
|
||||||
|
return num, true
|
||||||
|
}
|
||||||
50
iterator/iterator_test.go
Normal file
50
iterator/iterator_test.go
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
// Copyright 2022 dudaodong@gmail.com. All rights resulterved.
|
||||||
|
// Use of this source code is governed by MIT license
|
||||||
|
|
||||||
|
// Package iterator implements some feature of C++ STL iterators
|
||||||
|
package iterator
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/duke-git/lancet/v2/internal"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSliceIterator(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestSliceIterator")
|
||||||
|
|
||||||
|
// HashNext
|
||||||
|
t.Run("slice iterator HasNext: ", func(t *testing.T) {
|
||||||
|
iter1 := FromSlice([]int{1, 2, 3, 4})
|
||||||
|
for {
|
||||||
|
item, _ := iter1.Next()
|
||||||
|
|
||||||
|
if item == 4 {
|
||||||
|
assert.Equal(false, iter1.HasNext())
|
||||||
|
break
|
||||||
|
} else {
|
||||||
|
assert.Equal(true, iter1.HasNext())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
iter2 := FromSlice([]int{})
|
||||||
|
assert.Equal(false, iter2.HasNext())
|
||||||
|
})
|
||||||
|
|
||||||
|
//Next
|
||||||
|
t.Run("slice iterator Next: ", func(t *testing.T) {
|
||||||
|
iter1 := FromSlice([]int{1, 2, 3, 4})
|
||||||
|
for i := 0; i < 4; i++ {
|
||||||
|
item, ok := iter1.Next()
|
||||||
|
if !ok {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
assert.Equal(i+1, item)
|
||||||
|
}
|
||||||
|
|
||||||
|
iter2 := FromSlice([]int{})
|
||||||
|
_, ok := iter2.Next()
|
||||||
|
assert.Equal(false, ok)
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
61
iterator/operation.go
Normal file
61
iterator/operation.go
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
// Copyright 2022 dudaodong@gmail.com. All rights resulterved.
|
||||||
|
// Use of this source code is governed by MIT license
|
||||||
|
|
||||||
|
// Package iterator provides a way to iterate over values stored in containers.
|
||||||
|
// note:
|
||||||
|
// 1. Full feature iterator is complicated, this pacakge is just a experiment to explore how iterators could work in Go.
|
||||||
|
// 2. The functionality of this package is very simple and limited, may not meet the actual dev needs.
|
||||||
|
// 3. It is currently under development, unstable, and will not be completed for some time in the future.
|
||||||
|
// So, based on above factors, you may not use it in production. but, anyone is welcome to improve it.
|
||||||
|
// Hope that Go can support iterator in future. see https://github.com/golang/go/discussions/54245 and https://github.com/golang/go/discussions/56413
|
||||||
|
package iterator
|
||||||
|
|
||||||
|
// Map creates a new iterator which applies a function to all items of input iterator.
|
||||||
|
func Map[T any, U any](iter Iterator[T], iteratee func(item T) U) Iterator[U] {
|
||||||
|
return &mapIterator[T, U]{
|
||||||
|
iter: iter,
|
||||||
|
iteratee: iteratee,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type mapIterator[T any, U any] struct {
|
||||||
|
iter Iterator[T]
|
||||||
|
iteratee func(T) U
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mr *mapIterator[T, U]) HasNext() bool {
|
||||||
|
return mr.iter.HasNext()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mr *mapIterator[T, U]) Next() (U, bool) {
|
||||||
|
var zero U
|
||||||
|
item, ok := mr.iter.Next()
|
||||||
|
if !ok {
|
||||||
|
return zero, false
|
||||||
|
}
|
||||||
|
return mr.iteratee(item), true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filter creates a new iterator that returns only the items that pass specified predicate function.
|
||||||
|
func Filter[T any](iter Iterator[T], predicateFunc func(item T) bool) Iterator[T] {
|
||||||
|
return &filterIterator[T]{iter: iter, predicateFunc: predicateFunc}
|
||||||
|
}
|
||||||
|
|
||||||
|
type filterIterator[T any] struct {
|
||||||
|
iter Iterator[T]
|
||||||
|
predicateFunc func(T) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fr *filterIterator[T]) Next() (T, bool) {
|
||||||
|
for item, ok := fr.iter.Next(); ok; item, ok = fr.iter.Next() {
|
||||||
|
if fr.predicateFunc(item) {
|
||||||
|
return item, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var zero T
|
||||||
|
return zero, false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fr *filterIterator[T]) HasNext() bool {
|
||||||
|
return fr.iter.HasNext()
|
||||||
|
}
|
||||||
@@ -16,3 +16,8 @@ type Comparator interface {
|
|||||||
type Number interface {
|
type Number interface {
|
||||||
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~float32 | ~float64
|
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~float32 | ~float64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ordered is a constraint that permits any ordered type: any type that supports the operators < <= >= >
|
||||||
|
type Ordered interface {
|
||||||
|
Number | ~string
|
||||||
|
}
|
||||||
|
|||||||
@@ -12,18 +12,12 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RandString generate random string
|
const (
|
||||||
// see https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-go
|
NUMERAL = "0123456789"
|
||||||
func RandString(length int) string {
|
LOWER_LETTERS = "abcdefghijklmnopqrstuvwxyz"
|
||||||
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
UPPER_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
|
LETTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
b := make([]byte, length)
|
)
|
||||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
||||||
for i := range b {
|
|
||||||
b[i] = letters[r.Int63()%int64(len(letters))]
|
|
||||||
}
|
|
||||||
return string(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
// RandInt generate random int between min and max, maybe min, not be max
|
// RandInt generate random int between min and max, maybe min, not be max
|
||||||
func RandInt(min, max int) int {
|
func RandInt(min, max int) int {
|
||||||
@@ -50,6 +44,41 @@ func RandBytes(length int) []byte {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RandString generate random string
|
||||||
|
func RandString(length int) string {
|
||||||
|
return random(LETTERS, length)
|
||||||
|
}
|
||||||
|
|
||||||
|
// RandUpper generate a random upper case string
|
||||||
|
func RandUpper(length int) string {
|
||||||
|
return random(UPPER_LETTERS, length)
|
||||||
|
}
|
||||||
|
|
||||||
|
// RandLower generate a random lower case string
|
||||||
|
func RandLower(length int) string {
|
||||||
|
return random(LOWER_LETTERS, length)
|
||||||
|
}
|
||||||
|
|
||||||
|
// RandNumeral generate a random numeral string
|
||||||
|
func RandNumeral(length int) string {
|
||||||
|
return random(NUMERAL, length)
|
||||||
|
}
|
||||||
|
|
||||||
|
// RandNumeralOrLetter generate a random numeral or letter string
|
||||||
|
func RandNumeralOrLetter(length int) string {
|
||||||
|
return random(NUMERAL+LETTERS, length)
|
||||||
|
}
|
||||||
|
|
||||||
|
// random generate a random string based on given string range
|
||||||
|
func random(s string, length int) string {
|
||||||
|
b := make([]byte, length)
|
||||||
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||||
|
for i := range b {
|
||||||
|
b[i] = s[r.Int63()%int64(len(s))]
|
||||||
|
}
|
||||||
|
return string(b)
|
||||||
|
}
|
||||||
|
|
||||||
// UUIdV4 generate a random UUID of version 4 according to RFC 4122
|
// UUIdV4 generate a random UUID of version 4 according to RFC 4122
|
||||||
func UUIdV4() (string, error) {
|
func UUIdV4() (string, error) {
|
||||||
uuid := make([]byte, 16)
|
uuid := make([]byte, 16)
|
||||||
|
|||||||
@@ -19,6 +19,51 @@ func TestRandString(t *testing.T) {
|
|||||||
assert.Equal(true, reg.MatchString(randStr))
|
assert.Equal(true, reg.MatchString(randStr))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRandUpper(t *testing.T) {
|
||||||
|
pattern := `^[A-Z]+$`
|
||||||
|
reg := regexp.MustCompile(pattern)
|
||||||
|
|
||||||
|
randStr := RandUpper(6)
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestRandUpper")
|
||||||
|
assert.Equal(6, len(randStr))
|
||||||
|
assert.Equal(true, reg.MatchString(randStr))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRandLower(t *testing.T) {
|
||||||
|
pattern := `^[a-z]+$`
|
||||||
|
reg := regexp.MustCompile(pattern)
|
||||||
|
|
||||||
|
randStr := RandLower(6)
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestRandLower")
|
||||||
|
assert.Equal(6, len(randStr))
|
||||||
|
assert.Equal(true, reg.MatchString(randStr))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRandNumeral(t *testing.T) {
|
||||||
|
pattern := `^[0-9]+$`
|
||||||
|
reg := regexp.MustCompile(pattern)
|
||||||
|
|
||||||
|
randStr := RandNumeral(12)
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestRandNumeral")
|
||||||
|
assert.Equal(12, len(randStr))
|
||||||
|
assert.Equal(true, reg.MatchString(randStr))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRandNumeralOrLetter(t *testing.T) {
|
||||||
|
pattern := `^[0-9a-zA-Z]+$`
|
||||||
|
reg := regexp.MustCompile(pattern)
|
||||||
|
|
||||||
|
randStr := RandNumeralOrLetter(10)
|
||||||
|
t.Log(randStr)
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestRandNumeralOrLetter")
|
||||||
|
assert.Equal(10, len(randStr))
|
||||||
|
assert.Equal(true, reg.MatchString(randStr))
|
||||||
|
}
|
||||||
|
|
||||||
func TestRandInt(t *testing.T) {
|
func TestRandInt(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestRandInt")
|
assert := internal.NewAssert(t, "TestRandInt")
|
||||||
|
|
||||||
|
|||||||
356
slice/slice.go
356
slice/slice.go
@@ -10,22 +10,24 @@ import (
|
|||||||
"math/rand"
|
"math/rand"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
|
"github.com/duke-git/lancet/v2/lancetconstraints"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Contain check if the value is in the slice or not
|
// Contain check if the target value is in the slice or not
|
||||||
func Contain[T comparable](slice []T, value T) bool {
|
func Contain[T comparable](slice []T, target T) bool {
|
||||||
set := make(map[T]struct{}, len(slice))
|
for _, item := range slice {
|
||||||
for _, v := range slice {
|
if item == target {
|
||||||
set[v] = struct{}{}
|
return true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_, ok := set[value]
|
return false
|
||||||
return ok
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ContainSubSlice check if the slice contain subslice or not
|
// ContainSubSlice check if the slice contain a given subslice or not
|
||||||
func ContainSubSlice[T comparable](slice, subslice []T) bool {
|
func ContainSubSlice[T comparable](slice, subSlice []T) bool {
|
||||||
for _, v := range subslice {
|
for _, v := range subSlice {
|
||||||
if !Contain(slice, v) {
|
if !Contain(slice, v) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -34,47 +36,35 @@ func ContainSubSlice[T comparable](slice, subslice []T) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Chunk creates an slice of elements split into groups the length of size.
|
// Chunk creates a slice of elements split into groups the length of size
|
||||||
func Chunk[T any](slice []T, size int) [][]T {
|
func Chunk[T any](slice []T, size int) [][]T {
|
||||||
var result [][]T
|
result := [][]T{}
|
||||||
|
|
||||||
if len(slice) == 0 || size <= 0 {
|
if len(slice) == 0 || size <= 0 {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
length := len(slice)
|
for _, item := range slice {
|
||||||
if size == 1 || size >= length {
|
l := len(result)
|
||||||
for _, v := range slice {
|
if l == 0 || len(result[l-1]) == size {
|
||||||
var tmp []T
|
result = append(result, []T{})
|
||||||
tmp = append(tmp, v)
|
l++
|
||||||
result = append(result, tmp)
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// divide slice equally
|
result[l-1] = append(result[l-1], item)
|
||||||
divideNum := length/size + 1
|
|
||||||
for i := 0; i < divideNum; i++ {
|
|
||||||
if i == divideNum-1 {
|
|
||||||
if len(slice[i*size:]) > 0 {
|
|
||||||
result = append(result, slice[i*size:])
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
result = append(result, slice[i*size:(i+1)*size])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compact creates an slice with all falsey values removed. The values false, nil, 0, and "" are falsey
|
// Compact creates an slice with all falsey values removed. The values false, nil, 0, and "" are falsey
|
||||||
func Compact[T any](slice []T) []T {
|
func Compact[T comparable](slice []T) []T {
|
||||||
result := make([]T, 0)
|
var zero T
|
||||||
|
|
||||||
|
result := []T{}
|
||||||
for _, v := range slice {
|
for _, v := range slice {
|
||||||
if !reflect.DeepEqual(v, nil) &&
|
if v != zero {
|
||||||
!reflect.DeepEqual(v, false) &&
|
|
||||||
!reflect.DeepEqual(v, "") &&
|
|
||||||
!reflect.DeepEqual(v, 0) {
|
|
||||||
result = append(result, v)
|
result = append(result, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -82,11 +72,11 @@ func Compact[T any](slice []T) []T {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Concat creates a new slice concatenating slice with any additional slices and/or values.
|
// Concat creates a new slice concatenating slice with any additional slices.
|
||||||
func Concat[T any](slice []T, values ...[]T) []T {
|
func Concat[T any](slice []T, slices ...[]T) []T {
|
||||||
result := append([]T{}, slice...)
|
result := append([]T{}, slice...)
|
||||||
|
|
||||||
for _, v := range values {
|
for _, v := range slices {
|
||||||
result = append(result, v...)
|
result = append(result, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,7 +85,8 @@ func Concat[T any](slice []T, values ...[]T) []T {
|
|||||||
|
|
||||||
// Difference creates an slice of whose element in slice but not in comparedSlice
|
// Difference creates an slice of whose element in slice but not in comparedSlice
|
||||||
func Difference[T comparable](slice, comparedSlice []T) []T {
|
func Difference[T comparable](slice, comparedSlice []T) []T {
|
||||||
var result []T
|
result := []T{}
|
||||||
|
|
||||||
for _, v := range slice {
|
for _, v := range slice {
|
||||||
if !Contain(comparedSlice, v) {
|
if !Contain(comparedSlice, v) {
|
||||||
result = append(result, v)
|
result = append(result, v)
|
||||||
@@ -123,7 +114,7 @@ func DifferenceBy[T comparable](slice []T, comparedSlice []T, iteratee func(inde
|
|||||||
}
|
}
|
||||||
|
|
||||||
//DifferenceWith accepts comparator which is invoked to compare elements of slice to values. The order and references of result values are determined by the first slice. The comparator is invoked with two arguments: (arrVal, othVal).
|
//DifferenceWith accepts comparator which is invoked to compare elements of slice to values. The order and references of result values are determined by the first slice. The comparator is invoked with two arguments: (arrVal, othVal).
|
||||||
func DifferenceWith[T any](slice []T, comparedSlice []T, comparator func(value, otherValue T) bool) []T {
|
func DifferenceWith[T any](slice []T, comparedSlice []T, comparator func(item1, item2 T) bool) []T {
|
||||||
result := make([]T, 0)
|
result := make([]T, 0)
|
||||||
|
|
||||||
getIndex := func(arr []T, item T, comparison func(v1, v2 T) bool) int {
|
getIndex := func(arr []T, item T, comparison func(v1, v2 T) bool) int {
|
||||||
@@ -168,9 +159,8 @@ func EqualWith[T, U any](slice1 []T, slice2 []U, comparator func(T, U) bool) boo
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, v1 := range slice1 {
|
for i, v := range slice1 {
|
||||||
v2 := slice2[i]
|
if !comparator(v, slice2[i]) {
|
||||||
if !comparator(v1, v2) {
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -180,76 +170,68 @@ func EqualWith[T, U any](slice1 []T, slice2 []U, comparator func(T, U) bool) boo
|
|||||||
|
|
||||||
// Every return true if all of the values in the slice pass the predicate function.
|
// Every return true if all of the values in the slice pass the predicate function.
|
||||||
func Every[T any](slice []T, predicate func(index int, item T) bool) bool {
|
func Every[T any](slice []T, predicate func(index int, item T) bool) bool {
|
||||||
if predicate == nil {
|
|
||||||
panic("predicate func is missing")
|
|
||||||
}
|
|
||||||
|
|
||||||
var currentLength int
|
|
||||||
for i, v := range slice {
|
for i, v := range slice {
|
||||||
if predicate(i, v) {
|
if !predicate(i, v) {
|
||||||
currentLength++
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return currentLength == len(slice)
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// None return true if all the values in the slice mismatch the criteria
|
// None return true if all the values in the slice mismatch the criteria
|
||||||
func None[T any](slice []T, predicate func(index int, item T) bool) bool {
|
func None[T any](slice []T, predicate func(index int, item T) bool) bool {
|
||||||
if predicate == nil {
|
l := 0
|
||||||
panic("predicate func is missing")
|
|
||||||
}
|
|
||||||
|
|
||||||
var currentLength int
|
|
||||||
for i, v := range slice {
|
for i, v := range slice {
|
||||||
if !predicate(i, v) {
|
if !predicate(i, v) {
|
||||||
currentLength++
|
l++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return currentLength == len(slice)
|
return l == len(slice)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Some return true if any of the values in the list pass the predicate function.
|
// Some return true if any of the values in the list pass the predicate function.
|
||||||
func Some[T any](slice []T, predicate func(index int, item T) bool) bool {
|
func Some[T any](slice []T, predicate func(index int, item T) bool) bool {
|
||||||
if predicate == nil {
|
|
||||||
panic("predicate func is missing")
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, v := range slice {
|
for i, v := range slice {
|
||||||
if predicate(i, v) {
|
if predicate(i, v) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filter iterates over elements of slice, returning an slice of all elements pass the predicate function
|
// Filter iterates over elements of slice, returning an slice of all elements pass the predicate function
|
||||||
func Filter[T any](slice []T, predicate func(index int, item T) bool) []T {
|
func Filter[T any](slice []T, predicate func(index int, item T) bool) []T {
|
||||||
if predicate == nil {
|
|
||||||
panic("predicate func is missing")
|
|
||||||
}
|
|
||||||
|
|
||||||
result := make([]T, 0)
|
result := make([]T, 0)
|
||||||
|
|
||||||
for i, v := range slice {
|
for i, v := range slice {
|
||||||
if predicate(i, v) {
|
if predicate(i, v) {
|
||||||
result = append(result, v)
|
result = append(result, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Count iterates over elements of slice, returns a count of all matched elements
|
// Count returns the number of occurrences of the given item in the slice
|
||||||
func Count[T any](slice []T, predicate func(index int, item T) bool) int {
|
func Count[T comparable](slice []T, item T) int {
|
||||||
if predicate == nil {
|
count := 0
|
||||||
panic("predicate func is missing")
|
|
||||||
|
for _, v := range slice {
|
||||||
|
if item == v {
|
||||||
|
count++
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(slice) == 0 {
|
return count
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var count int
|
// CountBy iterates over elements of slice with predicate function, returns the number of all matched elements
|
||||||
|
func CountBy[T any](slice []T, predicate func(index int, item T) bool) int {
|
||||||
|
count := 0
|
||||||
|
|
||||||
for i, v := range slice {
|
for i, v := range slice {
|
||||||
if predicate(i, v) {
|
if predicate(i, v) {
|
||||||
count++
|
count++
|
||||||
@@ -261,10 +243,6 @@ func Count[T any](slice []T, predicate func(index int, item T) bool) int {
|
|||||||
|
|
||||||
// GroupBy iterate over elements of the slice, each element will be group by criteria, returns two slices
|
// GroupBy iterate over elements of the slice, each element will be group by criteria, returns two slices
|
||||||
func GroupBy[T any](slice []T, groupFn func(index int, item T) bool) ([]T, []T) {
|
func GroupBy[T any](slice []T, groupFn func(index int, item T) bool) ([]T, []T) {
|
||||||
if groupFn == nil {
|
|
||||||
panic("groupFn func is missing")
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(slice) == 0 {
|
if len(slice) == 0 {
|
||||||
return make([]T, 0), make([]T, 0)
|
return make([]T, 0), make([]T, 0)
|
||||||
}
|
}
|
||||||
@@ -285,11 +263,7 @@ func GroupBy[T any](slice []T, groupFn func(index int, item T) bool) ([]T, []T)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GroupWith return a map composed of keys generated from the resultults of running each element of slice thru iteratee.
|
// GroupWith return a map composed of keys generated from the resultults of running each element of slice thru iteratee.
|
||||||
func GroupWith[T any, U comparable](slice []T, iteratee func(T) U) map[U][]T {
|
func GroupWith[T any, U comparable](slice []T, iteratee func(item T) U) map[U][]T {
|
||||||
if iteratee == nil {
|
|
||||||
panic("iteratee func is missing")
|
|
||||||
}
|
|
||||||
|
|
||||||
result := make(map[U][]T)
|
result := make(map[U][]T)
|
||||||
|
|
||||||
for _, v := range slice {
|
for _, v := range slice {
|
||||||
@@ -306,15 +280,8 @@ func GroupWith[T any, U comparable](slice []T, iteratee func(T) U) map[U][]T {
|
|||||||
// Find iterates over elements of slice, returning the first one that passes a truth test on predicate function.
|
// Find iterates over elements of slice, returning the first one that passes a truth test on predicate function.
|
||||||
// If return T is nil then no items matched the predicate func
|
// If return T is nil then no items matched the predicate func
|
||||||
func Find[T any](slice []T, predicate func(index int, item T) bool) (*T, bool) {
|
func Find[T any](slice []T, predicate func(index int, item T) bool) (*T, bool) {
|
||||||
if predicate == nil {
|
|
||||||
panic("predicate func is missing")
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(slice) == 0 {
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
|
|
||||||
index := -1
|
index := -1
|
||||||
|
|
||||||
for i, v := range slice {
|
for i, v := range slice {
|
||||||
if predicate(i, v) {
|
if predicate(i, v) {
|
||||||
index = i
|
index = i
|
||||||
@@ -332,15 +299,8 @@ func Find[T any](slice []T, predicate func(index int, item T) bool) (*T, bool) {
|
|||||||
// FindLast iterates over elements of slice from end to begin, returning the first one that passes a truth test on predicate function.
|
// FindLast iterates over elements of slice from end to begin, returning the first one that passes a truth test on predicate function.
|
||||||
// If return T is nil then no items matched the predicate func
|
// If return T is nil then no items matched the predicate func
|
||||||
func FindLast[T any](slice []T, predicate func(index int, item T) bool) (*T, bool) {
|
func FindLast[T any](slice []T, predicate func(index int, item T) bool) (*T, bool) {
|
||||||
if predicate == nil {
|
|
||||||
panic("predicate func is missing")
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(slice) == 0 {
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
|
|
||||||
index := -1
|
index := -1
|
||||||
|
|
||||||
for i := len(slice) - 1; i >= 0; i-- {
|
for i := len(slice) - 1; i >= 0; i-- {
|
||||||
if predicate(i, slice[i]) {
|
if predicate(i, slice[i]) {
|
||||||
index = i
|
index = i
|
||||||
@@ -386,8 +346,11 @@ func Flatten(slice any) any {
|
|||||||
func FlattenDeep(slice any) any {
|
func FlattenDeep(slice any) any {
|
||||||
sv := sliceValue(slice)
|
sv := sliceValue(slice)
|
||||||
st := sliceElemType(sv.Type())
|
st := sliceElemType(sv.Type())
|
||||||
|
|
||||||
tmp := reflect.MakeSlice(reflect.SliceOf(st), 0, 0)
|
tmp := reflect.MakeSlice(reflect.SliceOf(st), 0, 0)
|
||||||
|
|
||||||
result := flattenRecursive(sv, tmp)
|
result := flattenRecursive(sv, tmp)
|
||||||
|
|
||||||
return result.Interface()
|
return result.Interface()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -408,10 +371,6 @@ func flattenRecursive(value reflect.Value, result reflect.Value) reflect.Value {
|
|||||||
|
|
||||||
// ForEach iterates over elements of slice and invokes function for each element
|
// ForEach iterates over elements of slice and invokes function for each element
|
||||||
func ForEach[T any](slice []T, iteratee func(index int, item T)) {
|
func ForEach[T any](slice []T, iteratee func(index int, item T)) {
|
||||||
if iteratee == nil {
|
|
||||||
panic("iteratee func is missing")
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, v := range slice {
|
for i, v := range slice {
|
||||||
iteratee(i, v)
|
iteratee(i, v)
|
||||||
}
|
}
|
||||||
@@ -419,11 +378,8 @@ func ForEach[T any](slice []T, iteratee func(index int, item T)) {
|
|||||||
|
|
||||||
// Map creates an slice of values by running each element of slice thru iteratee function.
|
// Map creates an slice of values by running each element of slice thru iteratee function.
|
||||||
func Map[T any, U any](slice []T, iteratee func(index int, item T) U) []U {
|
func Map[T any, U any](slice []T, iteratee func(index int, item T) U) []U {
|
||||||
if iteratee == nil {
|
|
||||||
panic("iteratee func is missing")
|
|
||||||
}
|
|
||||||
|
|
||||||
result := make([]U, len(slice), cap(slice))
|
result := make([]U, len(slice), cap(slice))
|
||||||
|
|
||||||
for i, v := range slice {
|
for i, v := range slice {
|
||||||
result[i] = iteratee(i, v)
|
result[i] = iteratee(i, v)
|
||||||
}
|
}
|
||||||
@@ -433,10 +389,6 @@ func Map[T any, U any](slice []T, iteratee func(index int, item T) U) []U {
|
|||||||
|
|
||||||
// Reduce creates an slice of values by running each element of slice thru iteratee function.
|
// Reduce creates an slice of values by running each element of slice thru iteratee function.
|
||||||
func Reduce[T any](slice []T, iteratee func(index int, item1, item2 T) T, initial T) T {
|
func Reduce[T any](slice []T, iteratee func(index int, item1, item2 T) T, initial T) T {
|
||||||
if iteratee == nil {
|
|
||||||
panic("iteratee func is missing")
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(slice) == 0 {
|
if len(slice) == 0 {
|
||||||
return initial
|
return initial
|
||||||
}
|
}
|
||||||
@@ -473,7 +425,19 @@ func ReplaceAll[T comparable](slice []T, old T, new T) []T {
|
|||||||
return Replace(slice, old, new, -1)
|
return Replace(slice, old, new, -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Repeat creates a slice with length n whose elements are param `item`.
|
||||||
|
func Repeat[T any](item T, n int) []T {
|
||||||
|
result := make([]T, n)
|
||||||
|
|
||||||
|
for i := range result {
|
||||||
|
result[i] = item
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
// InterfaceSlice convert param to slice of interface.
|
// InterfaceSlice convert param to slice of interface.
|
||||||
|
// This function is deprecated, use generics feature of go1.18+ for replacement
|
||||||
func InterfaceSlice(slice any) []any {
|
func InterfaceSlice(slice any) []any {
|
||||||
sv := sliceValue(slice)
|
sv := sliceValue(slice)
|
||||||
if sv.IsNil() {
|
if sv.IsNil() {
|
||||||
@@ -489,35 +453,37 @@ func InterfaceSlice(slice any) []any {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// StringSlice convert param to slice of string.
|
// StringSlice convert param to slice of string.
|
||||||
|
// This function is deprecated, use generics feature of go1.18+ for replacement
|
||||||
func StringSlice(slice any) []string {
|
func StringSlice(slice any) []string {
|
||||||
v := sliceValue(slice)
|
v := sliceValue(slice)
|
||||||
|
|
||||||
out := make([]string, v.Len())
|
result := make([]string, v.Len())
|
||||||
for i := 0; i < v.Len(); i++ {
|
for i := 0; i < v.Len(); i++ {
|
||||||
v, ok := v.Index(i).Interface().(string)
|
v, ok := v.Index(i).Interface().(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
panic("invalid element type")
|
panic("invalid element type")
|
||||||
}
|
}
|
||||||
out[i] = v
|
result[i] = v
|
||||||
}
|
}
|
||||||
|
|
||||||
return out
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// IntSlice convert param to slice of int.
|
// IntSlice convert param to slice of int.
|
||||||
|
// This function is deprecated, use generics feature of go1.18+ for replacement
|
||||||
func IntSlice(slice any) []int {
|
func IntSlice(slice any) []int {
|
||||||
sv := sliceValue(slice)
|
sv := sliceValue(slice)
|
||||||
|
|
||||||
out := make([]int, sv.Len())
|
result := make([]int, sv.Len())
|
||||||
for i := 0; i < sv.Len(); i++ {
|
for i := 0; i < sv.Len(); i++ {
|
||||||
v, ok := sv.Index(i).Interface().(int)
|
v, ok := sv.Index(i).Interface().(int)
|
||||||
if !ok {
|
if !ok {
|
||||||
panic("invalid element type")
|
panic("invalid element type")
|
||||||
}
|
}
|
||||||
out[i] = v
|
result[i] = v
|
||||||
}
|
}
|
||||||
|
|
||||||
return out
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteAt delete the element of slice from start index to end index - 1.
|
// DeleteAt delete the element of slice from start index to end index - 1.
|
||||||
@@ -604,12 +570,8 @@ func UpdateAt[T any](slice []T, index int, value T) []T {
|
|||||||
|
|
||||||
// Unique remove duplicate elements in slice.
|
// Unique remove duplicate elements in slice.
|
||||||
func Unique[T comparable](slice []T) []T {
|
func Unique[T comparable](slice []T) []T {
|
||||||
if len(slice) == 0 {
|
result := []T{}
|
||||||
return []T{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// here no use map filter. if use it, the result slice element order is random, not same as origin slice
|
|
||||||
var result []T
|
|
||||||
for i := 0; i < len(slice); i++ {
|
for i := 0; i < len(slice); i++ {
|
||||||
v := slice[i]
|
v := slice[i]
|
||||||
skip := true
|
skip := true
|
||||||
@@ -629,11 +591,8 @@ func Unique[T comparable](slice []T) []T {
|
|||||||
|
|
||||||
// UniqueBy call iteratee func with every item of slice, then remove duplicated.
|
// UniqueBy call iteratee func with every item of slice, then remove duplicated.
|
||||||
func UniqueBy[T comparable](slice []T, iteratee func(item T) T) []T {
|
func UniqueBy[T comparable](slice []T, iteratee func(item T) T) []T {
|
||||||
if len(slice) == 0 {
|
result := []T{}
|
||||||
return []T{}
|
|
||||||
}
|
|
||||||
|
|
||||||
var result []T
|
|
||||||
for _, v := range slice {
|
for _, v := range slice {
|
||||||
val := iteratee(v)
|
val := iteratee(v)
|
||||||
result = append(result, val)
|
result = append(result, val)
|
||||||
@@ -642,23 +601,53 @@ func UniqueBy[T comparable](slice []T, iteratee func(item T) T) []T {
|
|||||||
return Unique(result)
|
return Unique(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Union creates a slice of unique values, in order, from all given slices. using == for equality comparisons.
|
// Union creates a slice of unique elements, in order, from all given slices.
|
||||||
func Union[T comparable](slices ...[]T) []T {
|
func Union[T comparable](slices ...[]T) []T {
|
||||||
if len(slices) == 0 {
|
result := []T{}
|
||||||
return []T{}
|
contain := map[T]struct{}{}
|
||||||
}
|
|
||||||
|
|
||||||
// append all slices, then unique it
|
|
||||||
var allElements []T
|
|
||||||
|
|
||||||
for _, slice := range slices {
|
for _, slice := range slices {
|
||||||
allElements = append(allElements, slice...)
|
for _, item := range slice {
|
||||||
|
if _, ok := contain[item]; !ok {
|
||||||
|
contain[item] = struct{}{}
|
||||||
|
result = append(result, item)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Unique(allElements)
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Intersection creates a slice of unique values that included by all slices.
|
// UnionBy is like Union, what's more it accepts iteratee which is invoked for each element of each slice
|
||||||
|
func UnionBy[T any, V comparable](predicate func(item T) V, slices ...[]T) []T {
|
||||||
|
result := []T{}
|
||||||
|
contain := map[V]struct{}{}
|
||||||
|
|
||||||
|
for _, slice := range slices {
|
||||||
|
for _, item := range slice {
|
||||||
|
val := predicate(item)
|
||||||
|
if _, ok := contain[val]; !ok {
|
||||||
|
contain[val] = struct{}{}
|
||||||
|
result = append(result, item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// Merge all given slices into one slice
|
||||||
|
func Merge[T any](slices ...[]T) []T {
|
||||||
|
result := make([]T, 0)
|
||||||
|
|
||||||
|
for _, v := range slices {
|
||||||
|
result = append(result, v...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// Intersection creates a slice of unique elements that included by all slices.
|
||||||
func Intersection[T comparable](slices ...[]T) []T {
|
func Intersection[T comparable](slices ...[]T) []T {
|
||||||
if len(slices) == 0 {
|
if len(slices) == 0 {
|
||||||
return []T{}
|
return []T{}
|
||||||
@@ -667,12 +656,10 @@ func Intersection[T comparable](slices ...[]T) []T {
|
|||||||
return Unique(slices[0])
|
return Unique(slices[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
var result []T
|
|
||||||
|
|
||||||
reducer := func(sliceA, sliceB []T) []T {
|
reducer := func(sliceA, sliceB []T) []T {
|
||||||
hashMap := make(map[T]int)
|
hashMap := make(map[T]int)
|
||||||
for _, val := range sliceA {
|
for _, v := range sliceA {
|
||||||
hashMap[val] = 1
|
hashMap[v] = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
out := make([]T, 0)
|
out := make([]T, 0)
|
||||||
@@ -685,7 +672,7 @@ func Intersection[T comparable](slices ...[]T) []T {
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
result = reducer(slices[0], slices[1])
|
result := reducer(slices[0], slices[1])
|
||||||
|
|
||||||
reduceSlice := make([][]T, 2)
|
reduceSlice := make([][]T, 2)
|
||||||
for i := 2; i < len(slices); i++ {
|
for i := 2; i < len(slices); i++ {
|
||||||
@@ -740,9 +727,26 @@ func Shuffle[T any](slice []T) []T {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sort sorts a slice of any ordered type(number or string), use quick sort algrithm.
|
||||||
|
// default sort order is ascending (asc), if want descending order, set param `sortOrder` to `desc`
|
||||||
|
func Sort[T lancetconstraints.Ordered](slice []T, sortOrder ...string) {
|
||||||
|
if len(sortOrder) > 0 && sortOrder[0] == "desc" {
|
||||||
|
quickSort(slice, 0, len(slice)-1, "desc")
|
||||||
|
} else {
|
||||||
|
quickSort(slice, 0, len(slice)-1, "asc")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SortBy sorts the slice in ascending order as determined by the less function.
|
||||||
|
// This sort is not guaranteed to be stable
|
||||||
|
func SortBy[T any](slice []T, less func(a, b T) bool) {
|
||||||
|
quickSortBy(slice, 0, len(slice)-1, less)
|
||||||
|
}
|
||||||
|
|
||||||
// SortByField return sorted slice by field
|
// SortByField return sorted slice by field
|
||||||
// Slice element should be struct, field type should be int, uint, string, or bool
|
// slice element should be struct, field type should be int, uint, string, or bool
|
||||||
// default sortType is ascending (asc), if descending order, set sortType to desc
|
// default sortType is ascending (asc), if descending order, set sortType to desc
|
||||||
|
// This function is deprecated, use Sort and SortBy for replacement
|
||||||
func SortByField(slice any, field string, sortType ...string) error {
|
func SortByField(slice any, field string, sortType ...string) error {
|
||||||
sv := sliceValue(slice)
|
sv := sliceValue(slice)
|
||||||
t := sv.Type().Elem()
|
t := sv.Type().Elem()
|
||||||
@@ -812,27 +816,26 @@ func SortByField(slice any, field string, sortType ...string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Without creates a slice excluding all given values
|
// Without creates a slice excluding all given items
|
||||||
func Without[T comparable](slice []T, values ...T) []T {
|
func Without[T comparable](slice []T, items ...T) []T {
|
||||||
if len(values) == 0 || len(slice) == 0 {
|
if len(items) == 0 || len(slice) == 0 {
|
||||||
return slice
|
return slice
|
||||||
}
|
}
|
||||||
|
|
||||||
out := make([]T, 0, len(slice))
|
result := make([]T, 0, len(slice))
|
||||||
for _, v := range slice {
|
for _, v := range slice {
|
||||||
if !Contain(values, v) {
|
if !Contain(items, v) {
|
||||||
out = append(out, v)
|
result = append(result, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return out
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// IndexOf returns the index at which the first occurrence of a value is found in a slice or return -1
|
// IndexOf returns the index at which the first occurrence of a item is found in a slice or return -1 if the item cannot be found.
|
||||||
// if the value cannot be found.
|
func IndexOf[T comparable](slice []T, item T) int {
|
||||||
func IndexOf[T comparable](slice []T, value T) int {
|
|
||||||
for i, v := range slice {
|
for i, v := range slice {
|
||||||
if v == value {
|
if v == item {
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -840,11 +843,10 @@ func IndexOf[T comparable](slice []T, value T) int {
|
|||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
// LastIndexOf returns the index at which the last occurrence of a value is found in a slice or return -1
|
// LastIndexOf returns the index at which the last occurrence of the item is found in a slice or return -1 if the then cannot be found.
|
||||||
// if the value cannot be found.
|
func LastIndexOf[T comparable](slice []T, item T) int {
|
||||||
func LastIndexOf[T comparable](slice []T, value T) int {
|
|
||||||
for i := len(slice) - 1; i > 0; i-- {
|
for i := len(slice) - 1; i > 0; i-- {
|
||||||
if value == slice[i] {
|
if item == slice[i] {
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -853,25 +855,39 @@ func LastIndexOf[T comparable](slice []T, value T) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ToSlicePointer returns a pointer to the slices of a variable parameter transformation
|
// ToSlicePointer returns a pointer to the slices of a variable parameter transformation
|
||||||
func ToSlicePointer[T any](value ...T) []*T {
|
func ToSlicePointer[T any](items ...T) []*T {
|
||||||
out := make([]*T, len(value))
|
result := make([]*T, len(items))
|
||||||
for i := range value {
|
for i := range items {
|
||||||
out[i] = &value[i]
|
result[i] = &items[i]
|
||||||
}
|
}
|
||||||
return out
|
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToSlice returns a slices of a variable parameter transformation
|
// ToSlice returns a slices of a variable parameter transformation
|
||||||
func ToSlice[T any](value ...T) []T {
|
func ToSlice[T any](items ...T) []T {
|
||||||
out := make([]T, len(value))
|
result := make([]T, len(items))
|
||||||
copy(out, value)
|
copy(result, items)
|
||||||
return out
|
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// AppendIfAbsent only absent append the value
|
// AppendIfAbsent only absent append the item
|
||||||
func AppendIfAbsent[T comparable](slice []T, value T) []T {
|
func AppendIfAbsent[T comparable](slice []T, item T) []T {
|
||||||
if !Contain(slice, value) {
|
if !Contain(slice, item) {
|
||||||
slice = append(slice, value)
|
slice = append(slice, item)
|
||||||
}
|
}
|
||||||
return slice
|
return slice
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// KeyBy converts a slice to a map based on a callback function
|
||||||
|
func KeyBy[T any, U comparable](slice []T, iteratee func(item T) U) map[U]T {
|
||||||
|
result := make(map[U]T, len(slice))
|
||||||
|
|
||||||
|
for _, v := range slice {
|
||||||
|
k := iteratee(v)
|
||||||
|
result[k] = v
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ package slice
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
|
"github.com/duke-git/lancet/v2/lancetconstraints"
|
||||||
)
|
)
|
||||||
|
|
||||||
// sliceValue return the reflect value of a slice
|
// sliceValue return the reflect value of a slice
|
||||||
@@ -24,3 +26,66 @@ func sliceElemType(reflectType reflect.Type) reflect.Type {
|
|||||||
reflectType = reflectType.Elem()
|
reflectType = reflectType.Elem()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func quickSort[T lancetconstraints.Ordered](slice []T, lowIndex, highIndex int, order string) {
|
||||||
|
if lowIndex < highIndex {
|
||||||
|
p := partitionOrderedSlice(slice, lowIndex, highIndex, order)
|
||||||
|
quickSort(slice, lowIndex, p-1, order)
|
||||||
|
quickSort(slice, p+1, highIndex, order)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// partitionOrderedSlice split ordered slice into two parts for quick sort
|
||||||
|
func partitionOrderedSlice[T lancetconstraints.Ordered](slice []T, lowIndex, highIndex int, order string) int {
|
||||||
|
p := slice[highIndex]
|
||||||
|
i := lowIndex
|
||||||
|
|
||||||
|
for j := lowIndex; j < highIndex; j++ {
|
||||||
|
if order == "desc" {
|
||||||
|
if slice[j] > p {
|
||||||
|
swap(slice, i, j)
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if slice[j] < p {
|
||||||
|
swap(slice, i, j)
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
swap(slice, i, highIndex)
|
||||||
|
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
|
||||||
|
func quickSortBy[T any](slice []T, lowIndex, highIndex int, less func(a, b T) bool) {
|
||||||
|
if lowIndex < highIndex {
|
||||||
|
p := partitionAnySlice(slice, lowIndex, highIndex, less)
|
||||||
|
quickSortBy(slice, lowIndex, p-1, less)
|
||||||
|
quickSortBy(slice, p+1, highIndex, less)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// partitionAnySlice split any slice into two parts for quick sort
|
||||||
|
func partitionAnySlice[T any](slice []T, lowIndex, highIndex int, less func(a, b T) bool) int {
|
||||||
|
p := slice[highIndex]
|
||||||
|
i := lowIndex
|
||||||
|
|
||||||
|
for j := lowIndex; j < highIndex; j++ {
|
||||||
|
|
||||||
|
if less(slice[j], p) {
|
||||||
|
swap(slice, i, j)
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
swap(slice, i, highIndex)
|
||||||
|
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
|
||||||
|
// swap two slice value at index i and j
|
||||||
|
func swap[T any](slice []T, i, j int) {
|
||||||
|
slice[i], slice[j] = slice[j], slice[i]
|
||||||
|
}
|
||||||
|
|||||||
@@ -31,6 +31,10 @@ func TestChunk(t *testing.T) {
|
|||||||
|
|
||||||
arr := []string{"a", "b", "c", "d", "e"}
|
arr := []string{"a", "b", "c", "d", "e"}
|
||||||
|
|
||||||
|
assert.Equal([][]string{}, Chunk(arr, -1))
|
||||||
|
|
||||||
|
assert.Equal([][]string{}, Chunk(arr, 0))
|
||||||
|
|
||||||
r1 := [][]string{{"a"}, {"b"}, {"c"}, {"d"}, {"e"}}
|
r1 := [][]string{{"a"}, {"b"}, {"c"}, {"d"}, {"e"}}
|
||||||
assert.Equal(r1, Chunk(arr, 1))
|
assert.Equal(r1, Chunk(arr, 1))
|
||||||
|
|
||||||
@@ -43,8 +47,11 @@ func TestChunk(t *testing.T) {
|
|||||||
r4 := [][]string{{"a", "b", "c", "d"}, {"e"}}
|
r4 := [][]string{{"a", "b", "c", "d"}, {"e"}}
|
||||||
assert.Equal(r4, Chunk(arr, 4))
|
assert.Equal(r4, Chunk(arr, 4))
|
||||||
|
|
||||||
r5 := [][]string{{"a"}, {"b"}, {"c"}, {"d"}, {"e"}}
|
r5 := [][]string{{"a", "b", "c", "d", "e"}}
|
||||||
assert.Equal(r5, Chunk(arr, 5))
|
assert.Equal(r5, Chunk(arr, 5))
|
||||||
|
|
||||||
|
r6 := [][]string{{"a", "b", "c", "d", "e"}}
|
||||||
|
assert.Equal(r6, Chunk(arr, 6))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCompact(t *testing.T) {
|
func TestCompact(t *testing.T) {
|
||||||
@@ -186,13 +193,22 @@ func TestGroupWith(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCount(t *testing.T) {
|
func TestCount(t *testing.T) {
|
||||||
|
numbers := []int{1, 2, 3, 3, 5, 6}
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestCountBy")
|
||||||
|
|
||||||
|
assert.Equal(1, Count(numbers, 1))
|
||||||
|
assert.Equal(2, Count(numbers, 3))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCountBy(t *testing.T) {
|
||||||
nums := []int{1, 2, 3, 4, 5, 6}
|
nums := []int{1, 2, 3, 4, 5, 6}
|
||||||
evenFunc := func(i, num int) bool {
|
evenFunc := func(i, num int) bool {
|
||||||
return (num % 2) == 0
|
return (num % 2) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
assert := internal.NewAssert(t, "TestCount")
|
assert := internal.NewAssert(t, "TestCountBy")
|
||||||
assert.Equal(3, Count(nums, evenFunc))
|
assert.Equal(3, CountBy(nums, evenFunc))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFind(t *testing.T) {
|
func TestFind(t *testing.T) {
|
||||||
@@ -428,6 +444,29 @@ func TestUnion(t *testing.T) {
|
|||||||
assert.Equal([]int{1, 3, 4, 6}, Union(s1))
|
assert.Equal([]int{1, 3, 4, 6}, Union(s1))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUnionBy(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestUnionBy")
|
||||||
|
|
||||||
|
testFunc := func(i int) int {
|
||||||
|
return i / 2
|
||||||
|
}
|
||||||
|
|
||||||
|
result := UnionBy(testFunc, []int{0, 1, 2, 3, 4, 5}, []int{0, 2, 10})
|
||||||
|
assert.Equal(result, []int{0, 2, 4, 10})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMerge(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestMerge")
|
||||||
|
|
||||||
|
s1 := []int{1, 2, 3, 4}
|
||||||
|
s2 := []int{2, 3, 4, 5}
|
||||||
|
s3 := []int{4, 5, 6}
|
||||||
|
|
||||||
|
assert.Equal([]int{1, 2, 3, 4, 2, 3, 4, 5, 4, 5, 6}, Merge(s1, s2, s3))
|
||||||
|
assert.Equal([]int{1, 2, 3, 4, 2, 3, 4, 5}, Merge(s1, s2))
|
||||||
|
assert.Equal([]int{2, 3, 4, 5, 4, 5, 6}, Merge(s2, s3))
|
||||||
|
}
|
||||||
|
|
||||||
func TestIntersection(t *testing.T) {
|
func TestIntersection(t *testing.T) {
|
||||||
s1 := []int{1, 2, 2, 3}
|
s1 := []int{1, 2, 2, 3}
|
||||||
s2 := []int{1, 2, 3, 4}
|
s2 := []int{1, 2, 3, 4}
|
||||||
@@ -508,6 +547,56 @@ func TestDifferenceBy(t *testing.T) {
|
|||||||
assert.Equal([]int{1, 2}, DifferenceBy(s1, s2, addOne))
|
assert.Equal([]int{1, 2}, DifferenceBy(s1, s2, addOne))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSort(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestSort")
|
||||||
|
|
||||||
|
numbers := []int{1, 4, 3, 2, 5}
|
||||||
|
|
||||||
|
Sort(numbers)
|
||||||
|
assert.Equal([]int{1, 2, 3, 4, 5}, numbers)
|
||||||
|
|
||||||
|
Sort(numbers, "desc")
|
||||||
|
assert.Equal([]int{5, 4, 3, 2, 1}, numbers)
|
||||||
|
|
||||||
|
strings := []string{"a", "d", "c", "b", "e"}
|
||||||
|
|
||||||
|
Sort(strings)
|
||||||
|
assert.Equal([]string{"a", "b", "c", "d", "e"}, strings)
|
||||||
|
|
||||||
|
Sort(strings, "desc")
|
||||||
|
assert.Equal([]string{"e", "d", "c", "b", "a"}, strings)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSortBy(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestSortBy")
|
||||||
|
|
||||||
|
numbers := []int{1, 4, 3, 2, 5}
|
||||||
|
|
||||||
|
SortBy(numbers, func(a, b int) bool {
|
||||||
|
return a < b
|
||||||
|
})
|
||||||
|
assert.Equal([]int{1, 2, 3, 4, 5}, numbers)
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
Name string
|
||||||
|
Age uint
|
||||||
|
}
|
||||||
|
|
||||||
|
users := []User{
|
||||||
|
{Name: "a", Age: 21},
|
||||||
|
{Name: "b", Age: 15},
|
||||||
|
{Name: "c", Age: 100}}
|
||||||
|
|
||||||
|
SortBy(users, func(a, b User) bool {
|
||||||
|
return a.Age < b.Age
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Logf("sort users by age: %v", users)
|
||||||
|
|
||||||
|
// output
|
||||||
|
// [{b 15} {a 21} {c 100}]
|
||||||
|
}
|
||||||
|
|
||||||
func TestSortByFielDesc(t *testing.T) {
|
func TestSortByFielDesc(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestSortByFielDesc")
|
assert := internal.NewAssert(t, "TestSortByFielDesc")
|
||||||
|
|
||||||
@@ -644,3 +733,21 @@ func TestReplaceAll(t *testing.T) {
|
|||||||
assert.Equal([]string{"x", "b", "x", "c", "d", "x"}, ReplaceAll(strs, "a", "x"))
|
assert.Equal([]string{"x", "b", "x", "c", "d", "x"}, ReplaceAll(strs, "a", "x"))
|
||||||
assert.Equal([]string{"a", "b", "a", "c", "d", "a"}, ReplaceAll(strs, "e", "x"))
|
assert.Equal([]string{"a", "b", "a", "c", "d", "a"}, ReplaceAll(strs, "e", "x"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestKeyBy(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestKeyBy")
|
||||||
|
|
||||||
|
result := KeyBy([]string{"a", "ab", "abc"}, func(str string) int {
|
||||||
|
return len(str)
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.Equal(result, map[int]string{1: "a", 2: "ab", 3: "abc"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRepeat(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestRepeat")
|
||||||
|
|
||||||
|
result := Repeat("a", 6)
|
||||||
|
|
||||||
|
assert.Equal(result, []string{"a", "a", "a", "a", "a", "a"})
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user