diff --git a/CONTRIBUTING.en-US.md b/CONTRIBUTING.md similarity index 100% rename from CONTRIBUTING.en-US.md rename to CONTRIBUTING.md diff --git a/README.md b/README.md index 283a0377..02332e6d 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@
![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf) -[![Release](https://img.shields.io/badge/release-2.3.0-green.svg)](https://github.com/duke-git/lancet/releases) +[![Release](https://img.shields.io/badge/release-2.3.1-green.svg)](https://github.com/duke-git/lancet/releases) [![GoDoc](https://godoc.org/github.com/duke-git/lancet/v2?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet/v2) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet/v2)](https://goreportcard.com/report/github.com/duke-git/lancet/v2) [![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml) @@ -660,9 +660,12 @@ import "github.com/duke-git/lancet/v2/fileutil" - **CreateDir** : create directory in absolute path. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/fileutil.md#CreateDir)] [[play](https://go.dev/play/p/qUuCe1OGQnM)] -- **CopyFile** :copy src file to dest file. +- **CopyFile** : copy src file to dest file. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/fileutil.md#CopyFile)] [[play](https://go.dev/play/p/Jg9AMJMLrJi)] +- **CopyDir** : copy src directory to dest directory. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/fileutil.md#CopyDir)] + [[play](https://go.dev/play/p/YAyFTA_UuPb)] - **FileMode** : return file's mode and permission. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/fileutil.md#FileMode)] [[play](https://go.dev/play/p/2l2hI42fA3p)] @@ -731,8 +734,10 @@ import "github.com/duke-git/lancet/v2/fileutil" [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/fileutil.md#ReadFile)] - **ChunkRead** : reads a block from the file at the specified offset and returns all lines within the block. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/fileutil.md#ChunkRead)] + [[play](https://go.dev/play/p/r0hPmKWhsgf)] - **ParallelChunkRead** : reads the file in parallel and send each chunk of lines to the specified channel. - [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/fileutil.md#ParallelChunkRead)] + [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/fileutil.md#ParallelChunkRead)] + [[play](https://go.dev/play/p/teMXnCsdSEw)]

10. Formatter contains some functions for data formatting.        index

@@ -795,20 +800,28 @@ import "github.com/duke-git/lancet/v2/function" [[play](https://go.dev/play/p/hbON-Xeyn5N)] - **Pipeline** : takes a list of functions and returns a function whose param will be passed into the functions one by one. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#Pipeline)] + [[play](https://go.dev/play/p/mPdUVvj6HD6)] - **AcceptIf** : returns another function of the same signature as the apply function but also includes a bool value to indicate success or failure. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#AcceptIf)] + [[play](https://go.dev/play/p/XlXHHtzCf7d)] - **And** : returns a composed predicate that represents the logical AND of a list of predicates. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#And)] + [[play](https://go.dev/play/p/dTBHJMQ0zD2)] - **Or** : returns a composed predicate that represents the logical OR of a list of predicates. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#Or)] + [[play](https://go.dev/play/p/LitCIsDFNDA)] - **Negate** : returns a predicate that represents the logical negation of this predicate. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#Negate)] + [[play](https://go.dev/play/p/jbI8BtgFnVE)] - **Nor** : returns a composed predicate that represents the logical NOR of a list of predicates. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#Nor)] + [[play](https://go.dev/play/p/2KdCoBEOq84)] - **Nand** : returns a composed predicate that represents the logical Nand of a list of predicates. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#Nand)] + [[play](https://go.dev/play/p/Rb-FdNGpgSO)] - **Xnor** : returns a composed predicate that represents the logical XNOR of a list of predicates. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#Xnor)] + [[play](https://go.dev/play/p/FJxko8SFbqc)] - **Watcher** : Watcher is used for record code execution time. can start/stop/reset the watch timer. get the elapsed time of function execution. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#Watcher)] [[play](https://go.dev/play/p/l2yrOpCLd1I)] @@ -888,6 +901,10 @@ import "github.com/duke-git/lancet/v2/maputil" - **HasKey** : checks if map has key or not. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/maputil.md#HasKey)] [[play](https://go.dev/play/p/isZZHOsDhFc)] +- **ToSortedSlicesDefault** : converts a map to two slices sorted by key: one for the keys and another for the values. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/maputil.md#ToSortedSlicesDefault)] +- **ToSortedSlicesWithComparator** : converts a map to two slices sorted by key and using a custom comparison function: one for the keys and another for the values. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/maputil.md#ToSortedSlicesWithComparator)] - **NewConcurrentMap** : creates a ConcurrentMap with specific shard count. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/maputil.md#NewConcurrentMap)] [[play](https://go.dev/play/p/3PenTPETJT0)] @@ -959,12 +976,16 @@ import "github.com/duke-git/lancet/v2/mathutil" [[play](https://go.dev/play/p/aumarSHIGzP)] - **CeilToFloat** : round float up n decimal places. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/mathutil.md#CeilToFloat)] + [[play](https://go.dev/play/p/8hOeSADZPCo)] - **CeilToString** : round float up n decimal places, then conver to string. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/mathutil.md#CeilToString)] + [[play](https://go.dev/play/p/wy5bYEyUKKG)] - **FloorToFloat** : round float down n decimal places. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/mathutil.md#FloorToFloat)] + [[play](https://go.dev/play/p/vbCBrQHZEED)] - **FloorToString** : round float down n decimal places, then conver to string. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/mathutil.md#FloorToString)] + [[play](https://go.dev/play/p/Qk9KPd2IdDb)] - **Range** : Creates a slice of numbers from start with specified count, element step is 1. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/mathutil.md#Range)] [[play](https://go.dev/play/p/9ke2opxa8ZP)] @@ -1006,6 +1027,7 @@ import "github.com/duke-git/lancet/v2/mathutil" [[play](https://go.dev/play/p/fsyBh1Os-1d)] - **Div** : returns the result of x divided by y. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/mathutil.md#Div)] + [[play](https://go.dev/play/p/WLxDdGXXYat)]

14. Netutil package contains functions to get net information and send http request.        index

@@ -1413,6 +1435,14 @@ import "github.com/duke-git/lancet/v2/slice" [[play](https://go.dev/play/p/UzpGQptWppw)] - **SetToDefaultIf** : set elements to their default value if they match the given predicate. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#SetToDefaultIf)] + [[play](https://go.dev/play/p/9AXGlPRC0-A)] +- **Break** : breaks a list into two parts at the point where the predicate for the first time is true. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#Break)] +- **RightPadding** : adds padding to the right end of a slice. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#RightPadding)] +- **LeftPadding** : adds padding to the left begin of a slice. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#LeftPadding)] +

19. Stream package implements a sequence of elements supporting sequential and operations. this package is an experiment to explore if stream in go can work as the way java does. its function is very limited.        index

@@ -1660,8 +1690,11 @@ import "github.com/duke-git/lancet/v2/strutil" [[play](https://go.dev/play/p/HzLC9vsTwkf)] - **SubInBetween** : return substring between the start and end position(excluded) of source string. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/strutil.md#SubInBetween)] + [[play](https://go.dev/play/p/EDbaRvjeNsv)] - **HammingDistance** : calculates the Hamming distance between two strings. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/strutil.md#HammingDistance)] + [[play](https://go.dev/play/p/glNdQEA9HUi)] +

22. System package contain some functions about os, runtime, shell command.        index

@@ -2001,7 +2034,7 @@ import "github.com/duke-git/lancet/v2/xerror" ## How to Contribute -#### [Contributing Guide](./CONTRIBUTING.en-US.md) +#### [Contributing Guide](./CONTRIBUTING.md) ## Contributors Thank you to all the people who contributed to lancet! diff --git a/README_zh-CN.md b/README_zh-CN.md index 90a7f2a6..384c8977 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -4,7 +4,7 @@
![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf) -[![Release](https://img.shields.io/badge/release-2.3.0-green.svg)](https://github.com/duke-git/lancet/releases) +[![Release](https://img.shields.io/badge/release-2.3.1-green.svg)](https://github.com/duke-git/lancet/releases) [![GoDoc](https://godoc.org/github.com/duke-git/lancet/v2?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet/v2) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet/v2)](https://goreportcard.com/report/github.com/duke-git/lancet/v2) [![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml) @@ -659,9 +659,12 @@ import "github.com/duke-git/lancet/v2/fileutil" - **CreateDir** : 创建嵌套目录,例如/a/, /a/b/。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/fileutil.md#CreateDir)] [[play](https://go.dev/play/p/qUuCe1OGQnM)] -- **CopyFile** :拷贝文件,会覆盖原有的文件。 +- **CopyFile** : 拷贝文件,会覆盖原有的文件。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/fileutil.md#CopyFile)] [[play](https://go.dev/play/p/Jg9AMJMLrJi)] +- **CopyDir** : 拷贝目录。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/fileutil.md#CopyDir)] + [[play](https://go.dev/play/p/YAyFTA_UuPb)] - **FileMode** : 获取文件 mode 信息。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/fileutil.md#FileMode)] [[play](https://go.dev/play/p/2l2hI42fA3p)] @@ -730,8 +733,10 @@ import "github.com/duke-git/lancet/v2/fileutil" [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/fileutil.md#ReadFile)] - **ChunkRead** : 从文件的指定偏移读取块并返回块内所有行。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/fileutil.md#ChunkRead)] + [[play](https://go.dev/play/p/r0hPmKWhsgf)] - **ParallelChunkRead** : 并行读取文件并将每个块的行发送到指定通道。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/fileutil.md#ParallelChunkRead)] + [[play](https://go.dev/play/p/teMXnCsdSEw)] @@ -799,22 +804,31 @@ import "github.com/duke-git/lancet/v2/function" [[play](https://go.dev/play/p/mPdUVvj6HD6)] - **AcceptIf** : AcceptIf函数会返回另一个函数,该函数的签名与apply函数相同,但同时还会包含一个布尔值来表示成功或失败。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/function.md#AcceptIf)] + [[play](https://go.dev/play/p/XlXHHtzCf7d)] - **And** : 返回一个复合谓词判断函数,该判断函数表示一组谓词的逻辑and操作。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/function.md#And)] + [[play](https://go.dev/play/p/dTBHJMQ0zD2)] - **Or** : 返回一个复合谓词判断函数,该判断函数表示一组谓词的逻辑or操作。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/function.md#Or)] + [[play](https://go.dev/play/p/LitCIsDFNDA)] - **Negate** : 返回一个谓词函数,该谓词函数表示当前谓词的逻辑否定。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/function.md#Negate)] + [[play](https://go.dev/play/p/jbI8BtgFnVE)] - **Nor** : 返回一个复合谓词判断函数,该判断函数表示一组谓词的逻辑非或nor的操作。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/function.md#Nor)] + [[play](https://go.dev/play/p/2KdCoBEOq84)] - **Nand** : 返回一个复合谓词判断函数,该判断函数表示一组谓词的逻辑非与nand的操作。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/function.md#Nand)] + [[play](https://go.dev/play/p/Rb-FdNGpgSO)] - **Xnor** : 返回一个复合谓词判断函数,该判断函数表示一组谓词的逻辑异或xnor的操作。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/function.md#Xnor)] + [[play](https://go.dev/play/p/FJxko8SFbqc)] - **Watcher** : Watcher 用于记录代码执行时间。可以启动/停止/重置手表定时器。获取函数执行的时间。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/function.md#Watcher)] [[play](https://go.dev/play/p/l2yrOpCLd1I)] + +

12. maputil 包括一些操作 map 的函数。       回到目录

```go @@ -889,6 +903,10 @@ import "github.com/duke-git/lancet/v2/maputil" - **HasKey** : 检查 map 是否包含某个 key。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/maputil.md#HasKey)] [[play](https://go.dev/play/p/isZZHOsDhFc)] +- **ToSortedSlicesDefault** : 将map的key和value转化成两个根据key的值从小到大排序的切片,value切片中元素的位置与key对应。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/maputil.md#ToSortedSlicesDefault)] +- **ToSortedSlicesWithComparator** : 将map的key和value转化成两个使用比较器函数根据key的值自定义排序规则的切片,value切片中元素的位置与key对应。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/maputil.md#ToSortedSlicesWithComparator)] - **NewConcurrentMap** : ConcurrentMap 协程安全的 map 结构。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/maputil.md#NewConcurrentMap)] [[play](https://go.dev/play/p/3PenTPETJT0)] @@ -960,12 +978,16 @@ import "github.com/duke-git/lancet/v2/mathutil" [[play](https://go.dev/play/p/aumarSHIGzP)] - **CeilToFloat** : 向上舍入(进一法),保留n位小数。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/mathutil.md#CeilToFloat)] + [[play](https://go.dev/play/p/8hOeSADZPCo)] - **CeilToString** : 向上舍入(进一法),保留n位小数,返回字符串。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/mathutil.md#CeilToString)] + [[play](https://go.dev/play/p/wy5bYEyUKKG)] - **FloorToFloat** : 向下舍入(去尾法),保留n位小数。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/mathutil.md#FloorToFloat)] + [[play](https://go.dev/play/p/vbCBrQHZEED)] - **FloorToString** : 向下舍入(去尾法),保留n位小数,返回字符串。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/mathutil.md#FloorToString)] + [[play](https://go.dev/play/p/Qk9KPd2IdDb)] - **Range** : 根据指定的起始值和数量,创建一个数字切片。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/mathutil.md#Range)] [[play](https://go.dev/play/p/9ke2opxa8ZP)] @@ -1007,7 +1029,7 @@ import "github.com/duke-git/lancet/v2/mathutil" [[play](https://go.dev/play/p/fsyBh1Os-1d)] - **Div** : 除法运算。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/mathutil.md#Div)] - + [[play](https://go.dev/play/p/WLxDdGXXYat)]

14. netutil 网络包支持获取 ip 地址,发送 http 请求。       回到目录

@@ -1412,7 +1434,13 @@ import "github.com/duke-git/lancet/v2/slice" [[play](https://go.dev/play/p/UzpGQptWppw)] - **SetToDefaultIf** : 根据给定给定的predicate判定函数来修改切片中的元素。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#SetToDefaultIf)] - + [[play](https://go.dev/play/p/9AXGlPRC0-A)] +- **Break** : 根据判断函数将切片分成两部分。它开始附加到与函数匹配的第一个元素之后的第二个切片。第一个匹配之后的所有元素都包含在第二个切片中,无论它们是否与函数匹配。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#Break)] +- **RightPadding** : 在切片的右部添加元素。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#RightPadding)] +- **LeftPadding** : 在切片的左部添加元素。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#LeftPadding)]

19. stream 流,该包仅验证简单的 stream 实现,功能有限。       回到目录

@@ -1502,6 +1530,7 @@ import "github.com/duke-git/lancet/v2/stream" [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/stream.md#ToSlice)] [[play](https://go.dev/play/p/jI6_iZZuVFE)] +

20. structs 提供操作 struct, tag, field 的相关函数。       回到目录

```go @@ -1663,8 +1692,11 @@ import "github.com/duke-git/lancet/v2/strutil" [[play](https://go.dev/play/p/HzLC9vsTwkf)] - **SubInBetween** : 获取字符串中指定的起始字符串start和终止字符串end直接的子字符串。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/strutil.md#SubInBetween)] + [[play](https://go.dev/play/p/EDbaRvjeNsv)] - **HammingDistance** : 计算两个字符串之间的汉明距离。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/strutil.md#HammingDistance)] + [[play](https://go.dev/play/p/glNdQEA9HUi)] +

22. system 包含 os, runtime, shell command 的相关函数。       回到目录

diff --git a/convertor/convertor.go b/convertor/convertor.go index 1dcca55c..423df3bc 100644 --- a/convertor/convertor.go +++ b/convertor/convertor.go @@ -397,6 +397,7 @@ func GbkToUtf8(bs []byte) ([]byte, error) { } // ToStdBase64 convert data to standard base64 encoding. +// Play: https://go.dev/play/p/_fLJqJD3NMo func ToStdBase64(value any) string { if value == nil || (reflect.ValueOf(value).Kind() == reflect.Ptr && reflect.ValueOf(value).IsNil()) { return "" @@ -418,6 +419,7 @@ func ToStdBase64(value any) string { } // ToUrlBase64 convert data to URL base64 encoding. +// Play: https://go.dev/play/p/C_d0GlvEeUR func ToUrlBase64(value any) string { if value == nil || (reflect.ValueOf(value).Kind() == reflect.Ptr && reflect.ValueOf(value).IsNil()) { return "" @@ -439,6 +441,7 @@ func ToUrlBase64(value any) string { } // ToRawStdBase64 convert data to raw standard base64 encoding. +// Play: https://go.dev/play/p/wSAr3sfkDcv func ToRawStdBase64(value any) string { if value == nil || (reflect.ValueOf(value).Kind() == reflect.Ptr && reflect.ValueOf(value).IsNil()) { return "" @@ -460,6 +463,7 @@ func ToRawStdBase64(value any) string { } // ToRawUrlBase64 convert data to raw URL base64 encoding. +// Play: https://go.dev/play/p/HwdDPFcza1O func ToRawUrlBase64(value any) string { if value == nil || (reflect.ValueOf(value).Kind() == reflect.Ptr && reflect.ValueOf(value).IsNil()) { return "" diff --git a/datastructure/set/set.go b/datastructure/set/set.go index bfe835e1..3608a14a 100644 --- a/datastructure/set/set.go +++ b/datastructure/set/set.go @@ -187,10 +187,11 @@ func (s Set[T]) EachWithBreak(iteratee func(item T) bool) { // Pop delete the top element of set then return it, if set is empty, return nil-value of T and false. func (s Set[T]) Pop() (v T, ok bool) { if len(s) > 0 { - items := s.Values() - item := items[len(s)-1] - delete(s, item) - return item, true + for item := range s { + v = item + delete(s, item) + return v, true + } } return v, false diff --git a/datastructure/set/set_test.go b/datastructure/set/set_test.go index 261e1dcc..91432376 100644 --- a/datastructure/set/set_test.go +++ b/datastructure/set/set_test.go @@ -243,25 +243,34 @@ func TestEachWithBreak(t *testing.T) { // assert.Equal(6, sum) } -// func TestPop(t *testing.T) { -// assert := internal.NewAssert(t, "TestPop") +func TestPop(t *testing.T) { + t.Parallel() + assert := internal.NewAssert(t, "TestSet_Pop") + + s := New[int]() + + val, ok := s.Pop() + assert.Equal(0, val) + assert.Equal(false, ok) -// s := New[int]() + s = New(1, 2, 3, 4, 5) + sl := s.ToSlice() -// val, ok := s.Pop() -// assert.Equal(0, val) -// assert.Equal(false, ok) + val, ok = s.Pop() + assert.Equal(false, s.Contain(val)) + assert.Equal(true, ok) + assert.Equal(len(sl)-1, s.Size()) -// s.Add(1) -// s.Add(2) -// s.Add(3) + var found bool -// // s = New(1, 2, 3, 4, 5) + for _, v := range sl { + if v == val { + found = true + } + } -// val, ok = s.Pop() -// assert.Equal(3, val) -// assert.Equal(true, ok) -// } + assert.Equal(true, found) +} func TestSet_ToSlice(t *testing.T) { t.Parallel() diff --git a/docs/api/packages/convertor.md b/docs/api/packages/convertor.md index 841c3b7f..f32a0808 100644 --- a/docs/api/packages/convertor.md +++ b/docs/api/packages/convertor.md @@ -891,7 +891,7 @@ func main() { func ToStdBase64(value any) string ``` -示例: +示例:[运行](https://go.dev/play/p/_fLJqJD3NMo) ```go package main @@ -963,7 +963,7 @@ func main() { func ToUrlBase64(value any) string ``` -示例: +示例:[运行](https://go.dev/play/p/C_d0GlvEeUR) ```go package main @@ -1032,7 +1032,7 @@ func main() { func ToRawStdBase64(value any) string ``` -示例: +示例:[运行](https://go.dev/play/p/wSAr3sfkDcv) ```go package main @@ -1064,7 +1064,7 @@ func main() { fmt.Println(afterEncode) floatVal := 123.456 - afterEncode = convertor.ToRawStdBase64(floatVal) + afterEncode := convertor.ToRawStdBase64(floatVal) fmt.Println(afterEncode) boolVal := true @@ -1090,7 +1090,7 @@ func main() {

值转换为 ToRawUrlBase64 编码的字符串。error 类型的数据也会把 error 的原因进行编码,复杂的结构会转为 JSON 格式的字符串

-函数签名: +函数签名:[运行](https://go.dev/play/p/HwdDPFcza1O) ```go func ToRawUrlBase64(value any) string @@ -1109,7 +1109,7 @@ import ( func main() { stringVal := "hello" - afterEncode = convertor.ToRawUrlBase64(stringVal) + afterEncode := convertor.ToRawUrlBase64(stringVal) fmt.Println(afterEncode) byteSliceVal := []byte("hello") @@ -1132,11 +1132,11 @@ func main() { fmt.Println(afterEncode) boolVal := true - afterEncode = convertor.ToRawStdBase64(boolVal) + afterEncode = convertor.ToRawUrlBase64(boolVal) fmt.Println(afterEncode) errVal := errors.New("err") - afterEncode = convertor.ToRawStdBase64(errVal) + afterEncode = convertor.ToRawUrlBase64(errVal) fmt.Println(afterEncode) // Output: diff --git a/docs/api/packages/datastructure/hashmap.md b/docs/api/packages/datastructure/hashmap.md index 2768fcda..3369852f 100644 --- a/docs/api/packages/datastructure/hashmap.md +++ b/docs/api/packages/datastructure/hashmap.md @@ -31,6 +31,7 @@ import ( - [Iterate](#Iterate) - [Keys](#Keys) - [Values](#Values) +- [FilterByValue](#FilterByValue)
@@ -276,7 +277,7 @@ func main() { ### Values -

返回hashmap所有值的切片 (随机顺序).

+

返回hashmap所有值的切片 (随机顺序)。

函数签名: @@ -306,3 +307,40 @@ func main() { ``` +### FilterByValue + +

返回一个过滤后的HashMap。 如果任何值与 perdicate 函数不匹配,则返回 nil,否则返回包含选定值的 HashMap。

+ +函数签名: + +```go +func (hm *HashMap) FilterByValue(perdicate func(value any) bool) *HashMap +``` + +示例: + +```go +package main + +import ( + "fmt" + hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap" +) + +func main() { + hm := hashmap.NewHashMap() + + hm.Put("a", 1) + hm.Put("b", 2) + hm.Put("c", 3) + hm.Put("d", 4) + hm.Put("e", 5) + hm.Put("f", 6) + + filteredHM := hm.FilterByValue(func(value any) bool { + return value.(int) == 1 || value.(int) == 3 + }) + + fmt.Println(filteredHM.Size()) //2 +} +``` diff --git a/docs/api/packages/fileutil.md b/docs/api/packages/fileutil.md index 830dfc01..129bbe06 100644 --- a/docs/api/packages/fileutil.md +++ b/docs/api/packages/fileutil.md @@ -976,7 +976,7 @@ func main() { func ChunkRead(file *os.File, offset int64, size int, bufPool *sync.Pool) ([]string, error) ``` -示例: +示例:[运行](https://go.dev/play/p/r0hPmKWhsgf) ```go package main @@ -1035,7 +1035,7 @@ func main() { func ParallelChunkRead(filePath string, linesCh chan<- []string, chunkSizeMB, maxGoroutine int) error ``` -示例: +示例:[运行](https://go.dev/play/p/teMXnCsdSEw) ```go package main diff --git a/docs/api/packages/function.md b/docs/api/packages/function.md index 0d45096e..770116d1 100644 --- a/docs/api/packages/function.md +++ b/docs/api/packages/function.md @@ -424,7 +424,7 @@ func longRunningTask() { func And[T any](predicates ...func(T) bool) func(T) bool ``` -示例: +示例:[运行](https://go.dev/play/p/dTBHJMQ0zD2) ```go package main @@ -461,7 +461,7 @@ func main() { func Or[T any](predicates ...func(T) bool) func(T) bool ``` -示例: +示例:[运行](https://go.dev/play/p/LitCIsDFNDA) ```go package main @@ -496,7 +496,7 @@ func main() { func Negate[T any](predicate func(T) bool) func(T) bool ``` -示例: +示例:[运行](https://go.dev/play/p/jbI8BtgFnVE) ```go package main @@ -536,7 +536,7 @@ func main() { func Nor[T any](predicates ...func(T) bool) func(T) bool ``` -示例: +示例:[运行](https://go.dev/play/p/2KdCoBEOq84) ```go package main @@ -578,7 +578,7 @@ func main() { func Nand[T any](predicates ...func(T) bool) func(T) bool ``` -示例: +示例:[运行](https://go.dev/play/p/Rb-FdNGpgSO) ```go package main @@ -615,7 +615,7 @@ func main() { func Xnor[T any](predicates ...func(T) bool) func(T) bool ``` -示例: +示例:[运行](https://go.dev/play/p/FJxko8SFbqc) ```go package main @@ -652,7 +652,7 @@ func main() { func AcceptIf[T any](predicate func(T) bool, apply func(T) T) func(T) (T, bool) ``` -示例: +示例:[运行](https://go.dev/play/p/XlXHHtzCf7d) ```go package main @@ -663,9 +663,8 @@ import ( ) func main() { - - adder := AcceptIf( - And( + adder := function.AcceptIf( + function.And( func(x int) bool { return x > 10 }, func(x int) bool { diff --git a/docs/api/packages/maputil.md b/docs/api/packages/maputil.md index f6da5c38..663f7612 100644 --- a/docs/api/packages/maputil.md +++ b/docs/api/packages/maputil.md @@ -44,6 +44,8 @@ import ( - [Minus](#Minus) - [IsDisjoint](#IsDisjoint) - [HasKey](#HasKey) +- [ToSortedSlicesDefault](#ToSortedSlicesDefault) +- [ToSortedSlicesWithComparator](#ToSortedSlicesWithComparator) - [NewConcurrentMap](#NewConcurrentMap) - [ConcurrentMap_Get](#ConcurrentMap_Get) - [ConcurrentMap_Set](#ConcurrentMap_Set) @@ -988,6 +990,98 @@ func main() { } ``` +### ToSortedSlicesDefault + +

将map的key和value转化成两个根据key的值从小到大排序的切片,value切片中元素的位置与key对应。

+ +函数签名: + +```go +func ToSortedSlicesDefault[K constraints.Ordered, V any](m map[K]V) ([]K, []V) +``` + +示例:[运行](todo) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/maputil" +) + +func main() { + m := map[int]string{ + 1: "a", + 3: "c", + 2: "b", + } + + keys, values := ToSortedSlicesDefault(m) + + fmt.Println(keys) + fmt.Println(values) + + // Output: + // [1 2 3] + // [a b c] +} +``` + +### ToSortedSlicesWithComparator + +

将map的key和value转化成两个使用比较器函数根据key的值自定义排序规则的切片,value切片中元素的位置与key对应。

+ +函数签名: + +```go +func ToSortedSlicesWithComparator[K comparable, V any](m map[K]V, comparator func(a, b K) bool) ([]K, []V) +``` + +示例:[运行](todo) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/maputil" +) + +func main() { + m1 := map[time.Time]string{ + time.Date(2024, 3, 31, 0, 0, 0, 0, time.UTC): "today", + time.Date(2024, 3, 30, 0, 0, 0, 0, time.UTC): "yesterday", + time.Date(2024, 4, 1, 0, 0, 0, 0, time.UTC): "tomorrow", + } + + keys1, values1 := ToSortedSlicesWithComparator(m1, func(a, b time.Time) bool { + return a.Before(b) + }) + + m2 := map[int]string{ + 1: "a", + 3: "c", + 2: "b", + } + keys2, values2 := ToSortedSlicesWithComparator(m2, func(a, b int) bool { + return a > b + }) + + fmt.Println(keys2) + fmt.Println(values2) + + fmt.Println(keys1) + fmt.Println(values1) + + // Output: + // [2024-03-30 00:00:00 +0000 UTC 2024-03-31 00:00:00 +0000 UTC 2024-04-01 00:00:00 +0000 UTC] + // [yesterday today tomorrow] + // [3 2 1] + // [c b a] +} +``` + ### NewConcurrentMap

ConcurrentMap协程安全的map结构。

diff --git a/docs/api/packages/mathutil.md b/docs/api/packages/mathutil.md index d0f09038..a75d73ba 100644 --- a/docs/api/packages/mathutil.md +++ b/docs/api/packages/mathutil.md @@ -508,7 +508,7 @@ func main() { func CeilToFloat[T constraints.Float | constraints.Integer](x T, n int) float64 ``` -示例: +示例:[运行](https://go.dev/play/p/8hOeSADZPCo) ```go package main @@ -544,7 +544,7 @@ func main() { func CeilToString[T constraints.Float | constraints.Integer](x T, n int) string ``` -示例: +示例:[运行](https://go.dev/play/p/wy5bYEyUKKG) ```go package main @@ -577,10 +577,10 @@ func main() { 函数签名: ```go -func CeilToString[T constraints.Float | constraints.Integer](x T, n int) string +func FloorToFloat[T constraints.Float | constraints.Integer](x T, n int) float64 ``` -示例: +示例:[运行](https://go.dev/play/p/vbCBrQHZEED) ```go package main @@ -613,10 +613,10 @@ func main() { 函数签名: ```go -func CeilToString[T constraints.Float | constraints.Integer](x T, n int) string +func FloorToString[T constraints.Float | constraints.Integer](x T, n int) string ``` -示例: +示例:[运行](https://go.dev/play/p/Qk9KPd2IdDb) ```go package main @@ -1130,7 +1130,7 @@ func main() { ### Div -

除法运算.

+

除法运算。

函数签名: @@ -1138,7 +1138,7 @@ func main() { func Div[T constraints.Float | constraints.Integer](x T, y T) float64 ``` -示例: +示例:[运行](https://go.dev/play/p/WLxDdGXXYat) ```go package main @@ -1156,6 +1156,7 @@ func main() { fmt.Println(result1) fmt.Println(result2) fmt.Println(result3) + // Output: // 2.25 // 0.5 diff --git a/docs/api/packages/slice.md b/docs/api/packages/slice.md index 2b41f7a4..d43a66d6 100644 --- a/docs/api/packages/slice.md +++ b/docs/api/packages/slice.md @@ -94,6 +94,9 @@ import ( - [Join](#Join) - [Partition](#Partition) - [SetToDefaultIf](#SetToDefaultIf) +- [Break](#Break) +- [RightPadding](#RightPadding) +- [LeftPadding](#LeftPadding)
@@ -2581,7 +2584,7 @@ func main() { func SetToDefaultIf[T any](slice []T, predicate func(T) bool) ([]T, int) ``` -示例: +示例:[运行](https://go.dev/play/p/9AXGlPRC0-A) ```go import ( @@ -2600,4 +2603,91 @@ func main() { // [ b c d ] // 3 } +``` + +### Break + +

根据判断函数将切片分成两部分。它开始附加到与函数匹配的第一个元素之后的第二个切片。第一个匹配之后的所有元素都包含在第二个切片中,无论它们是否与函数匹配。

+ +函数签名: + +```go +func Break[T any](values []T, predicate func(T) bool) ([]T, []T) +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + nums := []int{1, 2, 3, 4, 5} + even := func(n int) bool { return n%2 == 0 } + + resultEven, resultAfterFirstEven := slice.Break(nums, even) + + fmt.Println(resultEven) + fmt.Println(resultAfterFirstEven) + + // Output: + // [1] + // [2 3 4 5] +} +``` + +RightPadding + +

在切片的右部添加元素。

+ +函数签名: + +```go +func RightPadding[T any](slice []T, paddingValue T, paddingLength int) []T +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + nums := []int{1, 2, 3, 4, 5} + padded := slice.RightPadding(nums, 0, 3) + fmt.Println(padded) + // Output: + // [1 2 3 4 5 0 0 0] +} +``` + +LeftPadding + +

在切片的左部添加元素。

+ +函数签名: + +```go +func LeftPadding[T any](slice []T, paddingValue T, paddingLength int) []T +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + nums := []int{1, 2, 3, 4, 5} + padded := slice.LeftPadding(nums, 0, 3) + fmt.Println(padded) + // Output: + // [0 0 0 1 2 3 4 5] +} ``` \ No newline at end of file diff --git a/docs/api/packages/strutil.md b/docs/api/packages/strutil.md index 95f190d3..645a2cd6 100644 --- a/docs/api/packages/strutil.md +++ b/docs/api/packages/strutil.md @@ -62,6 +62,7 @@ import ( - [RemoveWhiteSpace](#RemoveWhiteSpace) - [SubInBetween](#SubInBetween) - [HammingDistance](#HammingDistance) +- [Concat](#Concat)
@@ -1475,7 +1476,7 @@ func main() { func SubInBetween(str string, start string, end string) string ``` -示例: +示例:[运行](https://go.dev/play/p/EDbaRvjeNsv) ```go import ( @@ -1505,10 +1506,10 @@ func main() { 函数签名: ```go -HammingDistance(a, b string) (int, error) +func HammingDistance(a, b string) (int, error) ``` -示例: +示例:[运行](https://go.dev/play/p/glNdQEA9HUi) ```go import ( @@ -1528,4 +1529,38 @@ func main() { // 0 // 1 } + +``` +### Concat + +

拼接字符串。length是拼接后字符串的长度,如果不确定则传0或负数。

+ +函数签名: + +```go +func Concat(length int, str ...string) string +``` + +示例:[运行]() + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/strutil" +) + +func main() { + + result1 := strutil.Concat(12, "Hello", " ", "World", "!") + result2 := strutil.Concat(11, "Go", " ", "Language") + result3 := strutil.Concat(0, "An apple a ", "day,", "keeps the", " doctor away") + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // Hello World! + // Go Language + // An apple a day,keeps the doctor away +} ``` \ No newline at end of file diff --git a/docs/en/api/packages/convertor.md b/docs/en/api/packages/convertor.md index d61a4f3e..10ab38dd 100644 --- a/docs/en/api/packages/convertor.md +++ b/docs/en/api/packages/convertor.md @@ -571,6 +571,7 @@ func main() { } ``` + ### EncodeByte

Encode data to byte slice.

@@ -610,7 +611,7 @@ func main() { func DecodeByte(data []byte, target any) error ``` -Example: +Example:[Run](https://go.dev/play/p/zI6xsmuQRbn) ```go package main @@ -636,69 +637,6 @@ func main() { } ``` -### DeepClone - -

Creates a deep copy of passed item, can't clone unexported field of struct.

- -Signature: - -```go -func DeepClone[T any](src T) T -``` - -Example:[Run](https://go.dev/play/p/j4DP5dquxnk) - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/convertor" -) - -func main() { - type Struct struct { - Str string - Int int - Float float64 - Bool bool - Nil interface{} - unexported string - } - - cases := []interface{}{ - true, - 1, - 0.1, - map[string]int{ - "a": 1, - "b": 2, - }, - &Struct{ - Str: "test", - Int: 1, - Float: 0.1, - Bool: true, - Nil: nil, - // unexported: "can't be cloned", - }, - } - - for _, item := range cases { - cloned := convertor.DeepClone(item) - - isPointerEqual := &cloned == &item - fmt.Println(cloned, isPointerEqual) - } - - // Output: - // true false - // 1 false - // 0.1 false - // map[a:1 b:2] false - // &{test 1 0.1 true } false -} -``` ### CopyProperties @@ -779,41 +717,6 @@ func main() { } ``` -### ToInterface - -

Converts reflect value to its interface type.

- -Signature: - -```go -func ToInterface(v reflect.Value) (value interface{}, ok bool) -``` - -Example:[Run](https://go.dev/play/p/syqw0-WG7Xd) - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/convertor" -) - -func main() { - val := reflect.ValueOf("abc") - iVal, ok := convertor.ToInterface(val) - - fmt.Printf("%T\n", iVal) - fmt.Printf("%v\n", iVal) - fmt.Println(ok) - - // Output: - // string - // abc - // true -} -``` - ### Utf8ToGbk

Converts utf8 encoding data to GBK encoding data.

@@ -883,7 +786,7 @@ func main() { ### ToStdBase64 -

Convert a value to a string encoded in standard Base64. Error data of type "error" will also be encoded, and complex structures will be converted to a JSON-formatted string.

+

Convert a value to a string encoded in standard Base64. Error data of type "error" will also be encoded, and complex structures will be converted to a JSON formatted string.

Signature: @@ -891,7 +794,7 @@ func main() { func ToStdBase64(value any) string ``` -Example: +Example:[Run](https://go.dev/play/p/_fLJqJD3NMo) ```go package main @@ -953,9 +856,11 @@ func main() { ``` + + ### ToUrlBase64 -

Convert a value to a string encoded in url Base64. Error data of type "error" will also be encoded, and complex structures will be converted to a JSON-formatted string.

+

Convert a value to a string encoded in url Base64. Error data of type "error" will also be encoded, and complex structures will be converted to a JSON formatted string.

Signature: @@ -963,7 +868,7 @@ func main() { func ToUrlBase64(value any) string ``` -Example: +Example:[Run](https://go.dev/play/p/C_d0GlvEeUR) ```go package main @@ -1024,7 +929,7 @@ func main() { ### ToRawStdBase64 -

Convert a value to a string encoded in raw standard Base64. Error data of type "error" will also be encoded, and complex structures will be converted to a JSON-formatted string.

+

Convert a value to a string encoded in raw standard Base64. Error data of type "error" will also be encoded, and complex structures will be converted to a JSON formatted string.

Signature: @@ -1032,7 +937,7 @@ func main() { func ToRawStdBase64(value any) string ``` -Example: +Example:[Run](https://go.dev/play/p/wSAr3sfkDcv) ```go package main @@ -1045,7 +950,7 @@ import ( func main() { stringVal := "hello" - afterEncode = convertor.ToRawStdBase64(stringVal) + afterEncode := convertor.ToRawStdBase64(stringVal) fmt.Println(afterEncode) byteSliceVal := []byte("hello") @@ -1088,7 +993,7 @@ func main() { ### ToRawUrlBase64 -

Convert a value to a string encoded in raw url Base64. Error data of type "error" will also be encoded, and complex structures will be converted to a JSON-formatted string.

+

Convert a value to a string encoded in raw url Base64. Error data of type "error" will also be encoded, and complex structures will be converted to a JSON formatted string.

Signature: @@ -1096,7 +1001,7 @@ func main() { func ToRawUrlBase64(value any) string ``` -Example: +Example:[Run](https://go.dev/play/p/HwdDPFcza1O) ```go package main @@ -1109,7 +1014,7 @@ import ( func main() { stringVal := "hello" - afterEncode = convertor.ToRawUrlBase64(stringVal) + afterEncode := convertor.ToRawUrlBase64(stringVal) fmt.Println(afterEncode) byteSliceVal := []byte("hello") @@ -1132,11 +1037,11 @@ func main() { fmt.Println(afterEncode) boolVal := true - afterEncode = convertor.ToRawStdBase64(boolVal) + afterEncode = convertor.ToRawUrlBase64(boolVal) fmt.Println(afterEncode) errVal := errors.New("err") - afterEncode = convertor.ToRawStdBase64(errVal) + afterEncode = convertor.ToRawUrlBase64(errVal) fmt.Println(afterEncode) // Output: @@ -1148,4 +1053,67 @@ func main() { // dHJ1ZQ // ZXJy } +``` + +### DeepClone + +

Creates a deep copy of passed item, can't clone unexported field of struct.

+ +Signature: + +```go +func DeepClone[T any](src T) T +``` + +Example:[Run](https://go.dev/play/p/j4DP5dquxnk) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/convertor" +) + +func main() { + type Struct struct { + Str string + Int int + Float float64 + Bool bool + Nil interface{} + unexported string + } + + cases := []interface{}{ + true, + 1, + 0.1, + map[string]int{ + "a": 1, + "b": 2, + }, + &Struct{ + Str: "test", + Int: 1, + Float: 0.1, + Bool: true, + Nil: nil, + }, + } + + for _, item := range cases { + cloned := convertor.DeepClone(item) + + isPointerEqual := &cloned == &item + fmt.Println(cloned, isPointerEqual) + } + + // Output: + // true false + // 1 false + // 0.1 false + // map[a:1 b:2] false + // &{test 1 0.1 true } false +} ``` \ No newline at end of file diff --git a/docs/en/api/packages/datastructure/hashmap.md b/docs/en/api/packages/datastructure/hashmap.md index de0fd3c5..59d0a4ac 100644 --- a/docs/en/api/packages/datastructure/hashmap.md +++ b/docs/en/api/packages/datastructure/hashmap.md @@ -32,6 +32,7 @@ import ( - [Iterate](#Iterate) - [Keys](#Keys) - [Values](#Values) +- [FilterByValue](#FilterByValue)
@@ -311,4 +312,77 @@ func main() { } ``` +### FilterByValue +

Returns a filtered HashMap.

+ +Signature: + +```go +func (hm *HashMap) FilterByValue(perdicate func(value any) bool) *HashMap +``` + +Example: + +```go +package main + +import ( + "fmt" + hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap" +) + +func main() { + hm := hashmap.NewHashMap() + + hm.Put("a", 1) + hm.Put("b", 2) + hm.Put("c", 3) + hm.Put("d", 4) + hm.Put("e", 5) + hm.Put("f", 6) + + filteredHM := hm.FilterByValue(func(value any) bool { + return value.(int) == 1 || value.(int) == 3 + }) + + fmt.Println(filteredHM.Size()) //2 +} +``` + + + +### ToInterface + +

Converts reflect value to its interface type.

+ +Signature: + +```go +func ToInterface(v reflect.Value) (value interface{}, ok bool) +``` + +Example:[Run](https://go.dev/play/p/syqw0-WG7Xd) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/convertor" +) + +func main() { + val := reflect.ValueOf("abc") + iVal, ok := convertor.ToInterface(val) + + fmt.Printf("%T\n", iVal) + fmt.Printf("%v\n", iVal) + fmt.Println(ok) + + // Output: + // string + // abc + // true +} +``` \ No newline at end of file diff --git a/docs/en/api/packages/fileutil.md b/docs/en/api/packages/fileutil.md index b4a449ca..e5729369 100644 --- a/docs/en/api/packages/fileutil.md +++ b/docs/en/api/packages/fileutil.md @@ -167,7 +167,7 @@ func main() { ### CopyDir -

copy src directory to dst directory, it will copy all files and directories recursively. the access permission will be the same as the source directory. if dstPath exists, it will return an error.

+

Copy src directory to dst directory, it will copy all files and directories recursively. the access permission will be the same as the source directory. if dstPath exists, it will return an error.

Signature: @@ -974,7 +974,7 @@ func main() { func ChunkRead(file *os.File, offset int64, size int, bufPool *sync.Pool) ([]string, error) ``` -Example: +Example:[Run](https://go.dev/play/p/r0hPmKWhsgf) ```go package main @@ -1033,7 +1033,7 @@ func main() { func ParallelChunkRead(filePath string, linesCh chan<- []string, chunkSizeMB, maxGoroutine int) error ``` -Example: +Example:[Run](https://go.dev/play/p/teMXnCsdSEw) ```go package main diff --git a/docs/en/api/packages/function.md b/docs/en/api/packages/function.md index f4cf6d13..4c31cf18 100644 --- a/docs/en/api/packages/function.md +++ b/docs/en/api/packages/function.md @@ -423,7 +423,7 @@ func longRunningTask() { func And[T any](predicates ...func(T) bool) func(T) bool ``` -Example: +Example:[Run](https://go.dev/play/p/dTBHJMQ0zD2) ```go package main @@ -460,7 +460,7 @@ func main() { func Or[T any](predicates ...func(T) bool) func(T) bool ``` -Example: +Example:[Run](https://go.dev/play/p/LitCIsDFNDA) ```go package main @@ -495,7 +495,7 @@ func main() { func Negate[T any](predicate func(T) bool) func(T) bool ``` -Example: +Example:[Run](https://go.dev/play/p/jbI8BtgFnVE) ```go package main @@ -535,7 +535,7 @@ func main() { func Nor[T any](predicates ...func(T) bool) func(T) bool ``` -Example: +Example:[Run](https://go.dev/play/p/2KdCoBEOq84) ```go package main @@ -577,7 +577,7 @@ func main() { func Nand[T any](predicates ...func(T) bool) func(T) bool ``` -Example: +Example:[Run](https://go.dev/play/p/Rb-FdNGpgSO) ```go package main @@ -614,7 +614,7 @@ func main() { func Xnor[T any](predicates ...func(T) bool) func(T) bool ``` -Example: +Example:[Run](https://go.dev/play/p/FJxko8SFbqc) ```go package main @@ -643,9 +643,7 @@ func main() { ### AcceptIf -

AcceptIf returns another function of the same signature as the apply function but also includes a bool value to indicate success or failure. -A predicate function that takes an argument of type T and returns a bool. -An apply function that also takes an argument of type T and returns a modified value of the same type.

+

AcceptIf returns another function of the same signature as the apply function but also includes a bool value to indicate success or failure. A predicate function that takes an argument of type T and returns a bool. An apply function that also takes an argument of type T and returns a modified value of the same type.

Signature: @@ -653,7 +651,7 @@ An apply function that also takes an argument of type T and returns a modified v func AcceptIf[T any](predicate func(T) bool, apply func(T) T) func(T) (T, bool) ``` -Example: +Example:[Run](https://go.dev/play/p/XlXHHtzCf7d) ```go package main @@ -665,8 +663,8 @@ import ( func main() { - adder := AcceptIf( - And( + adder := function.AcceptIf( + function.And( func(x int) bool { return x > 10 }, func(x int) bool { diff --git a/docs/en/api/packages/maputil.md b/docs/en/api/packages/maputil.md index 6e12a075..894803e9 100644 --- a/docs/en/api/packages/maputil.md +++ b/docs/en/api/packages/maputil.md @@ -44,6 +44,8 @@ import ( - [Minus](#Minus) - [IsDisjoint](#IsDisjoint) - [HasKey](#HasKey) +- [ToSortedSlicesDefault](#ToSortedSlicesDefault) +- [ToSortedSlicesWithComparator](#ToSortedSlicesWithComparator) - [NewConcurrentMap](#NewConcurrentMap) - [ConcurrentMap_Get](#ConcurrentMap_Get) - [ConcurrentMap_Set](#ConcurrentMap_Set) @@ -992,6 +994,101 @@ func main() { } ``` +### ToSortedSlicesDefault + +

+Translate the key and value of the map into two slices that are sorted in ascending order according to the key’s value, with the position of the elements in the value slice corresponding to the key.

+ +Signature: + +```go +func ToSortedSlicesDefault[K constraints.Ordered, V any](m map[K]V) ([]K, []V) +``` + +Example:[Run](Todo) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/maputil" +) + +func main() { + m := map[int]string{ + 1: "a", + 3: "c", + 2: "b", + } + + keys, values := ToSortedSlicesDefault(m) + + fmt.Println(keys) + fmt.Println(values) + + // Output: + // [1 2 3] + // [a b c] +} +``` + +### ToSortedSlicesWithComparator + +

+Translate the key and value of the map into two slices that are sorted according to a custom sorting rule defined by a comparator function based on the key's value, with the position of the elements in the value slice corresponding to the key. +

+ +Signature: + +```go +func ToSortedSlicesWithComparator[K comparable, V any](m map[K]V, comparator func(a, b K) bool) ([]K, []V) +``` + +Example:[Run](Todo) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/maputil" +) + +func main() { + m1 := map[time.Time]string{ + time.Date(2024, 3, 31, 0, 0, 0, 0, time.UTC): "today", + time.Date(2024, 3, 30, 0, 0, 0, 0, time.UTC): "yesterday", + time.Date(2024, 4, 1, 0, 0, 0, 0, time.UTC): "tomorrow", + } + + keys1, values1 := ToSortedSlicesWithComparator(m1, func(a, b time.Time) bool { + return a.Before(b) + }) + + m2 := map[int]string{ + 1: "a", + 3: "c", + 2: "b", + } + keys2, values2 := ToSortedSlicesWithComparator(m2, func(a, b int) bool { + return a > b + }) + + fmt.Println(keys2) + fmt.Println(values2) + + fmt.Println(keys1) + fmt.Println(values1) + + // Output: + // [2024-03-30 00:00:00 +0000 UTC 2024-03-31 00:00:00 +0000 UTC 2024-04-01 00:00:00 +0000 UTC] + // [yesterday today tomorrow] + // [3 2 1] + // [c b a] +} +``` + ### NewConcurrentMap

ConcurrentMap is like map, but is safe for concurrent use by multiple goroutines.

diff --git a/docs/en/api/packages/mathutil.md b/docs/en/api/packages/mathutil.md index 2e0e49a9..3f4387f1 100644 --- a/docs/en/api/packages/mathutil.md +++ b/docs/en/api/packages/mathutil.md @@ -508,7 +508,7 @@ func main() { func CeilToFloat[T constraints.Float | constraints.Integer](x T, n int) float64 ``` -Example: +Example:[Run](https://go.dev/play/p/8hOeSADZPCo) ```go package main @@ -544,7 +544,7 @@ func main() { func CeilToString[T constraints.Float | constraints.Integer](x T, n int) string ``` -Example: +Example:[Run](https://go.dev/play/p/wy5bYEyUKKG) ```go package main @@ -577,10 +577,10 @@ func main() { Signature: ```go -func CeilToString[T constraints.Float | constraints.Integer](x T, n int) string +func FloorToFloat[T constraints.Float | constraints.Integer](x T, n int) float64 ``` -Example: +Example:[Run](https://go.dev/play/p/vbCBrQHZEED) ```go package main @@ -613,10 +613,10 @@ func main() { Signature: ```go -func CeilToString[T constraints.Float | constraints.Integer](x T, n int) string +func FloorToString[T constraints.Float | constraints.Integer](x T, n int) string ``` -Example: +Example:[Run](https://go.dev/play/p/Qk9KPd2IdDb) ```go package main @@ -1130,7 +1130,7 @@ func main() { ### Div -

returns the result of x divided by y.

+

Returns the result of x divided by y.

Signature: @@ -1138,7 +1138,7 @@ func main() { func Div[T constraints.Float | constraints.Integer](x T, y T) float64 ``` -Example: +Example:[Run](https://go.dev/play/p/WLxDdGXXYat) ```go package main diff --git a/docs/en/api/packages/slice.md b/docs/en/api/packages/slice.md index 9c46ce56..381152a1 100644 --- a/docs/en/api/packages/slice.md +++ b/docs/en/api/packages/slice.md @@ -93,6 +93,10 @@ import ( - [KeyBy](#KeyBy) - [Join](#Join) - [Partition](#Partition) +- [SetToDefaultIf](#SetToDefaultIf) +- [Break](#Break) +- [RightPadding](#RightPadding) +- [LeftPadding](#LeftPadding)
@@ -2577,7 +2581,7 @@ func main() { func SetToDefaultIf[T any](slice []T, predicate func(T) bool) ([]T, int) ``` -Example: +Example:[Run](https://go.dev/play/p/9AXGlPRC0-A) ```go import ( @@ -2596,4 +2600,91 @@ func main() { // [ b c d ] // 3 } +``` + +### Break + +

Splits a slice into two based on a predicate function. It starts appending to the second slice after the first element that matches the predicate. All elements after the first match are included in the second slice, regardless of whether they match the predicate or not.

+ +Signature: + +```go +func Break[T any](values []T, predicate func(T) bool) ([]T, []T) +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + nums := []int{1, 2, 3, 4, 5} + even := func(n int) bool { return n%2 == 0 } + + resultEven, resultAfterFirstEven := slice.Break(nums, even) + + fmt.Println(resultEven) + fmt.Println(resultAfterFirstEven) + + // Output: + // [1] + // [2 3 4 5] +} +``` + +RightPadding + +

RightPadding adds padding to the right end of a slice.

+ +Signature: + +```go +func RightPadding[T any](slice []T, paddingValue T, paddingLength int) []T +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + nums := []int{1, 2, 3, 4, 5} + padded := RightPadding(nums, 0, 3) + fmt.Println(padded) + // Output: + // [1 2 3 4 5 0 0 0] +} +``` + +LeftPadding + +

LeftPadding adds padding to the left begin of a slice.

+ +Signature: + +```go +func LeftPadding[T any](slice []T, paddingValue T, paddingLength int) []T +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + nums := []int{1, 2, 3, 4, 5} + padded := LeftPadding(nums, 0, 3) + fmt.Println(padded) + // Output: + // [0 0 0 1 2 3 4 5] +} ``` \ No newline at end of file diff --git a/docs/en/api/packages/strutil.md b/docs/en/api/packages/strutil.md index 676a8bd1..471e5230 100644 --- a/docs/en/api/packages/strutil.md +++ b/docs/en/api/packages/strutil.md @@ -62,6 +62,7 @@ import ( - [RemoveWhiteSpace](#RemoveWhiteSpace) - [SubInBetween](#SubInBetween) - [HammingDistance](#HammingDistance) +- [Concat](#Concat)
@@ -1477,7 +1478,7 @@ func main() { func SubInBetween(str string, start string, end string) string ``` -Example: +Example:[Run](https://go.dev/play/p/EDbaRvjeNsv) ```go import ( @@ -1507,10 +1508,10 @@ func main() { Signature: ```go -HammingDistance(a, b string) (int, error) +func HammingDistance(a, b string) (int, error) ``` -Example: +Example:[Run](https://go.dev/play/p/glNdQEA9HUi) ```go import ( @@ -1530,4 +1531,39 @@ func main() { // 0 // 1 } +``` + + +### Concat + +

Concatenates strings. length is the length of the concatenated string. If unsure, pass 0 or a negative number.

+ +Signature: + +```go +func Concat(length int, str ...string) string +``` + +Example:[Run]() + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/strutil" +) + +func main() { + + result1 := strutil.Concat(12, "Hello", " ", "World", "!") + result2 := strutil.Concat(11, "Go", " ", "Language") + result3 := strutil.Concat(0, "An apple a ", "day,", "keeps the", " doctor away") + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // Hello World! + // Go Language + // An apple a day,keeps the doctor away +} ``` \ No newline at end of file diff --git a/fileutil/file.go b/fileutil/file.go index 4e08d2be..052d0b20 100644 --- a/fileutil/file.go +++ b/fileutil/file.go @@ -869,7 +869,7 @@ func isCsvSupportedType(v interface{}) bool { } // ChunkRead reads a block from the file at the specified offset and returns all lines within the block -// Play: todo +// Play: https://go.dev/play/p/r0hPmKWhsgf func ChunkRead(file *os.File, offset int64, size int, bufPool *sync.Pool) ([]string, error) { buf := bufPool.Get().([]byte)[:size] // 从Pool获取缓冲区并调整大小 n, err := file.ReadAt(buf, offset) // 从指定偏移读取数据到缓冲区 @@ -901,7 +901,7 @@ func ChunkRead(file *os.File, offset int64, size int, bufPool *sync.Pool) ([]str // chunkSizeMB 分块的大小(单位MB,设置为0时使用默认100MB),设置过大反而不利,视情调整 // maxGoroutine 并发读取分块的数量,设置为0时使用CPU核心数 // linesCh用于接收返回结果的通道。 -// Play: todo +// Play: https://go.dev/play/p/teMXnCsdSEw func ParallelChunkRead(filePath string, linesCh chan<- []string, chunkSizeMB, maxGoroutine int) error { if chunkSizeMB == 0 { chunkSizeMB = 100 diff --git a/function/function.go b/function/function.go index 08409836..3100467e 100644 --- a/function/function.go +++ b/function/function.go @@ -139,6 +139,7 @@ func Pipeline[T any](funcs ...func(T) T) func(T) T { // AcceptIf returns another function of the same signature as the apply function but also includes a bool value to indicate success or failure. // A predicate function that takes an argument of type T and returns a bool. // An apply function that also takes an argument of type T and returns a modified value of the same type. +// Play: https://go.dev/play/p/XlXHHtzCf7d func AcceptIf[T any](predicate func(T) bool, apply func(T) T) func(T) (T, bool) { if predicate == nil { panic("programming error: predicate must be not nil") diff --git a/function/predicate.go b/function/predicate.go index 6d76d0a0..c31eeb2d 100644 --- a/function/predicate.go +++ b/function/predicate.go @@ -2,6 +2,7 @@ package function // And returns a composed predicate that represents the logical AND of a list of predicates. // It evaluates to true only if all predicates evaluate to true for the given value. +// Play: https://go.dev/play/p/dTBHJMQ0zD2 func And[T any](predicates ...func(T) bool) func(T) bool { if len(predicates) < 2 { panic("programming error: predicates count must be at least 2") @@ -18,6 +19,7 @@ func And[T any](predicates ...func(T) bool) func(T) bool { // Nand returns a composed predicate that represents the logical NAND of a list of predicates. // It evaluates to true only if all predicates evaluate to false for the given value. +// Play: https://go.dev/play/p/Rb-FdNGpgSO func Nand[T any](predicates ...func(T) bool) func(T) bool { if len(predicates) < 2 { panic("programming error: predicates count must be at least 2") @@ -33,6 +35,7 @@ func Nand[T any](predicates ...func(T) bool) func(T) bool { } // Negate returns a predicate that represents the logical negation of this predicate. +// Play: https://go.dev/play/p/jbI8BtgFnVE func Negate[T any](predicate func(T) bool) func(T) bool { return func(value T) bool { return !predicate(value) @@ -41,6 +44,7 @@ func Negate[T any](predicate func(T) bool) func(T) bool { // Or returns a composed predicate that represents the logical OR of a list of predicates. // It evaluates to true if at least one of the predicates evaluates to true for the given value. +// Play: https://go.dev/play/p/LitCIsDFNDA func Or[T any](predicates ...func(T) bool) func(T) bool { if len(predicates) < 2 { panic("programming error: predicates count must be at least 2") @@ -57,6 +61,7 @@ func Or[T any](predicates ...func(T) bool) func(T) bool { // Nor returns a composed predicate that represents the logical NOR of a list of predicates. // It evaluates to true only if all predicates evaluate to false for the given value. +// Play: https://go.dev/play/p/2KdCoBEOq84 func Nor[T any](predicates ...func(T) bool) func(T) bool { if len(predicates) < 2 { panic("programming error: predicates count must be at least 2") @@ -73,6 +78,7 @@ func Nor[T any](predicates ...func(T) bool) func(T) bool { // Xnor returns a composed predicate that represents the logical XNOR of a list of predicates. // It evaluates to true only if all predicates evaluate to true or false for the given value. +// Play: https://go.dev/play/p/FJxko8SFbqc func Xnor[T any](predicates ...func(T) bool) func(T) bool { if len(predicates) < 2 { panic("programming error: predicates count must be at least 2") diff --git a/maputil/map.go b/maputil/map.go index 06106b9a..b8cd70af 100644 --- a/maputil/map.go +++ b/maputil/map.go @@ -7,6 +7,10 @@ package maputil import ( "fmt" "reflect" + "sort" + "strings" + + "golang.org/x/exp/constraints" "github.com/duke-git/lancet/v2/slice" ) @@ -375,8 +379,7 @@ func getFieldNameByJsonTag(structObj any, jsonTag string) string { for i := 0; i < s.NumField(); i++ { field := s.Field(i) tag := field.Tag - name := tag.Get("json") - + name, _, _ := strings.Cut(tag.Get("json"), ",") if name == jsonTag { return field.Name } @@ -384,3 +387,50 @@ func getFieldNameByJsonTag(structObj any, jsonTag string) string { return "" } + +// ToSortedSlicesDefault converts a map to two slices sorted by key: one for the keys and another for the values. +func ToSortedSlicesDefault[K constraints.Ordered, V any](m map[K]V) ([]K, []V) { + keys := make([]K, 0, len(m)) + + // store the map’s keys into a slice + for k := range m { + keys = append(keys, k) + } + + // sort the slice of keys + sort.Slice(keys, func(i, j int) bool { + return keys[i] < keys[j] + }) + + // adjust the order of values according to the sorted keys + sortedValues := make([]V, len(keys)) + for i, k := range keys { + sortedValues[i] = m[k] + } + + return keys, sortedValues +} + +// ToSortedSlicesWithComparator converts a map to two slices sorted by key and using a custom comparison function: +// one for the keys and another for the values. +func ToSortedSlicesWithComparator[K comparable, V any](m map[K]V, comparator func(a, b K) bool) ([]K, []V) { + keys := make([]K, 0, len(m)) + + // store the map’s keys into a slice + for k := range m { + keys = append(keys, k) + } + + // sort the key slice using the provided comparison function + sort.Slice(keys, func(i, j int) bool { + return comparator(keys[i], keys[j]) + }) + + // adjust the order of values according to the sorted keys + sortedValues := make([]V, len(keys)) + for i, k := range keys { + sortedValues[i] = m[k] + } + + return keys, sortedValues +} diff --git a/maputil/map_example_test.go b/maputil/map_example_test.go index 1472257d..cadb309b 100644 --- a/maputil/map_example_test.go +++ b/maputil/map_example_test.go @@ -4,6 +4,7 @@ import ( "fmt" "sort" "strconv" + "time" ) func ExampleKeys() { @@ -450,3 +451,76 @@ func ExampleHasKey() { // true // false } + +func ExampleMapToStruct() { + + personReqMap := map[string]any{ + "name": "Nothin", + "max_age": 35, + "page": 1, + "pageSize": 10, + } + + type PersonReq struct { + Name string `json:"name"` + MaxAge int `json:"max_age"` + Page int `json:"page"` + PageSize int `json:"pageSize"` + } + var personReq PersonReq + _ = MapToStruct(personReqMap, &personReq) + fmt.Println(personReq) + + // Output: + // {Nothin 35 1 10} +} + +func ExampleToSortedSlicesDefault() { + m := map[int]string{ + 1: "a", + 3: "c", + 2: "b", + } + + keys, values := ToSortedSlicesDefault(m) + + fmt.Println(keys) + fmt.Println(values) + + // Output: + // [1 2 3] + // [a b c] +} + +func ExampleToSortedSlicesWithComparator() { + m1 := map[time.Time]string{ + time.Date(2024, 3, 31, 0, 0, 0, 0, time.UTC): "today", + time.Date(2024, 3, 30, 0, 0, 0, 0, time.UTC): "yesterday", + time.Date(2024, 4, 1, 0, 0, 0, 0, time.UTC): "tomorrow", + } + + keys1, values1 := ToSortedSlicesWithComparator(m1, func(a, b time.Time) bool { + return a.Before(b) + }) + + m2 := map[int]string{ + 1: "a", + 3: "c", + 2: "b", + } + keys2, values2 := ToSortedSlicesWithComparator(m2, func(a, b int) bool { + return a > b + }) + + fmt.Println(keys1) + fmt.Println(values1) + + fmt.Println(keys2) + fmt.Println(values2) + + // Output: + // [2024-03-30 00:00:00 +0000 UTC 2024-03-31 00:00:00 +0000 UTC 2024-04-01 00:00:00 +0000 UTC] + // [yesterday today tomorrow] + // [3 2 1] + // [c b a] +} diff --git a/maputil/map_test.go b/maputil/map_test.go index 7753637a..9737e36f 100644 --- a/maputil/map_test.go +++ b/maputil/map_test.go @@ -1,9 +1,11 @@ package maputil import ( + "math/cmplx" "sort" "strconv" "testing" + "time" "github.com/duke-git/lancet/v2/internal" ) @@ -483,7 +485,7 @@ func TestMapToStruct(t *testing.T) { Name string `json:"name"` Age int `json:"age"` Phone string `json:"phone"` - Addr *Address `json:"address"` + Addr *Address `json:"address,omitempty"` } Address struct { @@ -511,3 +513,181 @@ func TestMapToStruct(t *testing.T) { assert.Equal("test", p.Addr.Street) assert.Equal(1, p.Addr.Number) } + +func TestToSortedSlicesDefault(t *testing.T) { + t.Parallel() + + assert := internal.NewAssert(t, "TestToSortedSlicesDefault") + + testCases1 := []struct { + name string + inputMap map[string]any + expKeys []string + expValues []any + }{ + { + name: "Empty Map", + inputMap: map[string]any{}, + expKeys: []string{}, + expValues: []any{}, + }, + { + name: "Unsorted Map", + inputMap: map[string]any{"c": 3, "a": 1.6, "b": 2}, + expKeys: []string{"a", "b", "c"}, + expValues: []any{1.6, 2, 3}, + }, + } + + for _, tc := range testCases1 { + t.Run(tc.name, func(t *testing.T) { + keys, values := ToSortedSlicesDefault(tc.inputMap) + assert.Equal(tc.expKeys, keys) + assert.Equal(tc.expValues, values) + }) + } + + testCases2 := map[int64]string{ + 7: "seven", + 3: "three", + 5: "five", + } + actualK2, actualV2 := ToSortedSlicesDefault(testCases2) + case2Keys := []int64{3, 5, 7} + case2Values := []string{"three", "five", "seven"} + assert.Equal(case2Keys, actualK2) + assert.Equal(case2Values, actualV2) +} + +func TestToSortedSlicesWithComparator(t *testing.T) { + t.Parallel() + + assert := internal.NewAssert(t, "TestToSortedSlicesWithComparator") + + type Account struct { + ID int + Name string + CreateTime time.Time + FavorComplexNumber complex128 + Signal chan struct{} + } + + type testCase struct { + name string + inputMap map[Account]any + lessFunc func(a, b Account) bool + expKeys []Account + expValues []any + } + + now := time.Now() + tomorrow := now.AddDate(0, 0, 1) + yesterday := now.AddDate(0, 0, -1) + + account1 := Account{ID: 1, Name: "cya", CreateTime: now, FavorComplexNumber: complex(1.2, 3), + Signal: make(chan struct{}, 10)} + account2 := Account{ID: 2, Name: "Cannian", CreateTime: tomorrow, FavorComplexNumber: complex(1.2, 2), + Signal: make(chan struct{}, 7)} + account3 := Account{ID: 3, Name: "Clauviou", CreateTime: yesterday, FavorComplexNumber: complex(3, 4), + Signal: make(chan struct{}, 3)} + account1.Signal <- struct{}{} + account2.Signal <- struct{}{} + account2.Signal <- struct{}{} + + testCases := []testCase{ + { + name: "Sorted by Account ID", + inputMap: map[Account]any{ + account1: 100, + account2: 200, + account3: 300, + }, + lessFunc: func(a, b Account) bool { return a.ID < b.ID }, + expKeys: []Account{account1, account2, account3}, + expValues: []any{100, 200, 300}, + }, + { + name: "Reverse Sorted by Account ID", + inputMap: map[Account]any{ + account1: 100, + account2: 200, + account3: 300, + }, + lessFunc: func(a, b Account) bool { return a.ID > b.ID }, + expKeys: []Account{account3, account2, account1}, + expValues: []any{300, 200, 100}, + }, + { + name: "Sorted by Account Name", + inputMap: map[Account]any{ + account1: 100, + account2: 200, + account3: 300, + }, + lessFunc: func(a, b Account) bool { return a.Name < b.Name }, + expKeys: []Account{account2, account3, account1}, + expValues: []any{200, 300, 100}, + }, + { + name: "Empty Map", + inputMap: map[Account]any{}, + lessFunc: func(a, b Account) bool { return a.ID < b.ID }, + expKeys: []Account{}, + expValues: []any{}, + }, + { + name: "Sorted by Account CreateTime", + inputMap: map[Account]any{ + account1: "now", + account2: "tomorrow", + account3: "yesterday", + }, + lessFunc: func(a, b Account) bool { return a.CreateTime.Before(b.CreateTime) }, + expKeys: []Account{account3, account1, account2}, + expValues: []any{"yesterday", "now", "tomorrow"}, + }, + { + name: "Sorted by Account FavorComplexNumber", + inputMap: map[Account]any{ + account1: "1.2+3i", + account2: "1.2+2i", + account3: "3+4i", + }, + lessFunc: func(a, b Account) bool { return cmplx.Abs(a.FavorComplexNumber) < cmplx.Abs(b.FavorComplexNumber) }, + expKeys: []Account{account2, account1, account3}, + expValues: []any{"1.2+2i", "1.2+3i", "3+4i"}, + }, + { + name: "Sort by the buffer capacity of the channel", + inputMap: map[Account]any{ + account1: 10, + account2: 7, + account3: 3, + }, + lessFunc: func(a, b Account) bool { + return cap(a.Signal) < cap(b.Signal) + }, + expKeys: []Account{account3, account2, account1}, + expValues: []any{3, 7, 10}, + }, + { + name: "Sort by the number of elements in the channel", + inputMap: map[Account]any{ + account1: 1, + account2: 2, + account3: 0, + }, + lessFunc: func(a, b Account) bool { return len(a.Signal) < len(b.Signal) }, + expKeys: []Account{account3, account1, account2}, + expValues: []any{0, 1, 2}, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + keys, values := ToSortedSlicesWithComparator(tc.inputMap, tc.lessFunc) + assert.Equal(tc.expKeys, keys) + assert.Equal(tc.expValues, values) + }) + } +} diff --git a/mathutil/mathutil.go b/mathutil/mathutil.go index 531a0c95..dc6b1b1e 100644 --- a/mathutil/mathutil.go +++ b/mathutil/mathutil.go @@ -101,7 +101,7 @@ func TruncRound[T constraints.Float | constraints.Integer](x T, n int) T { } // FloorToFloat round down to n decimal places. -// Play: todo +// Play: https://go.dev/play/p/vbCBrQHZEED func FloorToFloat[T constraints.Float | constraints.Integer](x T, n int) float64 { tmp := math.Pow(10.0, float64(n)) x *= T(tmp) @@ -110,7 +110,7 @@ func FloorToFloat[T constraints.Float | constraints.Integer](x T, n int) float64 } // FloorToString round down to n decimal places. -// Play: todo +// Play: https://go.dev/play/p/Qk9KPd2IdDb func FloorToString[T constraints.Float | constraints.Integer](x T, n int) string { tmp := math.Pow(10.0, float64(n)) x *= T(tmp) @@ -120,7 +120,7 @@ func FloorToString[T constraints.Float | constraints.Integer](x T, n int) string } // CeilToFloat round up to n decimal places. -// Play: todo +// Play: https://go.dev/play/p/8hOeSADZPCo func CeilToFloat[T constraints.Float | constraints.Integer](x T, n int) float64 { tmp := math.Pow(10.0, float64(n)) x *= T(tmp) @@ -129,7 +129,7 @@ func CeilToFloat[T constraints.Float | constraints.Integer](x T, n int) float64 } // CeilToString round up to n decimal places. -// Play: todo +// Play: https://go.dev/play/p/wy5bYEyUKKG func CeilToString[T constraints.Float | constraints.Integer](x T, n int) string { tmp := math.Pow(10.0, float64(n)) x *= T(tmp) @@ -391,7 +391,7 @@ func Abs[T constraints.Integer | constraints.Float](x T) T { } // Div returns the result of x divided by y. -// Play: todo +// Play: https://go.dev/play/p/WLxDdGXXYat func Div[T constraints.Float | constraints.Integer](x T, y T) float64 { return float64(x) / float64(y) } diff --git a/netutil/http.go b/netutil/http.go index 54ad55e9..c9269da3 100644 --- a/netutil/http.go +++ b/netutil/http.go @@ -28,7 +28,6 @@ import ( "strings" "time" - "github.com/duke-git/lancet/v2/convertor" "github.com/duke-git/lancet/v2/slice" ) @@ -109,6 +108,7 @@ type HttpClientConfig struct { HandshakeTimeout time.Duration ResponseTimeout time.Duration Verbose bool + Proxy *url.URL } // defaultHttpClientConfig defalut client config. @@ -164,6 +164,11 @@ func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient { client.TLS = config.TLSConfig } + if config.Proxy != nil { + transport := client.Client.Transport.(*http.Transport) + transport.Proxy = http.ProxyURL(config.Proxy) + } + return client } @@ -363,11 +368,20 @@ func validateRequest(req *HttpRequest) error { // Play: https://go.dev/play/p/pFqMkM40w9z func StructToUrlValues(targetStruct any) (url.Values, error) { result := url.Values{} - s, err := convertor.StructToMap(targetStruct) + + var m map[string]interface{} + + jsonBytes, err := json.Marshal(targetStruct) if err != nil { return nil, err } - for k, v := range s { + + err = json.Unmarshal(jsonBytes, &m) + if err != nil { + return nil, err + } + + for k, v := range m { result.Add(k, fmt.Sprintf("%v", v)) } diff --git a/netutil/http_test.go b/netutil/http_test.go index 947e4f50..7cad1ae6 100644 --- a/netutil/http_test.go +++ b/netutil/http_test.go @@ -5,12 +5,12 @@ import ( "encoding/json" "io" "io/ioutil" - "log" "net/http" "net/http/httptest" "net/url" "os" "testing" + "time" "github.com/duke-git/lancet/v2/internal" ) @@ -23,7 +23,8 @@ func TestHttpGet(t *testing.T) { resp, err := HttpGet(url, header) if err != nil { - log.Fatal(err) + t.Log("net error: " + err.Error()) + return } body, _ := io.ReadAll(resp.Body) @@ -44,8 +45,10 @@ func TestHttpPost(t *testing.T) { resp, err := HttpPost(url, header, nil, bodyParams) if err != nil { - log.Fatal(err) + t.Log("net error: " + err.Error()) + return } + body, _ := io.ReadAll(resp.Body) t.Log("response: ", resp.StatusCode, string(body)) } @@ -54,21 +57,18 @@ func TestHttpPostFormData(t *testing.T) { apiUrl := "https://jsonplaceholder.typicode.com/todos" header := map[string]string{ "Content-Type": "application/x-www-form-urlencoded", - // "Content-Type": "multipart/form-data", } postData := url.Values{} postData.Add("userId", "1") postData.Add("title", "TestToDo") - // postData := make(map[string]string) - // postData["userId"] = "1" - // postData["title"] = "title" - resp, err := HttpPost(apiUrl, header, nil, postData) if err != nil { - log.Fatal(err) + t.Log("net error: " + err.Error()) + return } + body, _ := io.ReadAll(resp.Body) t.Log("response: ", resp.StatusCode, string(body)) } @@ -88,8 +88,10 @@ func TestHttpPut(t *testing.T) { resp, err := HttpPut(url, header, nil, bodyParams) if err != nil { - log.Fatal(err) + t.Log("net error: ", err.Error()) + return } + body, _ := io.ReadAll(resp.Body) t.Log("response: ", resp.StatusCode, string(body)) } @@ -109,8 +111,10 @@ func TestHttpPatch(t *testing.T) { resp, err := HttpPatch(url, header, nil, bodyParams) if err != nil { - log.Fatal(err) + t.Log("net error: ", err.Error()) + return } + body, _ := io.ReadAll(resp.Body) t.Log("response: ", resp.StatusCode, string(body)) } @@ -119,8 +123,10 @@ func TestHttpDelete(t *testing.T) { url := "https://jsonplaceholder.typicode.com/todos/1" resp, err := HttpDelete(url) if err != nil { - log.Fatal(err) + t.Log("net error: ", err.Error()) + return } + body, _ := io.ReadAll(resp.Body) t.Log("response: ", resp.StatusCode, string(body)) } @@ -147,7 +153,8 @@ func TestParseResponse(t *testing.T) { resp, err := HttpGet(url, header) if err != nil { - log.Fatal(err) + t.Log("net error: ", err.Error()) + return } type Todo struct { @@ -160,8 +167,10 @@ func TestParseResponse(t *testing.T) { toDoResp := &Todo{} err = ParseHttpResponse(resp, toDoResp) if err != nil { - log.Fatal(err) + t.Log("net error: ", err.Error()) + return } + t.Log("response: ", toDoResp) } @@ -178,7 +187,8 @@ func TestHttpClient_Get(t *testing.T) { httpClient := NewHttpClient() resp, err := httpClient.SendRequest(request) if err != nil || resp.StatusCode != 200 { - log.Fatal(err) + t.Log("net error: ", err.Error()) + return } type Todo struct { @@ -215,7 +225,8 @@ func TestHttpClent_Post(t *testing.T) { httpClient := NewHttpClient() resp, err := httpClient.SendRequest(request) if err != nil { - log.Fatal(err) + t.Log("net error: ", err.Error()) + return } body, _ := io.ReadAll(resp.Body) @@ -227,16 +238,25 @@ func TestStructToUrlValues(t *testing.T) { assert := internal.NewAssert(t, "TestStructToUrlValues") + type CommReq struct { + Version string `json:"version"` + } + type TodoQuery struct { - Id int `json:"id"` - UserId int `json:"userId"` - Name string `json:"name,omitempty"` + Id int `json:"id"` + UserId int `json:"userId"` + Name string `json:"name,omitempty"` + CommReq `json:",inline"` } item1 := TodoQuery{ Id: 1, UserId: 123, Name: "", + CommReq: CommReq{ + Version: "1.0", + }, } + todoValues, err := StructToUrlValues(item1) if err != nil { t.Errorf("params is invalid: %v", err) @@ -245,19 +265,10 @@ func TestStructToUrlValues(t *testing.T) { assert.Equal("1", todoValues.Get("id")) assert.Equal("123", todoValues.Get("userId")) assert.Equal("", todoValues.Get("name")) - - item2 := TodoQuery{ - Id: 2, - UserId: 456, - } - queryValues2, _ := StructToUrlValues(item2) - - assert.Equal("2", queryValues2.Get("id")) - assert.Equal("456", queryValues2.Get("userId")) - assert.Equal("", queryValues2.Get("name")) + assert.Equal("1.0", todoValues.Get("version")) } -func handleFileRequest(t *testing.T, w http.ResponseWriter, r *http.Request) { +func handleFileRequest(t *testing.T, _ http.ResponseWriter, r *http.Request) { err := r.ParseMultipartForm(1024) if err != nil { t.Fatal(err) @@ -361,3 +372,25 @@ func TestSendRequestWithFilePath(t *testing.T) { t.Fatalf("expected %d, got %d", http.StatusOK, resp.StatusCode) } } + +func TestProxy(t *testing.T) { + config := &HttpClientConfig{ + HandshakeTimeout: 20 * time.Second, + ResponseTimeout: 40 * time.Second, + // Use the proxy ip to add it here + //Proxy: &url.URL{ + // Scheme: "http", + // Host: "46.17.63.166:18888", + //}, + } + httpClient := NewHttpClientWithConfig(config) + resp, err := httpClient.Get("https://www.ipplus360.com/getLocation") + if err != nil { + t.Log("net error: ", err.Error()) + return + } + + if resp.StatusCode != http.StatusOK { + t.Fatalf("expected %d, got %d", http.StatusOK, resp.StatusCode) + } +} diff --git a/slice/slice.go b/slice/slice.go index 5893a9f8..6cf9e717 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -50,12 +50,23 @@ func ContainBy[T any](slice []T, predicate func(item T) bool) bool { // ContainSubSlice check if the slice contain a given subslice or not. // Play: https://go.dev/play/p/bcuQ3UT6Sev func ContainSubSlice[T comparable](slice, subSlice []T) bool { - for _, v := range subSlice { - if !Contain(slice, v) { + if len(subSlice) == 0 { + return true + } + if len(slice) == 0 { + return false + } + + elementMap := make(map[T]struct{}, len(slice)) + for _, item := range slice { + elementMap[item] = struct{}{} + } + + for _, item := range subSlice { + if _, ok := elementMap[item]; !ok { return false } } - return true } @@ -81,35 +92,41 @@ func Chunk[T any](slice []T, size int) [][]T { return result } -// Compact creates an slice with all falsey values removed. The values false, nil, 0, and "" are falsey. +// Compact creates a slice with all falsey values removed. The values false, nil, 0, and "" are falsey. // Play: https://go.dev/play/p/pO5AnxEr3TK func Compact[T comparable](slice []T) []T { var zero T - result := []T{} + result := make([]T, 0, len(slice)) for _, v := range slice { if v != zero { result = append(result, v) } } - - return result + return result[:len(result):len(result)] } // Concat creates a new slice concatenating slice with any additional slices. // Play: https://go.dev/play/p/gPt-q7zr5mk func Concat[T any](slice []T, slices ...[]T) []T { - result := append([]T{}, slice...) + totalLen := len(slice) for _, v := range slices { - result = append(result, v...) + totalLen += len(v) + } + + result := make([]T, 0, totalLen) + + result = append(result, slice...) + for _, s := range slices { + result = append(result, s...) } return result } -// Difference creates an slice of whose element in slice but not in comparedSlice. +// Difference creates a slice of whose element in slice but not in comparedSlice. // Play: https://go.dev/play/p/VXvadzLzhDa func Difference[T comparable](slice, comparedSlice []T) []T { result := []T{} @@ -755,21 +772,14 @@ func UpdateAt[T any](slice []T, index int, value T) []T { // Play: https://go.dev/play/p/AXw0R3ZTE6a func Unique[T comparable](slice []T) []T { result := []T{} - - for i := 0; i < len(slice); i++ { - v := slice[i] - skip := true - for j := range result { - if v == result[j] { - skip = false - break - } - } - if skip { - result = append(result, v) + exists := map[T]bool{} + for _, t := range slice { + if exists[t] { + continue } + exists[t] = true + result = append(result, t) } - return result } @@ -826,7 +836,11 @@ func UnionBy[T any, V comparable](predicate func(item T) V, slices ...[]T) []T { // Merge all given slices into one slice. // Play: https://go.dev/play/p/lbjFp784r9N func Merge[T any](slices ...[]T) []T { - result := make([]T, 0) + totalLen := 0 + for _, v := range slices { + totalLen += len(v) + } + result := make([]T, 0, totalLen) for _, v := range slices { result = append(result, v...) @@ -1173,7 +1187,7 @@ func AppendIfAbsent[T comparable](slice []T, item T) []T { // SetToDefaultIf sets elements to their default value if they match the given predicate. // It retains the positions of the elements in the slice. // It returns slice of T and the count of modified slice items -// Play: todo +// Play: https://go.dev/play/p/9AXGlPRC0-A func SetToDefaultIf[T any](slice []T, predicate func(T) bool) ([]T, int) { var count int for i := 0; i < len(slice); i++ { @@ -1239,6 +1253,30 @@ func Partition[T any](slice []T, predicates ...func(item T) bool) [][]T { return result } +// Breaks a list into two parts at the point where the predicate for the first time is true. +// Play: Todo +func Break[T any](values []T, predicate func(T) bool) ([]T, []T) { + a := make([]T, 0) + b := make([]T, 0) + if len(values) == 0 { + return a, b + } + matched := false + for _, value := range values { + + if !matched && predicate(value) { + matched = true + } + + if matched { + b = append(b, value) + } else { + a = append(a, value) + } + } + return a, b +} + // Random get a random item of slice, return idx=-1 when slice is empty // Play: https://go.dev/play/p/UzpGQptWppw func Random[T any](slice []T) (val T, idx int) { @@ -1249,3 +1287,35 @@ func Random[T any](slice []T) (val T, idx int) { idx = random.RandInt(0, len(slice)) return slice[idx], idx } + +// RightPadding adds padding to the right end of a slice. +// Play: Todo +func RightPadding[T any](slice []T, paddingValue T, paddingLength int) []T { + if paddingLength == 0 { + return slice + } + for i := 0; i < paddingLength; i++ { + slice = append(slice, paddingValue) + } + return slice +} + +// LeftPadding adds padding to the left begin of a slice. +// Play: Todo +func LeftPadding[T any](slice []T, paddingValue T, paddingLength int) []T { + if paddingLength == 0 { + return slice + } + + paddedSlice := make([]T, len(slice)+paddingLength) + i := 0 + for ; i < paddingLength; i++ { + paddedSlice[i] = paddingValue + } + for j := 0; j < len(slice); j++ { + paddedSlice[i] = slice[j] + i++ + } + + return paddedSlice +} diff --git a/slice/slice_example_test.go b/slice/slice_example_test.go index 037ea64f..f949e75f 100644 --- a/slice/slice_example_test.go +++ b/slice/slice_example_test.go @@ -1112,3 +1112,32 @@ func ExampleSetToDefaultIf() { // [ b c d ] // 3 } + +func ExampleBreak() { + nums := []int{1, 2, 3, 4, 5} + even := func(n int) bool { return n%2 == 0 } + + resultEven, resultAfterFirstEven := Break(nums, even) + fmt.Println(resultEven) + fmt.Println(resultAfterFirstEven) + + // Output: + // [1] + // [2 3 4 5] +} + +func ExampleRightPadding() { + nums := []int{1, 2, 3, 4, 5} + padded := RightPadding(nums, 0, 3) + fmt.Println(padded) + // Output: + // [1 2 3 4 5 0 0 0] +} + +func ExampleLeftPadding() { + nums := []int{1, 2, 3, 4, 5} + padded := LeftPadding(nums, 0, 3) + fmt.Println(padded) + // Output: + // [0 0 0 1 2 3 4 5] +} diff --git a/slice/slice_test.go b/slice/slice_test.go index 582a1568..fb2c4e2c 100644 --- a/slice/slice_test.go +++ b/slice/slice_test.go @@ -2,12 +2,11 @@ package slice import ( "fmt" + "github.com/duke-git/lancet/v2/internal" "math" "reflect" "strconv" "testing" - - "github.com/duke-git/lancet/v2/internal" ) func TestContain(t *testing.T) { @@ -109,6 +108,19 @@ func TestConcat(t *testing.T) { assert.Equal([]int{1, 2, 3, 4, 5}, Concat([]int{1, 2, 3}, []int{4}, []int{5})) } +func BenchmarkConcat(b *testing.B) { + slice1 := []int{1, 2, 3} + slice2 := []int{4, 5, 6} + slice3 := []int{7, 8, 9} + + for i := 0; i < b.N; i++ { + result := Concat(slice1, slice2, slice3) + if len(result) == 0 { + b.Fatal("unexpected empty result") + } + } +} + func TestEqual(t *testing.T) { t.Parallel() @@ -1357,3 +1369,54 @@ func TestSetToDefaultIf(t *testing.T) { assert.Equal(2, count) }) } + +func TestBreak(t *testing.T) { + t.Parallel() + + assert := internal.NewAssert(t, "TestBreak") + + // Test with integers + nums := []int{1, 2, 3, 4, 5} + even := func(n int) bool { return n%2 == 0 } + + resultEven, resultAfterFirstEven := Break(nums, even) + assert.Equal([]int{1}, resultEven) + assert.Equal([]int{2, 3, 4, 5}, resultAfterFirstEven) + + // Test with strings + strings := []string{"apple", "banana", "cherry", "date", "elderberry"} + startsWithA := func(s string) bool { return s[0] == 'a' } + + resultStartsWithA, resultAfterFirstStartsWithA := Break(strings, startsWithA) + assert.Equal([]string{}, resultStartsWithA) + assert.Equal([]string{"apple", "banana", "cherry", "date", "elderberry"}, resultAfterFirstStartsWithA) + + // Test with empty slice + emptySlice := []int{} + resultEmpty, _ := Break(emptySlice, even) + assert.Equal([]int{}, resultEmpty) + + // Test with all elements satisfying the predicate + allEven := []int{2, 4, 6, 8, 10} + emptyResult, resultAllEven := Break(allEven, even) + assert.Equal([]int{2, 4, 6, 8, 10}, resultAllEven) + assert.Equal([]int{}, emptyResult) + + // Test with no elements satisfying the predicate + allOdd := []int{1, 3, 5, 7, 9} + resultAllOdd, emptyResult := Break(allOdd, even) + assert.Equal([]int{1, 3, 5, 7, 9}, resultAllOdd) + assert.Equal([]int{}, emptyResult) +} + +func TestRightPaddingAndLeftPadding(t *testing.T) { + t.Parallel() + + assert := internal.NewAssert(t, "RightPaddingAndLeftPadding") + + // Test with integers + nums := []int{1, 2, 3, 4, 5} + + padded := LeftPadding(RightPadding(nums, 0, 3), 0, 3) + assert.Equal([]int{0, 0, 0, 1, 2, 3, 4, 5, 0, 0, 0}, padded) +} diff --git a/strutil/string.go b/strutil/string.go index cda730c6..72846f80 100644 --- a/strutil/string.go +++ b/strutil/string.go @@ -585,7 +585,7 @@ func RemoveWhiteSpace(str string, repalceAll bool) string { } // SubInBetween return substring between the start and end position(excluded) of source string. -// Play: todo +// Play: https://go.dev/play/p/EDbaRvjeNsv func SubInBetween(str string, start string, end string) string { if _, after, ok := strings.Cut(str, start); ok { if before, _, ok := strings.Cut(after, end); ok { @@ -599,7 +599,7 @@ func SubInBetween(str string, start string, end string) string { // HammingDistance calculates the Hamming distance between two strings. // The Hamming distance is the number of positions at which the corresponding symbols are different. // This func returns an error if the input strings are of unequal lengths. -// Play: todo +// Play: https://go.dev/play/p/glNdQEA9HUi func HammingDistance(a, b string) (int, error) { if len(a) != len(b) { return -1, errors.New("a length and b length are unequal") @@ -617,3 +617,24 @@ func HammingDistance(a, b string) (int, error) { return distance, nil } + +// Concat uses the strings.Builder to concatenate the input strings. +// - `length` is the expected length of the concatenated string. +// - if you are unsure about the length of the string to be concatenated, please pass 0 or a negative number. +func Concat(length int, str ...string) string { + if len(str) == 0 { + return "" + } + + sb := strings.Builder{} + if length <= 0 { + sb.Grow(len(str[0]) * len(str)) + } else { + sb.Grow(length) + } + + for _, s := range str { + sb.WriteString(s) + } + return sb.String() +} diff --git a/strutil/string_example_test.go b/strutil/string_example_test.go index a9bcb284..8eac765f 100644 --- a/strutil/string_example_test.go +++ b/strutil/string_example_test.go @@ -680,3 +680,17 @@ func ExampleHammingDistance() { // 3 // 1 } + +func ExampleConcat() { + result1 := Concat(12, "Hello", " ", "World", "!") + result2 := Concat(11, "Go", " ", "Language") + result3 := Concat(0, "An apple a ", "day,", "keeps the", " doctor away") + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // Hello World! + // Go Language + // An apple a day,keeps the doctor away +} diff --git a/strutil/string_test.go b/strutil/string_test.go index 3d94af06..6b2c8bef 100644 --- a/strutil/string_test.go +++ b/strutil/string_test.go @@ -561,6 +561,7 @@ func TestContainsAny(t *testing.T) { } func TestRemoveWhiteSpace(t *testing.T) { + t.Parallel() assert := internal.NewAssert(t, "TestRemoveWhiteSpace") str := " hello \r\n \t world" @@ -571,6 +572,7 @@ func TestRemoveWhiteSpace(t *testing.T) { } func TestSubInBetween(t *testing.T) { + t.Parallel() assert := internal.NewAssert(t, "TestSubInBetween") str := "abcde" @@ -583,6 +585,7 @@ func TestSubInBetween(t *testing.T) { } func TestHammingDistance(t *testing.T) { + t.Parallel() assert := internal.NewAssert(t, "HammingDistance") hd := func(a, b string) int { @@ -604,3 +607,16 @@ func TestHammingDistance(t *testing.T) { assert.Equal(0, hd("日本語", "日本語")) assert.Equal(3, hd("日本語", "語日本")) } + +func TestConcat(t *testing.T) { + t.Parallel() + assert := internal.NewAssert(t, "TestConcat") + + assert.Equal("", Concat(0)) + assert.Equal("a", Concat(1, "a")) + assert.Equal("ab", Concat(2, "a", "b")) + assert.Equal("abc", Concat(3, "a", "b", "c")) + assert.Equal("abc", Concat(3, "a", "", "b", "c", "")) + assert.Equal("你好,世界!", Concat(0, "你好", ",", "", "世界!", "")) + assert.Equal("Hello World!", Concat(0, "Hello", " Wo", "r", "ld!", "")) +} diff --git a/validator/validator.go b/validator/validator.go index bef71db4..18150b2e 100644 --- a/validator/validator.go +++ b/validator/validator.go @@ -8,6 +8,7 @@ import ( "encoding/json" "fmt" "net" + "net/mail" "net/url" "reflect" "regexp" @@ -24,7 +25,7 @@ var ( intStrMatcher *regexp.Regexp = regexp.MustCompile(`^[\+-]?\d+$`) urlMatcher *regexp.Regexp = regexp.MustCompile(`^((ftp|http|https?):\/\/)?(\S+(:\S*)?@)?((([1-9]\d?|1\d\d|2[01]\d|22[0-3])(\.(1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.([0-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(([a-zA-Z0-9]+([-\.][a-zA-Z0-9]+)*)|((www\.)?))?(([a-z\x{00a1}-\x{ffff}0-9]+-?-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.([a-z\x{00a1}-\x{ffff}]{2,}))?))(:(\d{1,5}))?((\/|\?|#)[^\s]*)?$`) dnsMatcher *regexp.Regexp = regexp.MustCompile(`^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$`) - emailMatcher *regexp.Regexp = regexp.MustCompile(`\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*`) + emailMatcher *regexp.Regexp = regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,4}$`) chineseMobileMatcher *regexp.Regexp = regexp.MustCompile(`^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$`) chineseIdMatcher *regexp.Regexp = regexp.MustCompile(`^(\d{17})([0-9]|X|x)$`) chineseMatcher *regexp.Regexp = regexp.MustCompile("[\u4e00-\u9fa5]") @@ -264,7 +265,10 @@ func IsDns(dns string) bool { // IsEmail check if the string is a email address. // Play: https://go.dev/play/p/Os9VaFlT33G func IsEmail(email string) bool { - return emailMatcher.MatchString(email) + _, err := mail.ParseAddress(email) + return err == nil + + // return emailMatcher.MatchString(email) } // IsChineseMobile check if the string is chinese mobile number. diff --git a/validator/validator_test.go b/validator/validator_test.go index 1dca20fc..a1b8d286 100644 --- a/validator/validator_test.go +++ b/validator/validator_test.go @@ -285,6 +285,7 @@ func TestIsEmail(t *testing.T) { assert := internal.NewAssert(t, "TestIsEmail") assert.Equal(true, IsEmail("abc@xyz.com")) + assert.Equal(false, IsEmail("@abc@xyz.com")) assert.Equal(false, IsEmail("a.b@@com")) }