1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-05 21:32:27 +08:00

Compare commits

...

66 Commits

Author SHA1 Message Date
dudaodong
b7e5d946f1 release v2.1.12 2022-12-15 16:55:20 +08:00
dudaodong
687db4ce79 update readme file 2022-12-15 16:44:30 +08:00
dudaodong
a9a4bb8841 update readme file 2022-12-15 16:42:22 +08:00
dudaodong
bc6cb5f61b fix code issue in doc 2022-12-15 16:38:50 +08:00
dudaodong
2c57266f8e test: update some test functions 2022-12-15 15:22:01 +08:00
dudaodong
57e49c9520 doc: update document for funcations CamelCase/KebabCase/UpperKebabCase/SnakeCase/UpperSnakeCase 2022-12-14 21:42:43 +08:00
dudaodong
985c9a5d9a refactor: refact CamelCase function 2022-12-14 21:27:29 +08:00
dudaodong
5cfb11f036 feat: fix and add SnakeCase/UpperSnakeCase 2022-12-14 21:17:30 +08:00
dudaodong
5b3d48a1e7 feat: fix and add KebabCase/UpperKebabCase 2022-12-14 21:09:22 +08:00
dudaodong
d0576e028f clean code 2022-12-13 19:52:37 +08:00
dudaodong
76bdec2b54 test: add cases for Capitalize 2022-12-13 16:16:16 +08:00
dudaodong
fa20aba3a7 fix: fix CamelCase function bug 2022-12-13 14:23:32 +08:00
dudaodong
7a4a429e23 test: add cases for Capitalize 2022-12-12 21:11:19 +08:00
dudaodong
a70ec6ad1e refactor: use constraints from golang.org/x/exp/constraints 2022-12-11 11:25:34 +08:00
dudaodong
e435fa271b doc: update doc for Comma 2022-12-10 21:03:16 +08:00
dudaodong
1533d00891 refactor: update Comma function 2022-12-10 20:59:27 +08:00
dudaodong
8b99641de0 test: remove print info in test function 2022-12-10 19:19:49 +08:00
dudaodong
251f899f18 fix: TestExecCommand failed in linux/macos 2022-12-10 19:12:19 +08:00
dudaodong
00407e5182 fix: fix lint issue 2022-12-10 19:09:18 +08:00
dudaodong
4e457ad672 change: change update and insert function in link datastruture 2022-12-10 17:45:29 +08:00
dudaodong
7d8d9c3543 fix: fix lint issue 2022-12-10 16:55:06 +08:00
dudaodong
1197e8d1b6 fix: fix lint issue 2022-12-10 16:53:41 +08:00
dudaodong
13bbe19ab2 fix: fix lint issue 2022-12-10 16:41:40 +08:00
dudaodong
2725575d2f doc: add doc for IsGBK' 2022-12-09 19:28:45 +08:00
dudaodong
037d2729ce doc: update doc for ExecCommand 2022-12-09 19:20:15 +08:00
dudaodong
09d98745b0 fix: fix ExecCommand bug in windows 2022-12-09 19:10:55 +08:00
dudaodong
af5cfe6da1 feat: add IsGBK validator 2022-12-09 17:36:59 +08:00
Mickls
d59259bbe0 feat: A more reasonable IndexOf function (#66) 2022-12-09 11:31:40 +08:00
dudaodong
3189628d54 fix: fix AesCfbDecrypt 2022-12-08 15:07:40 +08:00
dudaodong
62c5e251a5 remove website 2022-12-08 14:55:21 +08:00
dudaodong
6e6444c8c0 refator: clean code 2022-12-06 20:20:36 +08:00
dudaodong
dd613e98b2 refator: clean code 2022-12-06 20:00:41 +08:00
dudaodong
2d905ab03e fix: fix word misspelling 2022-12-04 11:25:02 +08:00
dudaodong
205fedb197 release v2.1.11 2022-12-04 11:11:05 +08:00
dudaodong
4c864da62d doc: update readme file 2022-12-04 11:10:19 +08:00
dudaodong
263ab7e316 clean code 2022-12-03 14:41:58 +08:00
dudaodong
809b7a53df doc: update docment for slice package 2022-12-03 14:33:15 +08:00
dudaodong
61c43daabb doc: add doc for Count and CountBy function 2022-12-03 14:20:37 +08:00
dudaodong
18914ee2cd feat: add deprecat IntSlice, InterfaceSlice and StringSlice 2022-12-03 14:00:44 +08:00
dudaodong
0a8058956f feat: add Count and CountBy function 2022-12-03 13:04:12 +08:00
dudaodong
a044da7d2f refactor: clean code 2022-12-03 12:58:25 +08:00
dudaodong
f8b785c4cb doc: add doc for Sort and SortBy 2022-12-02 15:47:16 +08:00
dudaodong
82c8a04c35 make SortByField function deprecate 2022-12-02 15:17:08 +08:00
dudaodong
280ecb5cef feat: add SortBy function for slice 2022-12-02 14:53:57 +08:00
dudaodong
ec27ad4c40 feat: add Sort function for slice 2022-12-01 22:46:56 +08:00
dudaodong
d66f92cd68 doc: update slice and convertor document 2022-12-01 11:43:10 +08:00
dudaodong
d8ed692651 refactor: code improvement, ToString, Contain, Compact 2022-12-01 11:38:25 +08:00
dudaodong
a16de97d1d refactor: code improvement, ToString, Contain, Compact 2022-12-01 11:38:14 +08:00
dudaodong
6f458e4367 doc: add doc for NewSetFromSlice 2022-11-30 12:34:56 +08:00
dudaodong
37c7508ad0 feat: add NewSetFromSlice function for set 2022-11-30 12:00:55 +08:00
dudaodong
acb5844b15 docs: add doc for AddIfNotExist and AddIfNotExistBy 2022-11-30 11:54:04 +08:00
dudaodong
76f4eeea16 docs: add doc for Merge and Repeat 2022-11-30 11:35:32 +08:00
dudaodong
5466a23019 refactor: fix bad code smell 2022-11-30 11:20:13 +08:00
dudaodong
5692982dd1 feat: add AddIfNotExistBy function for set 2022-11-29 23:59:22 +08:00
dudaodong
c39c8914fb refactor: fix bad code smell 2022-11-29 23:50:17 +08:00
dudaodong
29bdca1bd2 feat: add AddIfNotExist function for set 2022-11-29 23:39:29 +08:00
dudaodong
eb66d038ac feat: update iterator package 2022-11-29 13:58:48 +08:00
dudaodong
a99ada5ee1 feat: add iterator package 2022-11-28 19:24:25 +08:00
dudaodong
a87faf5453 feat: add Repeate function for slice 2022-11-27 21:43:38 +08:00
dudaodong
ab6fec2f69 refactor: fix bad code smell 2022-11-27 21:36:30 +08:00
dudaodong
7b290989f5 feat: add Merge function 2022-11-27 20:20:00 +08:00
dudaodong
5722c724e6 Merge branch 'main' into v2 2022-11-27 19:29:33 +08:00
dudaodong
f84584ca04 doc: add website link 2022-11-27 18:44:25 +08:00
dudaodong
80cbbdc787 docs: format index content 2022-11-26 17:37:09 +08:00
dudaodong
be148e07ba fix: fix chunk slice bug 2022-11-26 16:21:57 +08:00
dudaodong
d36ab5cc3a fix: go report issue ineffassign 2022-11-18 17:05:54 +08:00
85 changed files with 2634 additions and 957 deletions

View File

@@ -4,7 +4,7 @@
<br/>
![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf)
[![Release](https://img.shields.io/badge/release-2.1.10-green.svg)](https://github.com/duke-git/lancet/releases)
[![Release](https://img.shields.io/badge/release-2.1.12-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)
@@ -38,10 +38,10 @@ English | [简体中文](./README_zh-CN.md)
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.4. </b>
2. <b>For users who use version below go1.18, you should install v1.x.x. now latest v1 is v1.3.5. </b>
```go
go get github.com/duke-git/lancet@v1.3.3 // below go1.18, install latest version of v1.x.x
go get github.com/duke-git/lancet@v1.3.5 // below go1.18, install latest version of v1.x.x
```
## Usage
@@ -371,12 +371,11 @@ import "github.com/duke-git/lancet/v2/netutil"
- [SendRequest](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#SendRequest)
- [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)
- [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)
- [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)
- [HttpPatch<sup>Deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpPatch)
- [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)
- [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)
- [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)
### 14. Random package implements some basic functions to generate random int and string.
@@ -425,6 +424,7 @@ import "github.com/duke-git/lancet/v2/slice"
- [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)
- [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)
- [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)
@@ -441,21 +441,25 @@ import "github.com/duke-git/lancet/v2/slice"
- [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)
- [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)
- [InterfaceSlice](https://github.com/duke-git/lancet/blob/main/docs/slice.md#InterfaceSlice)
- [IntSlice<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/slice.md#IntSlice)
- [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)
- [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)
- [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)
- [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)
- [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)
- [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)
- [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)
- [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)
- [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)
@@ -483,12 +487,14 @@ import "github.com/duke-git/lancet/v2/strutil"
- [Capitalize](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Capitalize)
- [IsString](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#IsString)
- [KebabCase](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#KebabCase)
- [UpperKebabCase](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#UpperKebabCase)
- [LowerFirst](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#LowerFirst)
- [UpperFirst](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#UpperFirst)
- [PadEnd](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#PadEnd)
- [PadStart](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#PadStart)
- [Reverse](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Reverse)
- [SnakeCase](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#SnakeCase)
- [UpperSnakeCase](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#UpperSnakeCase)
- [SplitEx](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#SplitEx)
- [Wrap](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Wrap)
- [Unwrap](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Unwrap)
@@ -546,6 +552,7 @@ import "github.com/duke-git/lancet/v2/validator"
- [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)
- [IsZeroValue](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsZeroValue)
- [IsGBK](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsGBK)
### 20. xerror package implements helpers for errors.

View File

@@ -4,7 +4,7 @@
<br/>
![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf)
[![Release](https://img.shields.io/badge/release-2.1.10-green.svg)](https://github.com/duke-git/lancet/releases)
[![Release](https://img.shields.io/badge/release-2.1.12-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)
@@ -37,10 +37,10 @@
go get github.com/duke-git/lancet/v2 //安装v2最新版本v2.x.x
```
2. <b>使用 go1.18 以下版本的用户,必须安装 v1.x.x。目前最新的 v1 版本是 v1.3.4。</b>
2. <b>使用 go1.18 以下版本的用户,必须安装 v1.x.x。目前最新的 v1 版本是 v1.3.5。</b>
```go
go get github.com/duke-git/lancet@v1.3.3 // 使用go1.18以下版本, 必须安装v1.x.x版本
go get github.com/duke-git/lancet@v1.3.5 // 使用go1.18以下版本, 必须安装v1.x.x版本
```
## 用法
@@ -370,12 +370,11 @@ import "github.com/duke-git/lancet/v2/netutil"
- [SendRequest](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#SendRequest)
- [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)
- [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)
- [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)
- [HttpPatch<sup>Deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPatch)
- [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)
- [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)
- [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)
### 14. random 随机数生成器包,可以生成随机[]bytes, int, string。
@@ -424,6 +423,7 @@ import "github.com/duke-git/lancet/v2/slice"
- [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)
- [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)
- [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)
@@ -438,21 +438,25 @@ import "github.com/duke-git/lancet/v2/slice"
- [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)
- [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)
- [InterfaceSlice](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#InterfaceSlice)
- [IntSlice<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#IntSlice)
- [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)
- [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)
- [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)
- [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)
- [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)
- [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)
- [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)
- [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)
- [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)
@@ -480,12 +484,14 @@ import "github.com/duke-git/lancet/v2/strutil"
- [Capitalize](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Capitalize)
- [IsString](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#IsString)
- [KebabCase](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#KebabCase)
- [UpperKebabCase](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#UpperKebabCase)
- [LowerFirst](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#LowerFirst)
- [UpperFirst](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#UpperFirst)
- [PadEnd](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#PadEnd)
- [PadStart](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#PadStart)
- [Reverse](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Reverse)
- [SnakeCase](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#SnakeCase)
- [UpperSnakeCase](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#UpperSnakeCase)
- [SplitEx](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#SplitEx)
- [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)
@@ -543,6 +549,7 @@ import "github.com/duke-git/lancet/v2/validator"
- [IsUrl](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsUrl)
- [IsWeakPassword](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsWeakPassword)
- [IsZeroValue](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsZeroValue)
- [IsGBK](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsGBK)
### 20. xerror 包实现一些错误处理函数

View File

@@ -158,7 +158,7 @@ func (c *Channel) Bridge(ctx context.Context, chanStream <-chan <-chan any) <-ch
var stream <-chan any
select {
case maybeStream, ok := <-chanStream:
if ok == false {
if !ok {
return
}
stream = maybeStream

View File

@@ -90,34 +90,53 @@ func ToChannel[T any](array []T) <-chan T {
}
// 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 {
result := ""
if value == nil {
return result
return ""
}
v := reflect.ValueOf(value)
switch value.(type) {
case float32, float64:
result = strconv.FormatFloat(v.Float(), 'f', -1, 64)
return result
case int, int8, int16, int32, int64:
result = strconv.FormatInt(v.Int(), 10)
return result
case uint, uint8, uint16, uint32, uint64:
result = strconv.FormatUint(v.Uint(), 10)
return result
switch val := value.(type) {
case float32:
return strconv.FormatFloat(float64(val), 'f', -1, 32)
case float64:
return strconv.FormatFloat(val, 'f', -1, 64)
case int:
return strconv.FormatInt(int64(val), 10)
case int8:
return strconv.FormatInt(int64(val), 10)
case int16:
return strconv.FormatInt(int64(val), 10)
case int32:
return strconv.FormatInt(int64(val), 10)
case int64:
return strconv.FormatInt(val, 10)
case uint:
return strconv.FormatUint(uint64(val), 10)
case uint8:
return strconv.FormatUint(uint64(val), 10)
case uint16:
return strconv.FormatUint(uint64(val), 10)
case uint32:
return strconv.FormatUint(uint64(val), 10)
case uint64:
return strconv.FormatUint(val, 10)
case string:
result = v.String()
return result
return val
case []byte:
result = string(v.Bytes())
return result
return string(val)
default:
newValue, _ := json.Marshal(value)
result = string(newValue)
return result
b, err := json.Marshal(val)
if err != nil {
return ""
}
return string(b)
// todo: maybe we should't supprt other type conversion
// v := reflect.ValueOf(value)
// log.Panicf("Unsupported data type: %s ", v.String())
// return ""
}
}

View File

@@ -27,14 +27,9 @@ func TestToChannel(t *testing.T) {
assert := internal.NewAssert(t, "TestToChannel")
ch := ToChannel([]int{1, 2, 3})
val1, _ := <-ch
assert.Equal(1, val1)
val2, _ := <-ch
assert.Equal(2, val2)
val3, _ := <-ch
assert.Equal(3, val3)
assert.Equal(1, <-ch)
assert.Equal(2, <-ch)
assert.Equal(3, <-ch)
_, ok := <-ch
assert.Equal(false, ok)
@@ -137,9 +132,10 @@ func TestToString(t *testing.T) {
"", "",
"0", "1", "-1",
"123", "123", "123", "123", "123", "123", "123",
"12.3", "12.300000190734863",
"12.3", "12.3",
"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++ {
actual := ToString(cases[i])
@@ -253,6 +249,7 @@ func TestDecodeByte(t *testing.T) {
var obj string
byteData := []byte{6, 12, 0, 3, 97, 98, 99}
DecodeByte(byteData, &obj)
err := DecodeByte(byteData, &obj)
assert.IsNil(err)
assert.Equal("abc", obj)
}

View File

@@ -17,15 +17,19 @@ import (
// AesEcbEncrypt encrypt data with key use AES ECB algorithm
// len(key) should be 16, 24 or 32
func AesEcbEncrypt(data, key []byte) []byte {
cipher, _ := aes.NewCipher(generateAesKey(key))
length := (len(data) + aes.BlockSize) / aes.BlockSize
plain := make([]byte, length*aes.BlockSize)
copy(plain, data)
pad := byte(len(plain) - len(data))
for i := len(data); i < len(plain); i++ {
plain[i] = pad
}
encrypted := make([]byte, len(plain))
cipher, _ := aes.NewCipher(generateAesKey(key))
for bs, be := 0, cipher.BlockSize(); bs <= len(data); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
cipher.Encrypt(encrypted[bs:be], plain[bs:be])
}
@@ -108,27 +112,32 @@ func AesCfbEncrypt(data, key []byte) []byte {
encrypted := make([]byte, aes.BlockSize+len(data))
iv := encrypted[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(encrypted[aes.BlockSize:], data)
return encrypted
}
// AesCfbDecrypt decrypt data with key use AES CFB algorithm
// len(encrypted) should be great than 16, len(key) should be 16, 24 or 32
func AesCfbDecrypt(encrypted, key []byte) []byte {
block, _ := aes.NewCipher(key)
if len(encrypted) < aes.BlockSize {
panic("encrypted data is too short")
}
block, _ := aes.NewCipher(key)
iv := encrypted[:aes.BlockSize]
encrypted = encrypted[aes.BlockSize:]
stream := cipher.NewCFBDecrypter(block, iv)
stream.XORKeyStream(encrypted, encrypted)
return encrypted
}
@@ -139,6 +148,7 @@ func AesOfbEncrypt(data, key []byte) []byte {
if err != nil {
panic(err)
}
data = pkcs7Padding(data, aes.BlockSize)
encrypted := make([]byte, aes.BlockSize+len(data))
iv := encrypted[:aes.BlockSize]
@@ -148,6 +158,7 @@ func AesOfbEncrypt(data, key []byte) []byte {
stream := cipher.NewOFB(block, iv)
stream.XORKeyStream(encrypted[aes.BlockSize:], data)
return encrypted
}
@@ -170,5 +181,6 @@ func AesOfbDecrypt(data, key []byte) []byte {
mode.XORKeyStream(decrypted, data)
decrypted = pkcs7UnPadding(decrypted)
return decrypted
}

View File

@@ -15,7 +15,6 @@ import (
// DesEcbEncrypt encrypt data with key use DES ECB algorithm
// len(key) should be 8
func DesEcbEncrypt(data, key []byte) []byte {
cipher, _ := des.NewCipher(generateDesKey(key))
length := (len(data) + des.BlockSize) / des.BlockSize
plain := make([]byte, length*des.BlockSize)
copy(plain, data)
@@ -26,6 +25,8 @@ func DesEcbEncrypt(data, key []byte) []byte {
}
encrypted := make([]byte, len(plain))
cipher, _ := des.NewCipher(generateDesKey(key))
for bs, be := 0, cipher.BlockSize(); bs <= len(data); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
cipher.Encrypt(encrypted[bs:be], plain[bs:be])
}
@@ -59,6 +60,7 @@ func DesCbcEncrypt(data, key []byte) []byte {
encrypted := make([]byte, des.BlockSize+len(data))
iv := encrypted[:des.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}

View File

@@ -33,7 +33,11 @@ func GenerateRsaKey(keySize int, priKeyFile, pubKeyFile string) error {
if err != nil {
panic(err)
}
pem.Encode(file, &block)
err = pem.Encode(file, &block)
if err != nil {
return err
}
file.Close()
// public key
@@ -49,12 +53,16 @@ func GenerateRsaKey(keySize int, priKeyFile, pubKeyFile string) error {
Bytes: derpText,
}
//file,err = os.Create("rsa_public.pem")
file, err = os.Create(pubKeyFile)
if err != nil {
return err
}
pem.Encode(file, &block)
err = pem.Encode(file, &block)
if err != nil {
return err
}
file.Close()
return nil
@@ -72,7 +80,11 @@ func RsaEncrypt(data []byte, pubKeyFileName string) []byte {
}
defer file.Close()
buf := make([]byte, fileInfo.Size())
file.Read(buf)
_, err = file.Read(buf)
if err != nil {
panic(err)
}
block, _ := pem.Decode(buf)
@@ -101,7 +113,11 @@ func RsaDecrypt(data []byte, privateKeyFileName string) []byte {
}
buf := make([]byte, fileInfo.Size())
defer file.Close()
file.Read(buf)
_, err = file.Read(buf)
if err != nil {
panic(err)
}
block, _ := pem.Decode(buf)

View File

@@ -146,9 +146,9 @@ func (h *MaxHeap[T]) PrintStructure() {
lastNum := powerTwo(level - 1)
lastLen := lastNum + (lastNum - 1)
heapTree := make([][]string, level, level)
heapTree := make([][]string, level)
for i := 0; i < level; i++ {
heapTree[i] = make([]string, lastLen, lastLen)
heapTree[i] = make([]string, lastLen)
for j := 0; j < lastLen; j++ {
heapTree[i][j] = ""
}
@@ -169,9 +169,9 @@ func (h *MaxHeap[T]) PrintStructure() {
for n := 0; n < lastLen; n++ {
val := heapTree[m][n]
if val == "" {
fmt.Printf(" ")
fmt.Print(" ")
} else {
fmt.Printf(val)
fmt.Print(val)
}
}
fmt.Println()

View File

@@ -30,8 +30,6 @@ func TestMaxHeap_BuildMaxHeap(t *testing.T) {
assert.Equal(expected, heap.data)
assert.Equal(12, heap.Size())
heap.PrintStructure()
}
func TestMaxHeap_Push(t *testing.T) {

View File

@@ -1,13 +1,12 @@
package datastructure
import (
"errors"
"fmt"
"github.com/duke-git/lancet/v2/datastructure"
)
// DoublyLink is a linked list. Whose node has a generic Value, Pre pointer points to a previous node of the link, Next pointer points to a next node of the link.
// DoublyLink is a linked list. Whose node has a generic Value, Pre pointer points to a previous node of the dl, Next pointer points to a next node of the dl.
type DoublyLink[T any] struct {
Head *datastructure.LinkNode[T]
length int
@@ -19,30 +18,30 @@ func NewDoublyLink[T any]() *DoublyLink[T] {
}
// InsertAtHead insert value into doubly linklist at head index
func (link *DoublyLink[T]) InsertAtHead(value T) {
func (dl *DoublyLink[T]) InsertAtHead(value T) {
newNode := datastructure.NewLinkNode(value)
size := link.Size()
size := dl.Size()
if size == 0 {
link.Head = newNode
link.length++
dl.Head = newNode
dl.length++
return
}
newNode.Next = link.Head
newNode.Next = dl.Head
newNode.Pre = nil
link.Head.Pre = newNode
link.Head = newNode
dl.Head.Pre = newNode
dl.Head = newNode
link.length++
dl.length++
}
// InsertAtTail insert value into doubly linklist at tail index
func (link *DoublyLink[T]) InsertAtTail(value T) {
current := link.Head
func (dl *DoublyLink[T]) InsertAtTail(value T) {
current := dl.Head
if current == nil {
link.InsertAtHead(value)
dl.InsertAtHead(value)
return
}
@@ -55,28 +54,29 @@ func (link *DoublyLink[T]) InsertAtTail(value T) {
newNode.Pre = current
current.Next = newNode
link.length++
dl.length++
}
// InsertAt insert value into doubly linklist at index
func (link *DoublyLink[T]) InsertAt(index int, value T) error {
size := link.length
// param `index` should between [0, length], if index do not meet the conditions, do nothing
func (dl *DoublyLink[T]) InsertAt(index int, value T) {
size := dl.length
if index < 0 || index > size {
return errors.New("param index should between 0 and the length of doubly link.")
return
}
if index == 0 {
link.InsertAtHead(value)
return nil
dl.InsertAtHead(value)
return
}
if index == size {
link.InsertAtTail(value)
return nil
dl.InsertAtTail(value)
return
}
i := 0
current := link.Head
current := dl.Head
for current != nil {
if i == index-1 {
@@ -85,38 +85,36 @@ func (link *DoublyLink[T]) InsertAt(index int, value T) error {
newNode.Pre = current
current.Next = newNode
link.length++
dl.length++
return nil
return
}
i++
current = current.Next
}
return errors.New("doubly link list no exist")
}
// DeleteAtHead delete value in doubly linklist at head index
func (link *DoublyLink[T]) DeleteAtHead() error {
if link.Head == nil {
return errors.New("doubly link list no exist")
func (dl *DoublyLink[T]) DeleteAtHead() {
if dl.Head == nil {
return
}
current := link.Head
link.Head = current.Next
link.Head.Pre = nil
link.length--
return nil
current := dl.Head
dl.Head = current.Next
dl.Head.Pre = nil
dl.length--
}
// DeleteAtTail delete value in doubly linklist at tail index
func (link *DoublyLink[T]) DeleteAtTail() error {
if link.Head == nil {
return errors.New("doubly link list no exist")
// DeleteAtTail delete value in doubly linklist at tail
func (dl *DoublyLink[T]) DeleteAtTail() {
if dl.Head == nil {
return
}
current := link.Head
current := dl.Head
if current.Next == nil {
return link.DeleteAtHead()
dl.DeleteAtHead()
}
for current.Next.Next != nil {
@@ -124,45 +122,44 @@ func (link *DoublyLink[T]) DeleteAtTail() error {
}
current.Next = nil
link.length--
return nil
dl.length--
}
// DeleteAt delete value in doubly linklist at index
func (link *DoublyLink[T]) DeleteAt(index int) error {
if link.Head == nil {
return errors.New("doubly link list no exist")
// param `index` should be [0, len(DoublyLink)-1]
func (dl *DoublyLink[T]) DeleteAt(index int) {
if dl.Head == nil {
return
}
current := link.Head
current := dl.Head
if current.Next == nil || index == 0 {
return link.DeleteAtHead()
dl.DeleteAtHead()
}
if index == link.length-1 {
return link.DeleteAtTail()
if index == dl.length-1 {
dl.DeleteAtTail()
}
if index < 0 || index > link.length-1 {
return errors.New("param index should between 0 and link size -1.")
if index < 0 || index > dl.length-1 {
return
}
i := 0
for current != nil {
if i == index-1 {
current.Next = current.Next.Next
link.length--
return nil
dl.length--
return
}
i++
current = current.Next
}
return errors.New("delete error")
}
// Reverse the linked list
func (link *DoublyLink[T]) Reverse() {
current := link.Head
func (dl *DoublyLink[T]) Reverse() {
current := dl.Head
var temp *datastructure.LinkNode[T]
for current != nil {
@@ -173,20 +170,20 @@ func (link *DoublyLink[T]) Reverse() {
}
if temp != nil {
link.Head = temp.Pre
dl.Head = temp.Pre
}
}
// GetMiddleNode return node at middle index of linked list
func (link *DoublyLink[T]) GetMiddleNode() *datastructure.LinkNode[T] {
if link.Head == nil {
func (dl *DoublyLink[T]) GetMiddleNode() *datastructure.LinkNode[T] {
if dl.Head == nil {
return nil
}
if link.Head.Next == nil {
return link.Head
if dl.Head.Next == nil {
return dl.Head
}
fast := link.Head
slow := link.Head
fast := dl.Head
slow := dl.Head
for fast != nil {
fast = fast.Next
@@ -202,14 +199,14 @@ func (link *DoublyLink[T]) GetMiddleNode() *datastructure.LinkNode[T] {
}
// Size return the count of doubly linked list
func (link *DoublyLink[T]) Size() int {
return link.length
func (dl *DoublyLink[T]) Size() int {
return dl.length
}
// Values return slice of all doubly linklist node value
func (link *DoublyLink[T]) Values() []T {
func (dl *DoublyLink[T]) Values() []T {
result := []T{}
current := link.Head
current := dl.Head
for current != nil {
result = append(result, current.Value)
current = current.Next
@@ -218,8 +215,8 @@ func (link *DoublyLink[T]) Values() []T {
}
// Print all nodes info of a linked list
func (link *DoublyLink[T]) Print() {
current := link.Head
func (dl *DoublyLink[T]) Print() {
current := dl.Head
info := "[ "
for current != nil {
info += fmt.Sprintf("%+v, ", current)
@@ -229,13 +226,13 @@ func (link *DoublyLink[T]) Print() {
fmt.Println(info)
}
// IsEmpty checks if link is empty or not
func (link *DoublyLink[T]) IsEmpty() bool {
return link.length == 0
// IsEmpty checks if dl is empty or not
func (dl *DoublyLink[T]) IsEmpty() bool {
return dl.length == 0
}
// Clear all nodes in doubly linklist
func (link *DoublyLink[T]) Clear() {
link.Head = nil
link.length = 0
func (dl *DoublyLink[T]) Clear() {
dl.Head = nil
dl.length = 0
}

View File

@@ -41,29 +41,24 @@ func TestDoublyLink_InsertAt(t *testing.T) {
link := NewDoublyLink[int]()
err := link.InsertAt(1, 1)
assert.IsNotNil(err)
link.InsertAt(1, 1) //do nothing
link.InsertAt(0, 1)
link.InsertAt(1, 2)
link.InsertAt(2, 4)
link.InsertAt(2, 3)
link.Print()
expected := []int{1, 2, 3, 4}
values := link.Values()
assert.Equal(expected, values)
}
func TestDoublyLink_DeleteAtHead(t *testing.T) {
assert := internal.NewAssert(t, "TestDoublyLink_DeleteAtHead")
link := NewDoublyLink[int]()
err := link.DeleteAtHead()
assert.IsNotNil(err)
link.DeleteAtHead()
link.InsertAtTail(1)
link.InsertAtTail(2)
@@ -71,7 +66,6 @@ func TestDoublyLink_DeleteAtHead(t *testing.T) {
link.InsertAtTail(4)
link.DeleteAtHead()
link.Print()
expected := []int{2, 3, 4}
values := link.Values()
@@ -83,8 +77,7 @@ func TestDoublyLink_DeleteAtTail(t *testing.T) {
assert := internal.NewAssert(t, "TestDoublyLink_DeleteAtTail")
link := NewDoublyLink[int]()
err := link.DeleteAtTail()
assert.IsNotNil(err)
link.DeleteAtTail()
link.InsertAtTail(1)
link.InsertAtTail(2)
@@ -92,7 +85,6 @@ func TestDoublyLink_DeleteAtTail(t *testing.T) {
link.InsertAtTail(4)
link.DeleteAtTail()
link.Print()
expected := []int{1, 2, 3}
values := link.Values()
@@ -104,8 +96,7 @@ func TestDoublyLink_DeleteAt(t *testing.T) {
assert := internal.NewAssert(t, "TestDoublyLink_DeleteAt")
link := NewDoublyLink[int]()
err := link.DeleteAt(0)
assert.IsNotNil(err)
link.DeleteAt(0)
link.InsertAtTail(1)
link.InsertAtTail(2)
@@ -113,11 +104,7 @@ func TestDoublyLink_DeleteAt(t *testing.T) {
link.InsertAtTail(4)
link.InsertAtTail(5)
err = link.DeleteAt(5)
assert.IsNotNil(err)
err = link.DeleteAt(0)
assert.IsNil(err)
link.DeleteAt(0)
assert.Equal([]int{2, 3, 4, 5}, link.Values())
link.DeleteAt(3)

View File

@@ -1,14 +1,13 @@
package datastructure
import (
"errors"
"fmt"
"reflect"
"github.com/duke-git/lancet/v2/datastructure"
)
// SinglyLink is a linked list. Whose node has a Value generics and Next pointer points to a next node of the link.
// SinglyLink is a linked list. Whose node has a Value generics and Next pointer points to a next node of the sl.
type SinglyLink[T any] struct {
Head *datastructure.LinkNode[T]
length int
@@ -20,18 +19,18 @@ func NewSinglyLink[T any]() *SinglyLink[T] {
}
// InsertAtHead insert value into singly linklist at head index
func (link *SinglyLink[T]) InsertAtHead(value T) {
func (sl *SinglyLink[T]) InsertAtHead(value T) {
newNode := datastructure.NewLinkNode(value)
newNode.Next = link.Head
link.Head = newNode
link.length++
newNode.Next = sl.Head
sl.Head = newNode
sl.length++
}
// InsertAtTail insert value into singly linklist at tail index
func (link *SinglyLink[T]) InsertAtTail(value T) {
current := link.Head
func (sl *SinglyLink[T]) InsertAtTail(value T) {
current := sl.Head
if current == nil {
link.InsertAtHead(value)
sl.InsertAtHead(value)
return
}
@@ -43,65 +42,63 @@ func (link *SinglyLink[T]) InsertAtTail(value T) {
newNode.Next = nil
current.Next = newNode
link.length++
sl.length++
}
// InsertAt insert value into singly linklist at index
func (link *SinglyLink[T]) InsertAt(index int, value T) error {
size := link.length
// param `index` should between [0, len(SinglyLink)], if index do not meet the conditions, do nothing
func (sl *SinglyLink[T]) InsertAt(index int, value T) {
size := sl.length
if index < 0 || index > size {
return errors.New("param index should between 0 and the length of singly link.")
return
}
if index == 0 {
link.InsertAtHead(value)
return nil
sl.InsertAtHead(value)
return
}
if index == size {
link.InsertAtTail(value)
return nil
sl.InsertAtTail(value)
return
}
i := 0
current := link.Head
current := sl.Head
for current != nil {
if i == index-1 {
newNode := datastructure.NewLinkNode(value)
newNode.Next = current.Next
current.Next = newNode
link.length++
return nil
sl.length++
return
}
i++
current = current.Next
}
return errors.New("singly link list no exist")
}
// DeleteAtHead delete value in singly linklist at head index
func (link *SinglyLink[T]) DeleteAtHead() error {
if link.Head == nil {
return errors.New("singly link list no exist")
func (sl *SinglyLink[T]) DeleteAtHead() {
if sl.Head == nil {
return
}
current := link.Head
link.Head = current.Next
link.length--
return nil
current := sl.Head
sl.Head = current.Next
sl.length--
}
// DeleteAtTail delete value in singly linklist at tail index
func (link *SinglyLink[T]) DeleteAtTail() error {
if link.Head == nil {
return errors.New("singly link list no exist")
// DeleteAtTail delete value in singly linklist at tail
func (sl *SinglyLink[T]) DeleteAtTail() {
if sl.Head == nil {
return
}
current := link.Head
current := sl.Head
if current.Next == nil {
return link.DeleteAtHead()
sl.DeleteAtHead()
}
for current.Next.Next != nil {
@@ -109,68 +106,66 @@ func (link *SinglyLink[T]) DeleteAtTail() error {
}
current.Next = nil
link.length--
return nil
sl.length--
}
// DeleteAt delete value in singly linklist at index
func (link *SinglyLink[T]) DeleteAt(index int) error {
if link.Head == nil {
return errors.New("singly link list no exist")
// param `index` should be [0, len(SinglyLink)-1]
func (sl *SinglyLink[T]) DeleteAt(index int) {
if sl.Head == nil {
return
}
current := link.Head
current := sl.Head
if current.Next == nil || index == 0 {
return link.DeleteAtHead()
sl.DeleteAtHead()
}
if index == link.length-1 {
return link.DeleteAtTail()
if index == sl.length-1 {
sl.DeleteAtTail()
}
if index < 0 || index > link.length-1 {
return errors.New("param index should between 0 and link size -1.")
if index < 0 || index > sl.length-1 {
return
}
i := 0
for current != nil {
if i == index-1 {
current.Next = current.Next.Next
link.length--
return nil
sl.length--
return
}
i++
current = current.Next
}
return errors.New("delete error")
}
// DeleteValue delete value in singly linklist
func (link *SinglyLink[T]) DeleteValue(value T) {
if link.Head == nil {
func (sl *SinglyLink[T]) DeleteValue(value T) {
if sl.Head == nil {
return
}
dummyHead := datastructure.NewLinkNode(value)
dummyHead.Next = link.Head
dummyHead.Next = sl.Head
current := dummyHead
for current.Next != nil {
if reflect.DeepEqual(current.Next.Value, value) {
current.Next = current.Next.Next
link.length--
sl.length--
} else {
current = current.Next
}
}
link.Head = dummyHead.Next
sl.Head = dummyHead.Next
}
// Reverse the linked list
func (link *SinglyLink[T]) Reverse() {
func (sl *SinglyLink[T]) Reverse() {
var pre, next *datastructure.LinkNode[T]
current := link.Head
current := sl.Head
for current != nil {
next = current.Next
@@ -179,19 +174,19 @@ func (link *SinglyLink[T]) Reverse() {
current = next
}
link.Head = pre
sl.Head = pre
}
// GetMiddleNode return node at middle index of linked list
func (link *SinglyLink[T]) GetMiddleNode() *datastructure.LinkNode[T] {
if link.Head == nil {
func (sl *SinglyLink[T]) GetMiddleNode() *datastructure.LinkNode[T] {
if sl.Head == nil {
return nil
}
if link.Head.Next == nil {
return link.Head
if sl.Head.Next == nil {
return sl.Head
}
fast := link.Head
slow := link.Head
fast := sl.Head
slow := sl.Head
for fast != nil {
fast = fast.Next
@@ -207,14 +202,14 @@ func (link *SinglyLink[T]) GetMiddleNode() *datastructure.LinkNode[T] {
}
// Size return the count of singly linked list
func (link *SinglyLink[T]) Size() int {
return link.length
func (sl *SinglyLink[T]) Size() int {
return sl.length
}
// Values return slice of all singly linklist node value
func (link *SinglyLink[T]) Values() []T {
func (sl *SinglyLink[T]) Values() []T {
result := []T{}
current := link.Head
current := sl.Head
for current != nil {
result = append(result, current.Value)
current = current.Next
@@ -222,20 +217,20 @@ func (link *SinglyLink[T]) Values() []T {
return result
}
// IsEmpty checks if link is empty or not
func (link *SinglyLink[T]) IsEmpty() bool {
return link.length == 0
// IsEmpty checks if sl is empty or not
func (sl *SinglyLink[T]) IsEmpty() bool {
return sl.length == 0
}
// Clear all the node in singly linklist
func (link *SinglyLink[T]) Clear() {
link.Head = nil
link.length = 0
func (sl *SinglyLink[T]) Clear() {
sl.Head = nil
sl.length = 0
}
// Print all nodes info of a linked list
func (link *SinglyLink[T]) Print() {
current := link.Head
func (sl *SinglyLink[T]) Print() {
current := sl.Head
info := "[ "
for current != nil {
info += fmt.Sprintf("%+v, ", current)

View File

@@ -41,25 +41,12 @@ func TestSinglyLink_InsertAt(t *testing.T) {
link := NewSinglyLink[int]()
err := link.InsertAt(1, 1)
assert.IsNotNil(err)
link.InsertAt(1, 1) //do nothing
err = link.InsertAt(0, 1)
if err != nil {
t.FailNow()
}
err = link.InsertAt(1, 2)
if err != nil {
t.FailNow()
}
err = link.InsertAt(2, 4)
if err != nil {
t.FailNow()
}
err = link.InsertAt(2, 3)
if err != nil {
t.FailNow()
}
link.InsertAt(0, 1)
link.InsertAt(1, 2)
link.InsertAt(2, 4)
link.InsertAt(2, 3)
link.Print()
@@ -73,8 +60,8 @@ func TestSinglyLink_DeleteAtHead(t *testing.T) {
assert := internal.NewAssert(t, "TestSinglyLink_DeleteAtHead")
link := NewSinglyLink[int]()
err := link.DeleteAtHead()
assert.IsNotNil(err)
link.DeleteAtHead()
link.InsertAtTail(1)
link.InsertAtTail(2)
@@ -94,8 +81,6 @@ func TestSinglyLink_DeleteAtTail(t *testing.T) {
assert := internal.NewAssert(t, "TestSinglyLink_DeleteAtTail")
link := NewSinglyLink[int]()
err := link.DeleteAtTail()
assert.IsNotNil(err)
link.InsertAtTail(1)
link.InsertAtTail(2)
@@ -103,7 +88,6 @@ func TestSinglyLink_DeleteAtTail(t *testing.T) {
link.InsertAtTail(4)
link.DeleteAtTail()
link.Print()
expected := []int{1, 2, 3}
values := link.Values()
@@ -133,8 +117,6 @@ func TestSinglyLink_DeleteAt(t *testing.T) {
assert := internal.NewAssert(t, "TestSinglyLink_DeleteAt")
link := NewSinglyLink[int]()
err := link.DeleteAt(0)
assert.IsNotNil(err)
link.InsertAtTail(1)
link.InsertAtTail(2)
@@ -142,11 +124,7 @@ func TestSinglyLink_DeleteAt(t *testing.T) {
link.InsertAtTail(4)
link.InsertAtTail(5)
err = link.DeleteAt(5)
assert.IsNotNil(err)
err = link.DeleteAt(0)
assert.IsNil(err)
link.DeleteAt(0)
assert.Equal([]int{2, 3, 4, 5}, link.Values())
link.DeleteAt(3)
@@ -167,7 +145,6 @@ func TestSinglyLink_Reverse(t *testing.T) {
link.InsertAtTail(4)
link.Reverse()
link.Print()
assert.Equal([]int{4, 3, 2, 1}, link.Values())
}

View File

@@ -156,7 +156,7 @@ func (l *List[T]) DeleteAt(index int) {
return
}
if index == size-1 {
data = append(data[:index])
data = data[:index]
} else {
data = append(data[:index], data[index+1:]...)
}
@@ -174,7 +174,7 @@ func (l *List[T]) DeleteIf(f func(T) bool) int {
continue
}
if index == size-1 {
data = append(data[:index])
data = data[:index]
} else {
data = append(data[:index], data[index+1:]...)
index--
@@ -221,7 +221,7 @@ func (l *List[T]) IsEmpty() bool {
// Clear the data of list
func (l *List[T]) Clear() {
l.data = make([]T, 0, 0)
l.data = make([]T, 0)
}
// Clone return a copy of list
@@ -235,7 +235,7 @@ func (l *List[T]) Clone() *List[T] {
// Merge two list, return new list, don't change original list
func (l *List[T]) Merge(other *List[T]) *List[T] {
l1, l2 := len(l.data), len(other.data)
ml := NewList(make([]T, l1+l2, l1+l2))
ml := NewList(make([]T, l1+l2))
data := append([]T{}, append(l.data, other.data...)...)
ml.data = data
@@ -274,7 +274,7 @@ func (l *List[T]) Unique() {
data := l.data
size := len(data)
uniqueData := make([]T, 0, 0)
uniqueData := make([]T, 0)
for i := 0; i < size; i++ {
value := data[i]
skip := true
@@ -305,7 +305,7 @@ func (l *List[T]) Union(other *List[T]) *List[T] {
// Intersection creates a new list whose element both be contained in list l and other
func (l *List[T]) Intersection(other *List[T]) *List[T] {
result := NewList(make([]T, 0, 0))
result := NewList(make([]T, 0))
for _, v := range l.data {
if other.Contain(v) {

View File

@@ -18,8 +18,6 @@ func TestArrayQueue_Enqueue(t *testing.T) {
data := queue.Data()
size := queue.Size()
queue.Print()
assert.Equal(expected, data)
assert.Equal(3, size)
}
@@ -35,7 +33,6 @@ func TestArrayQueue_Dequeue(t *testing.T) {
val, ok := queue.Dequeue()
assert.Equal(true, ok)
queue.Print()
assert.Equal(1, val)
assert.Equal([]int{2, 3}, queue.Data())
}
@@ -50,8 +47,6 @@ func TestArrayQueue_Front(t *testing.T) {
val := queue.Front()
queue.Print()
assert.Equal(1, val)
assert.Equal([]int{1, 2, 3}, queue.Data())
}
@@ -66,8 +61,6 @@ func TestArrayQueue_Back(t *testing.T) {
val := queue.Back()
queue.Print()
assert.Equal(3, val)
assert.Equal([]int{1, 2, 3}, queue.Data())
}

View File

@@ -10,31 +10,43 @@ func TestCircularQueue_Enqueue(t *testing.T) {
assert := internal.NewAssert(t, "TestCircularQueue_Enqueue")
queue := NewCircularQueue[int](6)
queue.Enqueue(1)
queue.Enqueue(2)
queue.Enqueue(3)
queue.Enqueue(4)
queue.Enqueue(5)
queue.Print()
err := queue.Enqueue(1)
assert.IsNil(err)
err = queue.Enqueue(2)
assert.IsNil(err)
err = queue.Enqueue(3)
assert.IsNil(err)
err = queue.Enqueue(4)
assert.IsNil(err)
err = queue.Enqueue(5)
assert.IsNil(err)
assert.Equal([]int{1, 2, 3, 4, 5}, queue.Data())
assert.Equal(5, queue.Size())
err := queue.Enqueue(6)
err = queue.Enqueue(6)
assert.IsNotNil(err)
}
func TestCircularQueue_Dequeue(t *testing.T) {
assert := internal.NewAssert(t, "TestCircularQueue_DeQueue")
queue := NewCircularQueue[int](6)
queue := NewCircularQueue[int](4)
assert.Equal(true, queue.IsEmpty())
queue.Enqueue(1)
queue.Enqueue(2)
queue.Enqueue(3)
queue.Enqueue(4)
queue.Enqueue(5)
err := queue.Enqueue(1)
assert.IsNil(err)
err = queue.Enqueue(2)
assert.IsNil(err)
err = queue.Enqueue(3)
assert.IsNil(err)
val, err := queue.Dequeue()
assert.IsNil(err)
@@ -43,11 +55,7 @@ func TestCircularQueue_Dequeue(t *testing.T) {
assert.Equal(false, queue.IsFull())
val, _ = queue.Dequeue()
queue.Print()
assert.Equal(2, *val)
queue.Enqueue(6)
queue.Print()
assert.Equal(false, queue.IsFull())
}
@@ -55,55 +63,52 @@ func TestCircularQueue_Front(t *testing.T) {
assert := internal.NewAssert(t, "TestCircularQueue_Front")
queue := NewCircularQueue[int](6)
queue.Enqueue(1)
queue.Enqueue(2)
queue.Enqueue(3)
queue.Enqueue(4)
queue.Enqueue(5)
queue.Print()
err := queue.Enqueue(1)
assert.IsNil(err)
queue.Dequeue()
queue.Dequeue()
queue.Enqueue(6)
queue.Enqueue(7)
err = queue.Enqueue(2)
assert.IsNil(err)
queue.Print()
err = queue.Enqueue(3)
assert.IsNil(err)
val := queue.Front()
assert.Equal(3, val)
assert.Equal(5, queue.Size())
assert.IsNil(err)
assert.Equal(1, val)
assert.Equal(3, queue.Size())
}
func TestCircularQueue_Back(t *testing.T) {
assert := internal.NewAssert(t, "TestCircularQueue_Back")
queue := NewCircularQueue[int](6)
queue := NewCircularQueue[int](3)
assert.Equal(true, queue.IsEmpty())
queue.Enqueue(1)
queue.Enqueue(2)
queue.Enqueue(3)
queue.Enqueue(4)
queue.Enqueue(5)
err := queue.Enqueue(1)
assert.IsNil(err)
queue.Print()
assert.Equal(5, queue.Back())
err = queue.Enqueue(2)
assert.IsNil(err)
queue.Dequeue()
queue.Dequeue()
queue.Enqueue(6)
queue.Enqueue(7)
assert.Equal(2, queue.Back())
queue.Print()
assert.Equal(7, queue.Back())
val, _ := queue.Dequeue()
assert.Equal(1, *val)
err = queue.Enqueue(3)
assert.IsNil(err)
assert.Equal(3, queue.Back())
}
func TestCircularQueue_Contain(t *testing.T) {
assert := internal.NewAssert(t, "TestCircularQueue_Contain")
queue := NewCircularQueue[int](2)
queue.Enqueue(1)
err := queue.Enqueue(1)
assert.IsNil(err)
assert.Equal(true, queue.Contain(1))
assert.Equal(false, queue.Contain(2))
}
@@ -115,7 +120,9 @@ func TestCircularQueue_Clear(t *testing.T) {
assert.Equal(true, queue.IsEmpty())
assert.Equal(0, queue.Size())
queue.Enqueue(1)
err := queue.Enqueue(1)
assert.IsNil(err)
assert.Equal(false, queue.IsEmpty())
assert.Equal(1, queue.Size())
@@ -127,22 +134,12 @@ func TestCircularQueue_Clear(t *testing.T) {
func TestCircularQueue_Data(t *testing.T) {
assert := internal.NewAssert(t, "TestCircularQueue_Data")
queue := NewCircularQueue[int](6)
queue.Enqueue(1)
queue.Enqueue(2)
queue.Enqueue(3)
queue.Enqueue(4)
queue.Enqueue(5)
queue := NewCircularQueue[int](3)
err := queue.Enqueue(1)
assert.IsNil(err)
queue.Print()
assert.Equal([]int{1, 2, 3, 4, 5}, queue.Data())
queue.Dequeue()
queue.Dequeue()
queue.Enqueue(6)
queue.Enqueue(7)
queue.Print()
assert.Equal([]int{3, 4, 5, 6, 7}, queue.Data())
err = queue.Enqueue(2)
assert.IsNil(err)
assert.Equal([]int{1, 2}, queue.Data())
}

View File

@@ -14,8 +14,6 @@ func TestLinkedQueue_Enqueue(t *testing.T) {
queue.Enqueue(2)
queue.Enqueue(3)
queue.Print()
assert.Equal([]int{1, 2, 3}, queue.Data())
assert.Equal(3, queue.Size())
}

View File

@@ -23,19 +23,24 @@ func TestPriorityQueue_Enqueue(t *testing.T) {
assert := internal.NewAssert(t, "TestPriorityQueue_Enqueue")
comparator := &intComparator{}
pq := NewPriorityQueue[int](10, comparator)
pq := NewPriorityQueue[int](3, comparator)
assert.Equal(true, pq.IsEmpty())
assert.Equal(false, pq.IsFull())
for i := 1; i < 11; i++ {
pq.Enqueue(i)
}
err := pq.Enqueue(1)
assert.IsNil(err)
err = pq.Enqueue(2)
assert.IsNil(err)
err = pq.Enqueue(3)
assert.IsNil(err)
assert.Equal(true, pq.IsFull())
queueData := pq.Data()
assert.Equal([]int{10, 9, 6, 7, 8, 2, 5, 1, 4, 3}, queueData)
assert.Equal([]int{3, 1, 2}, queueData)
}
@@ -43,22 +48,23 @@ func TestPriorityQueue_Dequeue(t *testing.T) {
assert := internal.NewAssert(t, "TestPriorityQueue_Dequeue")
comparator := &intComparator{}
pq := NewPriorityQueue[int](10, comparator)
pq := NewPriorityQueue[int](3, comparator)
_, ok := pq.Dequeue()
assert.Equal(false, ok)
for i := 1; i < 11; i++ {
pq.Enqueue(i)
}
err := pq.Enqueue(1)
assert.IsNil(err)
assert.Equal(10, pq.Size())
err = pq.Enqueue(2)
assert.IsNil(err)
err = pq.Enqueue(3)
assert.IsNil(err)
assert.Equal(3, pq.Size())
val, ok := pq.Dequeue()
assert.Equal(true, ok)
assert.Equal(10, val)
assert.Equal([]int{9, 8, 6, 7, 3, 2, 5, 1, 4}, pq.Data())
assert.Equal(9, pq.Size())
assert.Equal(3, val)
}

View File

@@ -4,22 +4,59 @@ package datastructure
type Set[T comparable] map[T]struct{}
// 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.Add(values...)
set.Add(items...)
return set
}
// Add value to set
func (s Set[T]) Add(values ...T) {
for _, v := range values {
// NewSetFromSlice create a set from slice
func NewSetFromSlice[T comparable](items []T) Set[T] {
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{}{}
}
}
// Contain checks if set contains value or not
func (s Set[T]) Contain(value T) bool {
_, ok := s[value]
// 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.
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
}
@@ -41,9 +78,9 @@ func (s Set[T]) Clone() Set[T] {
return set
}
// Delete value of set
func (s Set[T]) Delete(values ...T) {
for _, v := range values {
// Delete item of set
func (s Set[T]) Delete(items ...T) {
for _, v := range items {
delete(s, v)
}
}
@@ -58,7 +95,7 @@ func (s Set[T]) Equal(other Set[T]) bool {
}
// 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 {
fn(v)
}
@@ -76,13 +113,13 @@ func (s Set[T]) Size() int {
// Values return all values of set
func (s Set[T]) Values() []T {
values := make([]T, 0, 0)
result := make([]T, 0, len(s))
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

View File

@@ -6,6 +6,19 @@ import (
"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) {
assert := internal.NewAssert(t, "TestSet_Add")
@@ -17,6 +30,38 @@ func TestSet_Add(t *testing.T) {
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) {
assert := internal.NewAssert(t, "TestSet_Contain")

View File

@@ -14,8 +14,6 @@ func TestLinkedStack_Push(t *testing.T) {
stack.Push(2)
stack.Push(3)
stack.Print()
expected := []int{3, 2, 1}
values := stack.Data()
size := stack.Size()

View File

@@ -27,8 +27,6 @@ func TestBSTree_Insert(t *testing.T) {
bstree.Insert(5)
bstree.Insert(2)
bstree.Insert(4)
bstree.Print()
}
func TestBSTree_PreOrderTraverse(t *testing.T) {
@@ -86,8 +84,6 @@ func TestBSTree_LevelOrderTraverse(t *testing.T) {
bstree.Insert(2)
bstree.Insert(4)
bstree.Print()
acturl := bstree.LevelOrderTraverse()
t.Log(acturl)
assert.Equal([]int{6, 5, 7, 2, 4}, acturl)
@@ -103,10 +99,8 @@ func TestBSTree_Delete(t *testing.T) {
bstree.Insert(2)
bstree.Insert(4)
bstree.Print()
bstree.Delete(4)
bstree.Print()
acturl1 := bstree.InOrderTraverse()
t.Log(acturl1)
assert.Equal([]int{2, 5, 6, 7}, acturl1)
@@ -129,8 +123,6 @@ func TestBSTree_Depth(t *testing.T) {
bstree.Insert(2)
bstree.Insert(4)
bstree.Print()
assert.Equal(bstree.Depth(), 4)
}
@@ -150,8 +142,6 @@ func TestBSTree_IsSubTree(t *testing.T) {
subTree.Insert(4)
subTree.Insert(6)
subTree.Print()
assert.Equal(true, superTree.HasSubTree(subTree))
assert.Equal(false, subTree.HasSubTree(superTree))
}

View File

@@ -38,35 +38,35 @@ func inOrderTraverse[T any](node *datastructure.TreeNode[T]) []T {
return data
}
func preOrderPrint[T any](node *datastructure.TreeNode[T]) {
if node == nil {
return
}
// func preOrderPrint[T any](node *datastructure.TreeNode[T]) {
// if node == nil {
// return
// }
fmt.Printf("%v, ", node.Value)
preOrderPrint(node.Left)
preOrderPrint(node.Right)
}
// fmt.Printf("%v, ", node.Value)
// preOrderPrint(node.Left)
// preOrderPrint(node.Right)
// }
func postOrderPrint[T any](node *datastructure.TreeNode[T]) {
if node == nil {
return
}
// func postOrderPrint[T any](node *datastructure.TreeNode[T]) {
// if node == nil {
// return
// }
preOrderPrint(node.Left)
preOrderPrint(node.Right)
fmt.Printf("%v, ", node.Value)
}
// postOrderPrint(node.Left)
// postOrderPrint(node.Right)
// fmt.Printf("%v, ", node.Value)
// }
func inOrderPrint[T any](node *datastructure.TreeNode[T]) {
if node == nil {
return
}
// func inOrderPrint[T any](node *datastructure.TreeNode[T]) {
// if node == nil {
// return
// }
inOrderPrint(node.Left)
fmt.Printf("%v, ", node.Value)
inOrderPrint(node.Right)
}
// inOrderPrint(node.Left)
// fmt.Printf("%v, ", node.Value)
// inOrderPrint(node.Right)
// }
func levelOrderTraverse[T any](root *datastructure.TreeNode[T], traversal *[]T) {
var q []*datastructure.TreeNode[T] // queue

View File

@@ -21,6 +21,7 @@ import (
<div STYLE="page-break-after: always;"></div>
## Index
- [BubbleSort](#BubbleSort)
- [InsertionSort](#InsertionSort)
- [SelectionSort](#SelectionSort)
@@ -31,7 +32,6 @@ import (
- [CountSort](#CountSort)
- [BinarySearch](#BinarySearch)
- [BinaryIterativeSearch](#BinaryIterativeSearch)
- [LinearSearch](#LinearSearch)
- [LRUCache](#LRUCache)

View File

@@ -29,7 +29,6 @@ import (
- [QuickSort](#QuickSort)
- [HeapSort](#HeapSort)
- [MergeSort](#MergeSort)
- [CountSort](#CountSort)
- [BinarySearch](#BinarySearch)
- [BinaryIterativeSearch](#BinaryIterativeSearch)

View File

@@ -25,7 +25,6 @@ import (
- [ToBytes](#ToBytes)
- [ToChar](#ToChar)
- [ToChannel](#ToChannel)
- [ToFloat](#ToFloat)
- [ToInt](#ToInt)
- [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>

View File

@@ -27,7 +27,6 @@ import (
- [ToBytes](#ToBytes)
- [ToChar](#ToChar)
- [ToChannel](#ToChannel)
- [ToFloat](#ToFloat)
- [ToInt](#ToInt)
- [ToJson](#ToJson)
@@ -401,7 +400,7 @@ func main() {
### <span id="ToString">ToString</span>
<p>将interface转成字符串</p>
<p>将值转换为字符串,对于数字、字符串、[]byte将转换为字符串。 对于其他类型(切片、映射、数组、结构)将调用 json.Marshal</p>
<b>函数签名:</b>

View File

@@ -47,7 +47,6 @@ import (
- [HmacSha1](#HmacSha1)
- [HmacSha256](#HmacSha256)
- [HmacSha512](#HmacSha512)
- [Md5String](#Md5String)
- [Md5File](#Md5File)
- [Sha1](#Sha1)

View File

@@ -33,7 +33,6 @@ import (
- [AesOfbDecrypt](#AesOfbDecrypt)
- [Base64StdEncode](#Base64StdEncode)
- [Base64StdDecode](#Base64StdDecode)
- [DesEcbEncrypt](#DesEcbEncrypt)
- [DesEcbDecrypt](#DesEcbDecrypt)
- [DesCbcEncrypt](#DesCbcEncrypt)
@@ -43,7 +42,6 @@ import (
- [DesCfbDecrypt](#DesCfbDecrypt)
- [DesOfbEncrypt](#DesOfbEncrypt)
- [DesOfbDecrypt](#DesOfbDecrypt)
- [HmacMd5](#HmacMd5)
- [HmacSha1](#HmacSha1)
- [HmacSha256](#HmacSha256)

View File

@@ -132,12 +132,12 @@ func main() {
### <span id="SinglyLink_InsertAt">InsertAt</span>
<p>Insert value into singly linklist at index, index shoud be great or equal 0 and less or equal number of link nodes</p>
<p>Insert value into singly linklist at index, param `index` should between [0, len(SinglyLink)], if index do not meet the conditions, do nothing</p>
<b>Signature:</b>
```go
func (link *SinglyLink[T]) InsertAt(index int, value T) error
func (link *SinglyLink[T]) InsertAt(index int, value T)
```
<b>Example:</b>
@@ -152,6 +152,8 @@ import (
func main() {
lk := link.NewSinglyLink[int]()
lk.InsertAt(1, 1) //do nothing
lk.InsertAt(0, 1)
lk.InsertAt(1, 2)
lk.InsertAt(2, 3)
@@ -228,12 +230,12 @@ func main() {
### <span id="SinglyLink_DeleteAt">DeleteAt</span>
<p>Delete value at specific index, index shoud be great or equal 0 and less or less than number of link nodes - 1</p>
<p>Delete value at specific index, param `index` should be [0, len(SinglyLink)-1]</p>
<b>Signature:</b>
```go
func (link *SinglyLink[T]) DeleteAt(index int) error
func (link *SinglyLink[T]) DeleteAt(index int)
```
<b>Example:</b>
@@ -253,9 +255,8 @@ func main() {
lk.InsertAtTail(3)
lk.InsertAtTail(4)
err := lk.DeleteAt(3)
lk.DeleteAt(3)
fmt.Println(err) //nil
fmt.Println(lk.Values()) //[]int{1, 2, 3}
}
```
@@ -268,7 +269,7 @@ func main() {
<b>Signature:</b>
```go
func (link *SinglyLink[T]) DeleteAtHead() error
func (link *SinglyLink[T]) DeleteAtHead()
```
<b>Example:</b>
@@ -288,9 +289,8 @@ func main() {
lk.InsertAtTail(3)
lk.InsertAtTail(4)
err := lk.DeleteAtHead()
lk.DeleteAtHead()
fmt.Println(err) //nil
fmt.Println(lk.Values()) //[]int{2, 3, 4}
}
```
@@ -304,7 +304,7 @@ func main() {
<b>Signature:</b>
```go
func (link *SinglyLink[T]) DeleteAtTail() error
func (link *SinglyLink[T]) DeleteAtTail()
```
<b>Example:</b>
@@ -323,9 +323,8 @@ func main() {
lk.InsertAtTail(2)
lk.InsertAtTail(3)
err := lk.DeleteAtTail()
lk.DeleteAtTail()
fmt.Println(err) //nil
fmt.Println(lk.Values()) //[]int{1, 2}
}
```
@@ -628,12 +627,12 @@ func main() {
### <span id="DoublyLink_InsertAt">InsertAt</span>
<p>Insert value into doubly linklist at index, index shoud be great or equal 0 and less or equal number of link nodes</p>
<p>Insert value into doubly linklist at index, param `index` should between [0, len(DoublyLink)], if index do not meet the conditions, do nothing</p>
<b>Signature:</b>
```go
func (link *DoublyLink[T]) InsertAt(index int, value T) error
func (link *DoublyLink[T]) InsertAt(index int, value T)
```
<b>Example:</b>
@@ -648,6 +647,8 @@ import (
func main() {
lk := link.NewDoublyLink[int]()
lk.InsertAt(1, 1) //do nothing
lk.InsertAt(0, 1)
lk.InsertAt(1, 2)
lk.InsertAt(2, 3)
@@ -724,12 +725,12 @@ func main() {
### <span id="DoublyLink_DeleteAt">DeleteAt</span>
<p>Delete value at specific index, index shoud be great or equal 0 and less or less than number of link nodes - 1</p>
<p>Delete value at specific index, param `index` should be [0, len(DoublyLink)-1]</p>
<b>Signature:</b>
```go
func (link *DoublyLink[T]) DeleteAt(index int) error
func (link *DoublyLink[T]) DeleteAt(index int)
```
<b>Example:</b>
@@ -749,9 +750,8 @@ func main() {
lk.InsertAtTail(3)
lk.InsertAtTail(4)
err := lk.DeleteAt(3)
lk.DeleteAt(3)
fmt.Println(err) //nil
fmt.Println(lk.Values()) //[]int{1, 2, 3}
}
```
@@ -764,7 +764,7 @@ func main() {
<b>Signature:</b>
```go
func (link *DoublyLink[T]) DeleteAtHead() error
func (link *DoublyLink[T]) DeleteAtHead()
```
<b>Example:</b>
@@ -784,9 +784,8 @@ func main() {
lk.InsertAtTail(3)
lk.InsertAtTail(4)
err := lk.DeleteAtHead()
lk.DeleteAtHead()
fmt.Println(err) //nil
fmt.Println(lk.Values()) //[]int{2, 3, 4}
}
```

View File

@@ -132,12 +132,12 @@ func main() {
### <span id="SinglyLink_InsertAt">InsertAt</span>
<p>将值插入到索引处的链表中,索引应大于或等于 0 且小于或等于链表节点数</p>
<p>将值插入到索引处的链表中,索引应大于或等于0且小于或等于链表节点数</p>
<b>函数签名:</b>
```go
func (link *SinglyLink[T]) InsertAt(index int, value T) error
func (link *SinglyLink[T]) InsertAt(index int, value T)
```
<b>例子:</b>
@@ -152,6 +152,8 @@ import (
func main() {
lk := link.NewSinglyLink[int]()
lk.InsertAt(1, 1) //do nothing
lk.InsertAt(0, 1)
lk.InsertAt(1, 2)
lk.InsertAt(2, 3)
@@ -228,12 +230,12 @@ func main() {
### <span id="SinglyLink_DeleteAt">DeleteAt</span>
<p>删除特定索引处的值索引应大于或等于0且小于或等于链接节点数 - 1</p>
<p>删除特定索引处的值索引应大于或等于0且小于或等于链接节点数-1</p>
<b>函数签名:</b>
```go
func (link *SinglyLink[T]) DeleteAt(index int) error
func (link *SinglyLink[T]) DeleteAt(index int)
```
<b>例子:</b>
@@ -253,9 +255,8 @@ func main() {
lk.InsertAtTail(3)
lk.InsertAtTail(4)
err := lk.DeleteAt(3)
lk.DeleteAt(3)
fmt.Println(err) //nil
fmt.Println(lk.Values()) //[]int{1, 2, 3}
}
```
@@ -268,7 +269,7 @@ func main() {
<b>函数签名:</b>
```go
func (link *SinglyLink[T]) DeleteAtHead() error
func (link *SinglyLink[T]) DeleteAtHead()
```
<b>例子:</b>
@@ -288,9 +289,8 @@ func main() {
lk.InsertAtTail(3)
lk.InsertAtTail(4)
err := lk.DeleteAtHead()
lk.DeleteAtHead()
fmt.Println(err) //nil
fmt.Println(lk.Values()) //[]int{2, 3, 4}
}
```
@@ -304,7 +304,7 @@ func main() {
<b>函数签名:</b>
```go
func (link *SinglyLink[T]) DeleteAtTail() error
func (link *SinglyLink[T]) DeleteAtTail()
```
<b>例子:</b>
@@ -323,9 +323,8 @@ func main() {
lk.InsertAtTail(2)
lk.InsertAtTail(3)
err := lk.DeleteAtTail()
lk.DeleteAtTail()
fmt.Println(err) //nil
fmt.Println(lk.Values()) //[]int{1, 2}
}
```
@@ -628,12 +627,12 @@ func main() {
### <span id="DoublyLink_InsertAt">InsertAt</span>
<p>将值插入到索引处的链表中,索引应大于或等于 0 且小于或等于链表节点数</p>
<p>将值插入到索引处的链表中,索引应大于或等于0且小于或等于链表节点数</p>
<b>函数签名:</b>
```go
func (link *DoublyLink[T]) InsertAt(index int, value T) error
func (link *DoublyLink[T]) InsertAt(index int, value T)
```
<b>例子:</b>
@@ -648,6 +647,8 @@ import (
func main() {
lk := link.NewDoublyLink[int]()
lk.InsertAt(1, 1) //do nothing
lk.InsertAt(0, 1)
lk.InsertAt(1, 2)
lk.InsertAt(2, 3)
@@ -724,12 +725,12 @@ func main() {
### <span id="DoublyLink_DeleteAt">DeleteAt</span>
<p>删除特定索引处的值索引应大于或等于0且小于或等于链接节点数 - 1</p>
<p>删除特定索引处的值索引应大于或等于0且小于或等于链接节点数-1</p>
<b>函数签名:</b>
```go
func (link *DoublyLink[T]) DeleteAt(index int) error
func (link *DoublyLink[T]) DeleteAt(index int)
```
<b>例子:</b>
@@ -749,9 +750,8 @@ func main() {
lk.InsertAtTail(3)
lk.InsertAtTail(4)
err := lk.DeleteAt(3)
lk.DeleteAt(3)
fmt.Println(err) //nil
fmt.Println(lk.Values()) //[]int{1, 2, 3}
}
```
@@ -764,7 +764,7 @@ func main() {
<b>函数签名:</b>
```go
func (link *DoublyLink[T]) DeleteAtHead() error
func (link *DoublyLink[T]) DeleteAtHead()
```
<b>例子:</b>
@@ -784,9 +784,8 @@ func main() {
lk.InsertAtTail(3)
lk.InsertAtTail(4)
err := lk.DeleteAtHead()
lk.DeleteAtHead()
fmt.Println(err) //nil
fmt.Println(lk.Values()) //[]int{2, 3, 4}
}
```
@@ -800,7 +799,7 @@ func main() {
<b>函数签名:</b>
```go
func (link *DoublyLink[T]) DeleteAtTail() error
func (link *DoublyLink[T]) DeleteAtTail()
```
<b>例子:</b>
@@ -819,9 +818,8 @@ func main() {
lk.InsertAtTail(2)
lk.InsertAtTail(3)
err := lk.DeleteAtTail()
lk.DeleteAtTail()
fmt.Println(err) //nil
fmt.Println(lk.Values()) //[]int{1, 2}
}
```

View File

@@ -22,8 +22,11 @@ import (
## Index
- [NewSet](#NewSet)
- [NewSetFromSlice](#NewSetFromSlice)
- [Values](#Values)
- [Add](#Add)
- [AddIfNotExist](#AddIfNotExist)
- [AddIfNotExistBy](#AddIfNotExistBy)
- [Delete](#Delete)
- [Contain](#Contain)
- [ContainAll](#ContainAll)
@@ -34,7 +37,6 @@ import (
- [IsEmpty](#IsEmpty)
- [Union](#Union)
- [Intersection](#Intersection)
- [SymmetricDifference](#SymmetricDifference)
- [Minus](#Minus)
@@ -45,13 +47,13 @@ import (
## Documentation
### <span id="NewSet">NewSet</span>
<p>Make a Set instance</p>
<p>Create a set instance</p>
<b>Signature:</b>
```go
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>
@@ -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>
@@ -100,12 +126,12 @@ func main() {
### <span id="Add">Add</span>
<p>Add value to set</p>
<p>Add items to set</p>
<b>Signature:</b>
```go
func (s Set[T]) Add(values ...T)
func (s Set[T]) Add(items ...T)
```
<b>Example:</b>
@@ -126,14 +152,83 @@ func main() {
```
### <span id="Delete">Delete</span>
<p>Delete value in set</p>
### <span id="AddIfNotExist">AddIfNotExist</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>
<b>Signature:</b>
```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>
@@ -157,12 +252,12 @@ func main() {
### <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>
```go
func (s Set[T]) Contain(value T) bool
func (s Set[T]) Contain(item T) bool
```
<b>Example:</b>
@@ -309,7 +404,7 @@ func main() {
<b>Signature:</b>
```go
func (s Set[T]) Iterate(fn func(value T))
func (s Set[T]) Iterate(fn func(item T))
```
<b>Example:</b>
@@ -324,8 +419,8 @@ import (
func main() {
set1 := set.NewSet(1, 2, 3)
arr := []int{}
set.Iterate(func(value int) {
arr = append(arr, value)
set.Iterate(func(item int) {
arr = append(arr, item)
})
fmt.Println(arr) //1,2,3

View File

@@ -22,8 +22,11 @@ import (
## 目录
- [NewSet](#NewSet)
- [NewSetFromSlice](#NewSetFromSlice)
- [Values](#Values)
- [Add](#Add)
- [AddIfNotExist](#AddIfNotExist)
- [AddIfNotExistBy](#AddIfNotExistBy)
- [Delete](#Delete)
- [Contain](#Contain)
- [ContainAll](#ContainAll)
@@ -34,7 +37,6 @@ import (
- [IsEmpty](#IsEmpty)
- [Union](#Union)
- [Intersection](#Intersection)
- [SymmetricDifference](#SymmetricDifference)
- [Minus](#Minus)
@@ -51,7 +53,7 @@ import (
```go
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>
@@ -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>
<p>获取集合中所有元素的切片</p>
@@ -105,7 +132,7 @@ func main() {
<b>函数签名:</b>
```go
func (s Set[T]) Add(values ...T)
func (s Set[T]) Add(items ...T)
```
<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>
<p>删除集合中元素</p>
@@ -133,7 +230,7 @@ func main() {
<b>函数签名:</b>
```go
func (s Set[T]) Delete(values ...T)
func (s Set[T]) Delete(items ...T)
```
<b>例子:</b>
@@ -162,7 +259,7 @@ func main() {
<b>函数签名:</b>
```go
func (s Set[T]) Contain(value T) bool
func (s Set[T]) Contain(item T) bool
```
<b>例子:</b>
@@ -309,7 +406,7 @@ func main() {
<b>函数签名:</b>
```go
func (s Set[T]) Iterate(fn func(value T))
func (s Set[T]) Iterate(fn func(item T))
```
<b>例子:</b>
@@ -324,8 +421,8 @@ import (
func main() {
set1 := set.NewSet(1, 2, 3)
arr := []int{}
set.Iterate(func(value int) {
arr = append(arr, value)
set.Iterate(func(item int) {
arr = append(arr, item)
})
fmt.Println(arr) //1,2,3
@@ -420,9 +517,6 @@ func main() {
```
### <span id="SymmetricDifference">SymmetricDifference</span>
<p>返回一个集合,其中元素在第一个集合或第二个集合中,且不同时存在于两个集合中</p>

View File

@@ -29,7 +29,6 @@ import (
- [PreOrderTraverse](#BSTree_PreOrderTraverse)
- [InOrderTraverse](#BSTree_InOrderTraverse)
- [PostOrderTraverse](#BSTree_PostOrderTraverse)
- [LevelOrderTraverse](#BSTree_LevelOrderTraverse)
- [Depth](#BSTree_Depth)
- [HasSubTree](#BSTree_HasSubTree)

View File

@@ -29,7 +29,6 @@ import (
- [PreOrderTraverse](#BSTree_PreOrderTraverse)
- [InOrderTraverse](#BSTree_InOrderTraverse)
- [PostOrderTraverse](#BSTree_PostOrderTraverse)
- [LevelOrderTraverse](#BSTree_LevelOrderTraverse)
- [Depth](#BSTree_Depth)
- [HasSubTree](#BSTree_HasSubTree)

View File

@@ -42,7 +42,6 @@ import (
- [GetNightTimestamp](#GetNightTimestamp)
- [FormatTimeToStr](#FormatTimeToStr)
- [FormatStrToTime](#FormatStrToTime)
- [NewUnixNow](#NewUnixNow)
- [NewUnix](#NewUnix)
- [NewFormat](#NewFormat)

View File

@@ -41,7 +41,6 @@ import (
- [GetNightTimestamp](#GetNightTimestamp)
- [FormatTimeToStr](#FormatTimeToStr)
- [FormatStrToTime](#FormatStrToTime)
- [NewUnixNow](#NewUnixNow)
- [NewUnix](#NewUnix)
- [NewFormat](#NewFormat)

View File

@@ -28,7 +28,6 @@ import (
- [IsExist](#IsExist)
- [IsLink](#IsLink)
- [IsDir](#IsDir)
- [ListFileNames](#ListFileNames)
- [RemoveFile](#RemoveFile)
- [ReadFileToString](#ReadFileToString)

View File

@@ -28,7 +28,6 @@ import (
- [IsExist](#IsExist)
- [IsLink](#IsLink)
- [IsDir](#IsDir)
- [ListFileNames](#ListFileNames)
- [RemoveFile](#RemoveFile)
- [ReadFileToString](#ReadFileToString)

View File

@@ -28,13 +28,12 @@ import (
### <span id="Comma">Comma</span>
<p>Add comma to number by every 3 numbers from right. ahead by symbol char.
Param should be number or numberic string.</p>
<p>Add comma to a number value by every 3 numbers from right to left. ahead by symbol char. if value is a invalid number string like "aa", return empty string.</p>
<b>Signature:</b>
```go
func Comma(v any, symbol string) string
func Comma[T constraints.Float | constraints.Integer | string](value T, symbol string) string
```
<b>Example:</b>

View File

@@ -28,12 +28,12 @@ import (
### <span id="Comma">Comma</span>
<p>用逗号每隔3位分割数字/字符串,签名添加符号。参数必须是数字或者可以转为数字的字符串</p>
<p>用逗号每隔3位分割数字/字符串,支持前缀添加符号。参数value必须是数字或者可以转为数字的字符串, 否则返回空字符串</p>
<b>函数签名:</b>
```go
func Comma(v any, symbol string) string
func Comma[T constraints.Float | constraints.Integer | string](value T, symbol string) string
```
<b>例子:</b>

View File

@@ -28,7 +28,6 @@ import (
- [MaxBy](#MaxBy)
- [Min](#Min)
- [MinBy](#MaxBy)
- [Percent](#Percent)
- [RoundToFloat](#RoundToFloat)
- [RoundToString](#RoundToString)
@@ -46,7 +45,7 @@ import (
<b>Signature:</b>
```go
func Average[T lancetconstraints.Number](numbers ...T) T
func Average[T constraints.Integer | constraints.Float](numbers ...T) T
```
<b>Example:</b>
@@ -158,7 +157,7 @@ func main() {
<b>Signature:</b>
```go
func Max[T lancetconstraints.Number](numbers ...T) T
func Max[T constraints.Integer | constraints.Float](numbers ...T) T
```
<b>Example:</b>
@@ -224,7 +223,7 @@ func main() {
<b>Signature:</b>
```go
func Min[T lancetconstraints.Number](numbers ...T) T
func Min[T constraints.Integer | constraints.Float](numbers ...T) T
```
<b>Example:</b>

View File

@@ -28,7 +28,6 @@ import (
- [MaxBy](#MaxBy)
- [Min](#Min)
- [MinBy](#MaxBy)
- [Percent](#Percent)
- [RoundToFloat](#RoundToFloat)
- [RoundToString](#RoundToString)
@@ -45,7 +44,7 @@ import (
<b>函数签名:</b>
```go
func Average[T lancetconstraints.Number](numbers ...T) T
func Average[T constraints.Integer | constraints.Float](numbers ...T) T
```
<b>例子:</b>
@@ -155,7 +154,7 @@ func main() {
<b>函数签名:</b>
```go
func Max[T lancetconstraints.Number](numbers ...T) T
func Max[T constraints.Integer | constraints.Float](numbers ...T) T
```
<b>例子:</b>
@@ -221,7 +220,7 @@ func main() {
<b>函数签名:</b>
```go
func Min[T lancetconstraints.Number](numbers ...T) T
func Min[T constraints.Integer | constraints.Float](numbers ...T) T
```
<b>例子:</b>

View File

@@ -25,7 +25,6 @@ import (
## Index
- [ConvertMapToQueryString](#ConvertMapToQueryString)
- [EncodeUrl](#EncodeUrl)
- [GetInternalIp](#GetInternalIp)
- [GetIps](#GetIps)
- [GetMacAddrs](#GetMacAddrs)
@@ -38,7 +37,6 @@ import (
- [SendRequest](#SendRequest)
- [DecodeResponse](#DecodeResponse)
- [StructToUrlValues](#StructToUrlValues)
- [HttpGet<sup>Deprecated</sup>](#HttpGet)
- [HttpDelete<sup>Deprecated</sup>](#HttpDelete)
- [HttpPost<sup>Deprecated</sup>](#HttpPost)

View File

@@ -30,7 +30,6 @@ import (
- [GetMacAddrs](#GetMacAddrs)
- [GetPublicIpInfo](#GetPublicIpInfo)
- [GetRequestPublicIp](#GetRequestPublicIp)
- [IsPublicIP](#IsPublicIP)
- [IsInternalIP](#IsInternalIP)
- [HttpRequest](#HttpRequest)
@@ -38,13 +37,11 @@ import (
- [SendRequest](#SendRequest)
- [DecodeResponse](#DecodeResponse)
- [StructToUrlValues](#StructToUrlValues)
- [HttpGet<sup>Deprecated</sup>](#HttpGet)
- [HttpDelete<sup>Deprecated</sup>](#HttpDelete)
- [HttpPost<sup>Deprecated</sup>](#HttpPost)
- [HttpPut<sup>Deprecated</sup>](#HttpPut)
- [HttpPatch<sup>Deprecated</sup>](#HttpPatch)
- [ParseHttpResponse](#ParseHttpResponse)
<div STYLE="page-break-after: always;"></div>

View File

@@ -26,7 +26,6 @@ import (
- [RandInt](#RandInt)
- [RandString](#RandString)
- [RandUpper](#RandUpper)
- [RandLower](#RandLower)
- [RandNumeral](#RandNumeral)
- [RandNumeralOrLetter](#RandNumeralOrLetter)

View File

@@ -26,7 +26,6 @@ import (
- [RandInt](#RandInt)
- [RandString](#RandString)
- [RandUpper](#RandUpper)
- [RandLower](#RandLower)
- [RandNumeral](#RandNumeral)
- [RandNumeralOrLetter](#RandNumeralOrLetter)

View File

@@ -29,6 +29,7 @@ import (
- [Compact](#Compact)
- [Concat](#Concat)
- [Count](#Count)
- [CountBy](#CountBy)
- [Difference](#Difference)
- [DifferenceBy](#DifferenceBy)
- [DifferenceWith](#DifferenceWith)
@@ -43,24 +44,27 @@ import (
- [Flatten](#Flatten)
- [FlattenDeep](#FlattenDeep)
- [ForEach](#ForEach)
- [GroupBy](#GroupBy)
- [GroupWith](#GroupWith)
- [IntSlice](#IntSlice)
- [InterfaceSlice](#InterfaceSlice)
- [IntSlice<sup>deprecated</sup>](#IntSlice)
- [InterfaceSlice<sup>deprecated</sup>](#InterfaceSlice)
- [Intersection](#Intersection)
- [InsertAt](#InsertAt)
- [IndexOf](#IndexOf)
- [LastIndexOf](#LastIndexOf)
- [Map](#Map)
- [Merge](#Merge)
- [Reverse](#Reverse)
- [Reduce](#Reduce)
- [Replace](#Replace)
- [ReplaceAll](#ReplaceAll)
- [Repeat](#Repeat)
- [Shuffle](#Shuffle)
- [SortByField](#SortByField)
- [Sort](#Sort)
- [SortBy](#SortBy)
- [SortByField<sup>deprecated</sup>](#SortByField)
- [Some](#Some)
- [StringSlice](#StringSlice)
- [StringSlice<sup>deprecated</sup>](#StringSlice)
- [SymmetricDifference](#SymmetricDifference)
- [ToSlice](#ToSlice)
- [ToSlicePointer](#ToSlicePointer)
@@ -78,12 +82,12 @@ import (
### <span id="AppendIfAbsent">AppendIfAbsent</span>
<p>If slice doesn't contain the value, append it to the slice.</p>
<p>If slice doesn't contain the item, append it to the slice.</p>
<b>Signature:</b>
```go
func AppendIfAbsent[T comparable](slice []T, value T) []T
func AppendIfAbsent[T comparable](slice []T, item T) []T
```
<b>Example:</b>
@@ -106,12 +110,12 @@ func main() {
### <span id="Contain">Contain</span>
<p>Check if the value is in the slice or not.</p>
<p>Check if the target value is in the slice or not.</p>
<b>Signature:</b>
```go
func Contain[T comparable](slice []T, value T) bool
func Contain[T comparable](slice []T, target T) bool
```
<b>Example:</b>
@@ -135,7 +139,7 @@ func main() {
<b>Signature:</b>
```go
func ContainSubSlice[T comparable](slice, subslice []T) bool
func ContainSubSlice[T comparable](slice, subSlice []T) bool
```
<b>Example:</b>
@@ -172,8 +176,8 @@ import (
func main() {
arr := []string{"a", "b", "c", "d", "e"}
res := slice.Chunk(InterfaceSlice(arr), 3)
fmt.Println(res) //[][]any{{"a", "b", "c"}, {"d", "e"}}
res := slice.Chunk((arr), 3)
fmt.Println(res) //[][]string{{"a", "b", "c"}, {"d", "e"}}
}
```
@@ -184,7 +188,7 @@ func main() {
<b>Signature:</b>
```go
func Compact[T any](slice []T) []T
func Compact[T comparable](slice []T) []T
```
<b>Example:</b>
@@ -203,12 +207,12 @@ func main() {
### <span id="Concat">Concat</span>
<p>Creates a new slice concatenating slice with any additional slices and/or values.</p>
<p>Creates a new slice concatenating slice with any additional slices.</p>
<b>Signature:</b>
```go
func Concat[T any](slice []T, values ...[]T) []T
func Concat[T any](slice []T, slices ...[]T) []T
```
<b>Example:</b>
@@ -230,12 +234,38 @@ func main() {
### <span id="Count">Count</span>
<p>Count iterates over elements of slice, returns a count of all matched elements.</p>
<p>Returns the number of occurrences of the given item in the slice.</p>
<b>Signature:</b>
```go
func Count[T any](slice []T, predicate func(index int, t T) bool) int
func Count[T comparable](slice []T, item T) int
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
nums := []int{1, 2, 3, 3, 4, 5}
fmt.Println(slice.Count(nums, 1)) //1
fmt.Println(slice.Count(nums, 3)) //2
}
```
### <span id="CountBy">CountBy</span>
<p>Iterates over elements of slice with predicate function, returns the number of all matched elements.</p>
<b>Signature:</b>
```go
func CountBy[T any](slice []T, predicate func(index int, item T) bool) int
```
<b>Example:</b>
@@ -248,15 +278,16 @@ import (
func main() {
nums := []int{1, 2, 3, 4, 5, 6}
evenFunc := func(i, num int) bool {
evenFunc := func(_, num int) bool {
return (num % 2) == 0
}
res := slice.Count(nums, evenFunc)
res := slice.CountBy(nums, evenFunc)
fmt.Println(res) //3
}
```
### <span id="Difference">Difference</span>
<p>Creates an slice of whose element not included in the other given slice.</p>
@@ -715,11 +746,12 @@ func main() {
return math.Floor(num)
}
res := slice.GroupWith(nums, floor)
fmt.Println(res) //map[float64][]float64{ 4: {4.2}, 6: {6.1, 6.3},}
}
```
### <span id="IntSlice">IntSlice</span>
### <span id="IntSlice">IntSlice (Deprecated: use generic feature of go1.18+ for replacement)</span>
<p>Convert interface slice to int slice.</p>
@@ -744,7 +776,7 @@ func main() {
}
```
### <span id="InterfaceSlice">InterfaceSlice</span>
### <span id="InterfaceSlice">InterfaceSlice (Deprecated: use generic feature of go1.18+ for replacement)</span>
<p>Convert value to interface slice.</p>
@@ -827,12 +859,12 @@ func main() {
### <span id="IndexOf">IndexOf</span>
<p>Returns the index at which the first occurrence of a value is found in a slice or return -1 if the value cannot be found.</p>
<p>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.</p>
<b>Signature:</b>
```go
func IndexOf[T comparable](slice []T, value T) int
func IndexOf[T comparable](slice []T, item T) int
```
<b>Example:</b>
@@ -855,12 +887,12 @@ func main() {
### <span id="LastIndexOf">LastIndexOf</span>
<p>Returns the index at which the last occurrence of a value is found in a slice or return -1 if the value cannot be found.</p>
<p>Returns the index at which the last occurrence of a item is found in a slice or return -1 if the item cannot be found.</p>
<b>Signature:</b>
```go
func LastIndexOf[T comparable](slice []T, value T) int
func LastIndexOf[T comparable](slice []T, item T) int
```
<b>Example:</b>
@@ -909,6 +941,36 @@ func main() {
}
```
### <span id="Merge">Merge</span>
<p>Merge all given slices into one slice.</p>
<b>Signature:</b>
```go
func Merge[T any](slices ...[]T) []T
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
s1 := []int{1, 2, 3}
s2 := []int{2, 4}
res := slice.Merge(s1, s2)
fmt.Println(res) //[]int{1, 2, 3, 2, 4}
}
```
### <span id="Reverse">Reverse</span>
<p>Reverse the elements order in slice.</p>
@@ -1018,6 +1080,31 @@ func main() {
}
```
### <span id="Repeat">Repeat</span>
<p>Creates a slice with length n whose elements are passed param item.</p>
<b>Signature:</b>
```go
func Repeat[T any](item T, n int) []T
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
fmt.Println(slice.Repeat("a", 3)) //[]string{"a", "a", "a"}
}
```
### <span id="Shuffle">Shuffle</span>
<p>Creates an slice of shuffled values.</p>
@@ -1043,7 +1130,94 @@ func main() {
}
```
### <span id="SortByField">SortByField</span>
### <span id="Sort">Sort</span>
<p>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`. Ordered type: number(all ints uints floats) or string.
</p>
<b>Signature:</b>
```go
func Sort[T constraints.Ordered](slice []T, sortOrder ...string)
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
numbers := []int{1, 4, 3, 2, 5}
slice.Sort(numbers)
fmt.Println(numbers) //{1,2,3,4,5}
slice.Sort(numbers, "desc")
fmt.Println(numbers) //{5,4,3,2,1}
strings := []string{"a", "d", "c", "b", "e"}
slice.Sort(strings)
fmt.Println(strings) //{"a", "b", "c", "d", "e"}
slice.Sort(strings, "desc")
fmt.Println(strings) //{"e", "d", "c", "b", "a"}
}
```
### <span id="SortBy">SortBy</span>
<p>Sorts the slice in ascending order as determined by the less function. This sort is not guaranteed to be stable.<p>
<b>Signature:</b>
```go
func SortBy[T any](slice []T, less func(a, b T) bool)
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
numbers := []int{1, 4, 3, 2, 5}
slice.SortBy(numbers, func(a, b int) bool {
return a < b
})
fmt.Println(numbers) //{1, 2, 3, 4, 5}
type User struct {
Name string
Age uint
}
users := []User{
{Name: "a", Age: 21},
{Name: "b", Age: 15},
{Name: "c", Age: 100}}
slice.SortBy(users, func(a, b User) bool {
return a.Age < b.Age
})
fmt.Printf("sort users by age: %v", users)
// output
// [{b 15} {a 21} {c 100}]
}
```
### <span id="SortByField">SortByField (Deprecated: use Sort and SortBy for replacement)</span>
<p>Sort struct slice by field. Slice element should be struct, field type should be int, uint, string, or bool. Default sort type is ascending (asc), if descending order, set sortType to desc</p>
@@ -1115,7 +1289,7 @@ func main() {
}
```
### <span id="StringSlice">StringSlice</span>
### <span id="StringSlice">StringSlice (Deprecated: use generic feature of go1.18+ for replacement)</span>
<p>Convert interface slice to string slice.</p>
@@ -1171,12 +1345,12 @@ func main() {
### <span id="ToSlice">ToSlice</span>
<p>Returns a slices of a variable parameter transformation</p>
<p>Creates a slice of give items.</p>
<b>Signature:</b>
```go
func ToSlice[T any](value ...T) []T
func ToSlice[T any](items ...T) []T
```
<b>Example:</b>
@@ -1200,7 +1374,7 @@ func main() {
<b>Signature:</b>
```go
func ToSlicePointer[T any](value ...T) []*T
func ToSlicePointer[T any](items ...T) []*T
```
<b>Example:</b>
@@ -1351,12 +1525,12 @@ func main() {
### <span id="Without">Without</span>
<p>Creates a slice excluding all given values. </p>
<p>Creates a slice excluding all given items. </p>
<b>Signature:</b>
```go
func Without[T comparable](slice []T, values ...T) []T
func Without[T comparable](slice []T, items ...T) []T
```
<b>Example:</b>

View File

@@ -20,8 +20,10 @@ import (
<div STYLE="page-break-after: always;"></div>
## 目录
- [AppendIfAbsent](#AppendIfAbsent)
- [Contain](#Contain)
- [ContainSubSlice](#ContainSubSlice)
@@ -29,6 +31,7 @@ import (
- [Compact](#Compact)
- [Concat](#Concat)
- [Count](#Count)
- [CountBy](#CountBy)
- [Difference](#Difference)
- [DifferenceBy](#DifferenceBy)
- [DifferenceWith](#DifferenceWith)
@@ -45,22 +48,25 @@ import (
- [ForEach](#ForEach)
- [GroupBy](#GroupBy)
- [GroupWith](#GroupWith)
- [IntSlice](#IntSlice)
- [InterfaceSlice](#InterfaceSlice)
- [IntSlice<sup>deprecated</sup>](#IntSlice)
- [InterfaceSlice<sup>deprecated</sup>](#InterfaceSlice)
- [Intersection](#Intersection)
- [InsertAt](#InsertAt)
- [IndexOf](#IndexOf)
- [LastIndexOf](#LastIndexOf)
- [Map](#Map)
- [Merge](#Merge)
- [Reverse](#Reverse)
- [Reduce](#Reduce)
- [Replace](#Replace)
- [ReplaceAll](#ReplaceAll)
- [Repeat](#Repeat)
- [Shuffle](#Shuffle)
- [SortByField](#SortByField)
- [Sort](#Sort)
- [SortBy](#SortBy)
- [SortByField<sup>deprecated</sup>](#SortByField)
- [Some](#Some)
- [StringSlice](#StringSlice)
- [StringSlice<sup>deprecated</sup>](#StringSlice)
- [SymmetricDifference](#SymmetricDifference)
- [ToSlice](#ToSlice)
- [ToSlicePointer](#ToSlicePointer)
@@ -72,6 +78,7 @@ import (
- [Without](#Without)
- [KeyBy](#KeyBy)
<div STYLE="page-break-after: always;"></div>
## 文档
@@ -83,7 +90,7 @@ import (
<b>函数签名:</b>
```go
func AppendIfAbsent[T comparable](slice []T, value T) []T
func AppendIfAbsent[T comparable](slice []T, item T) []T
```
<b>例子:</b>
@@ -111,7 +118,7 @@ func main() {
<b>函数签名:</b>
```go
func Contain[T comparable](slice []T, value T) bool
func Contain[T comparable](slice []T, target T) bool
```
<b>例子:</b>
@@ -135,7 +142,7 @@ func main() {
<b>函数签名:</b>
```go
func ContainSubSlice[T comparable](slice, subslice []T) bool
func ContainSubSlice[T comparable](slice, subSlice []T) bool
```
<b>例子:</b>
@@ -172,8 +179,8 @@ import (
func main() {
arr := []string{"a", "b", "c", "d", "e"}
res := slice.Chunk(InterfaceSlice(arr), 3)
fmt.Println(res) //[][]any{{"a", "b", "c"}, {"d", "e"}}
res := slice.Chunk(arr, 3)
fmt.Println(res) //[][]string{{"a", "b", "c"}, {"d", "e"}}
}
```
@@ -184,7 +191,7 @@ func main() {
<b>函数签名:</b>
```go
func Compact[T any](slice []T) []T
func Compact[T comparable](slice []T) []T
```
<b>例子:</b>
@@ -203,12 +210,12 @@ func main() {
### <span id="Concat">Concat</span>
<p>连接values到slice中values类型可以是切片或多个值</p>
<p>合并多个slices到slice中</p>
<b>函数签名:</b>
```go
func Concat[T any](slice []T, values ...[]T) []T
func Concat[T any](slice []T, slices ...[]T) []T
```
<b>例子:</b>
@@ -230,12 +237,38 @@ func main() {
### <span id="Count">Count</span>
<p>遍历切片对每个元素执行函数function. 返回符合函数返回值为true的元素的个数</p>
<p>返回切片中指定元素的个数</p>
<b>函数签名:</b>
```go
func Count[T any](slice []T, predicate func(index int, t T) bool) int
func Count[T comparable](slice []T, item T) int
```
<b>例子:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
nums := []int{1, 2, 3, 3, 4, 5}
fmt.Println(slice.Count(nums, 1)) //1
fmt.Println(slice.Count(nums, 3)) //2
}
```
### <span id="CountBy">CountBy</span>
<p>遍历切片对每个元素执行函数predicate. 返回符合函数返回值为true的元素的个数.</p>
<b>函数签名:</b>
```go
func CountBy[T any](slice []T, predicate func(index int, item T) bool) int
```
<b>例子:</b>
@@ -248,11 +281,11 @@ import (
func main() {
nums := []int{1, 2, 3, 4, 5, 6}
evenFunc := func(i, num int) bool {
evenFunc := func(_, num int) bool {
return (num % 2) == 0
}
res := slice.Count(nums, evenFunc)
res := slice.CountBy(nums, evenFunc)
fmt.Println(res) //3
}
```
@@ -719,7 +752,7 @@ func main() {
}
```
### <span id="IntSlice">IntSlice</span>
### <span id="IntSlice">IntSlice (已弃用: 使用go1.18+泛型代替)</span>
<p>将接口切片转换为int切片</p>
@@ -744,7 +777,7 @@ func main() {
}
```
### <span id="InterfaceSlice">InterfaceSlice</span>
### <span id="InterfaceSlice">InterfaceSlice(已弃用: 使用go1.18+泛型代替)</span>
<p>将值转换为接口切片</p>
@@ -832,7 +865,7 @@ func main() {
<b>函数签名:</b>
```go
func IndexOf[T comparable](slice []T, value T) int
func IndexOf[T comparable](slice []T, item T) int
```
<b>例子:</b>
@@ -860,7 +893,7 @@ func main() {
<b>函数签名:</b>
```go
func LastIndexOf[T comparable](slice []T, value T) int
func LastIndexOf[T comparable](slice []T, item T) int
```
<b>例子:</b>
@@ -883,7 +916,7 @@ func main() {
### <span id="Map">Map</span>
<p>通过运行函数slice中的每个元素创建一个新切片</p>
<p>slice中的每个元素执行map函数以创建一个新切片</p>
<b>函数签名:</b>
@@ -909,6 +942,34 @@ func main() {
}
```
### <span id="Merge">Merge</span>
<p>合并多个切片(不会消除重复元素).</p>
<b>函数签名:</b>
```go
func Merge[T any](slices ...[]T) []T
```
<b>例子:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
s1 := []int{1, 2, 3}
s2 := []int{2, 4}
res := slice.Merge(s1, s2)
fmt.Println(res) //[]int{1, 2, 3, 2, 4}
}
```
### <span id="Reverse">Reverse</span>
<p>反转切片中的元素顺序</p>
@@ -1018,6 +1079,30 @@ func main() {
}
```
### <span id="Repeat">Repeat</span>
<p>创建一个切片包含n个传入的item</p>
<b>函数签名:</b>
```go
func Repeat[T any](item T, n int) []T
```
<b>例子:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
fmt.Println(slice.Repeat("a", 3)) //[]string{"a", "a", "a"}
}
```
### <span id="Shuffle">Shuffle</span>
<p>随机打乱切片中的元素顺序</p>
@@ -1043,7 +1128,93 @@ func main() {
}
```
### <span id="SortByField">SortByField</span>
### <span id="Sort">Sort</span>
<p>对任何有序类型(数字或字符串)的切片进行排序,使用快速排序算法。 默认排序顺序为升序 (asc),如果需要降序,请将参数 `sortOrder` 设置为 `desc`。 Ordered类型数字所有整数浮点数或字符串。</p>
<b>函数签名:</b>
```go
func Sort[T constraints.Ordered](slice []T, sortOrder ...string)
```
<b>例子:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
numbers := []int{1, 4, 3, 2, 5}
slice.Sort(numbers)
fmt.Println(numbers) //{1,2,3,4,5}
slice.Sort(numbers, "desc")
fmt.Println(numbers) //{5,4,3,2,1}
strings := []string{"a", "d", "c", "b", "e"}
slice.Sort(strings)
fmt.Println(strings) //{"a", "b", "c", "d", "e"}
slice.Sort(strings, "desc")
fmt.Println(strings) //{"e", "d", "c", "b", "a"}
}
```
### <span id="SortBy">SortBy</span>
<p>按照less函数确定的升序规则对切片进行排序。排序不保证稳定性</p>
<b>函数签名:</b>
```go
func SortBy[T any](slice []T, less func(a, b T) bool)
```
<b>例子:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
numbers := []int{1, 4, 3, 2, 5}
slice.SortBy(numbers, func(a, b int) bool {
return a < b
})
fmt.Println(numbers) //{1, 2, 3, 4, 5}
type User struct {
Name string
Age uint
}
users := []User{
{Name: "a", Age: 21},
{Name: "b", Age: 15},
{Name: "c", Age: 100}}
slice.SortBy(users, func(a, b User) bool {
return a.Age < b.Age
})
fmt.Printf("sort users by age: %v", users)
// output
// [{b 15} {a 21} {c 100}]
}
```
### <span id="SortByField">SortByField (已弃用: 请使用 Sort或SortBy 代替该方法)</span>
<p>按字段对结构切片进行排序。slice元素应为struct字段类型应为int、uint、string或bool。 默认排序类型是升序asc如果是降序设置 sortType 为 desc</p>
@@ -1115,7 +1286,7 @@ func main() {
}
```
### <span id="StringSlice">StringSlice</span>
### <span id="StringSlice">StringSlice(已弃用: 使用go1.18+泛型代替)</span>
<p>将接口切片转换为字符串切片</p>
@@ -1176,7 +1347,7 @@ func main() {
<b>函数签名:</b>
```go
func ToSlice[T any](value ...T) []T
func ToSlice[T any](items ...T) []T
```
<b>例子:</b>
@@ -1200,7 +1371,7 @@ func main() {
<b>函数签名:</b>
```go
func ToSlicePointer[T any](value ...T) []*T
func ToSlicePointer[T any](items ...T) []*T
```
<b>例子:</b>
@@ -1356,7 +1527,7 @@ func main() {
<b>函数签名:</b>
```go
func Without[T comparable](slice []T, values ...T) []T
func Without[T comparable](slice []T, items ...T) []T
```
<b>例子:</b>

View File

@@ -28,15 +28,16 @@ import (
- [Capitalize](#Capitalize)
- [IsString](#IsString)
- [KebabCase](#KebabCase)
- [UpperKebabCase](#UpperKebabCase)
- [LowerFirst](#LowerFirst)
- [UpperFirst](#UpperFirst)
- [PadEnd](#PadEnd)
- [PadStart](#PadStart)
- [Reverse](#Reverse)
- [SnakeCase](#SnakeCase)
- [UpperSnakeCase](#UpperSnakeCase)
- [SplitEx](#SplitEx)
- [Wrap](#Wrap)
- [Unwrap](#Unwrap)
@@ -166,10 +167,8 @@ func main() {
```
### <span id="CamelCase">CamelCase</span>
<p>Covert string to camelCase string.</p>
<p>Coverts string to camelCase string, non letters and numbers will be ignored.</p>
<b>Signature:</b>
@@ -196,6 +195,75 @@ func main() {
s4 := strutil.CamelCase("foo bar")
fmt.Println(s4) //fooBar
s4 := strutil.CamelCase("Foo-#1😄$_%^&*(1bar")
fmt.Println(s4) //foo11Bar
}
```
### <span id="KebabCase">KebabCase</span>
<p>KebabCase covert string to kebab-case, non letters and numbers will be ignored.</p>
<b>Signature:</b>
```go
func KebabCase(s string) string
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
s1 := strutil.KebabCase("Foo Bar-")
fmt.Println(s1) //foo-bar
s2 := strutil.KebabCase("foo_Bar")
fmt.Println(s2) //foo-bar
s3 := strutil.KebabCase("fooBar")
fmt.Println(s3) //foo-bar
s4 := strutil.KebabCase("__FOO_BAR__")
fmt.Println(s4) //foo-bar
}
```
### <span id="UpperKebabCase">UpperKebabCase</span>
<p>UpperKebabCase covert string to upper KEBAB-CASE, non letters and numbers will be ignored.</p>
<b>Signature:</b>
```go
func KebabCase(s string) string
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
s1 := strutil.UpperKebabCase("Foo Bar-")
fmt.Println(s1) //FOO-BAR
s2 := strutil.UpperKebabCase("foo_Bar")
fmt.Println(s2) //FOO-BAR
s3 := strutil.UpperKebabCase("fooBar")
fmt.Println(s3) //FOO-BAR
s4 := strutil.UpperKebabCase("__FOO_BAR__")
fmt.Println(s4) //FOO-BAR
}
```
@@ -458,7 +526,7 @@ func main() {
### <span id="SnakeCase">SnakeCase</span>
<p>Covert string to snake_case.</p>
<p>Coverts string to snake_case, non letters and numbers will be ignored.</p>
<b>Signature:</b>
@@ -484,14 +552,47 @@ func main() {
fmt.Println(s3) //foo_bar
s4 := strutil.SnakeCase("__FOO_BAR__")
fmt.Println(s4) //f_o_o_b_a_r
fmt.Println(s4) //foo_bar
s5 := strutil.SnakeCase("aBbc-s$@a&%_B.B^C")
fmt.Println(s5) //a_bbc_s_a_b_b_c
s5 := strutil.SnakeCase("Foo-#1😄$_%^&*(1bar")
fmt.Println(s5) //foo_1_1_bar
}
```
### <span id="UpperSnakeCase">UpperSnakeCase</span>
<p>Coverts string to upper KEBAB-CASE, non letters and numbers will be ignored.</p>
<b>Signature:</b>
```go
func SnakeCase(s string) string
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
s1 := strutil.UpperSnakeCase("Foo Bar-")
fmt.Println(s1) //FOO_BAR
s2 := strutil.UpperSnakeCase("foo_Bar")
fmt.Println(s2) //FOO_BAR
s3 := strutil.UpperSnakeCase("fooBar")
fmt.Println(s3) //FOO_BAR
s4 := strutil.UpperSnakeCase("__FOO_BAR__")
fmt.Println(s4) //FOO_BAR
s5 := strutil.UpperSnakeCase("Foo-#1😄$_%^&*(1bar")
fmt.Println(s5) //FOO_1_1_BAR
}
```
### <span id="SplitEx">SplitEx</span>

View File

@@ -28,15 +28,16 @@ import (
- [Capitalize](#Capitalize)
- [IsString](#IsString)
- [KebabCase](#KebabCase)
- [UpperKebabCase](#UpperKebabCase)
- [LowerFirst](#LowerFirst)
- [UpperFirst](#UpperFirst)
- [PadEnd](#PadEnd)
- [PadStart](#PadStart)
- [Reverse](#Reverse)
- [SnakeCase](#SnakeCase)
- [UpperSnakeCase](#UpperSnakeCase)
- [SplitEx](#SplitEx)
- [Wrap](#Wrap)
- [Unwrap](#Unwrap)
@@ -170,7 +171,7 @@ func main() {
### <span id="CamelCase">CamelCase</span>
<p>将字符串转换为驼峰式字符串</p>
<p>将字符串转换为驼峰式字符串, 非字母和数字会被忽略</p>
<b>函数签名:</b>
@@ -197,6 +198,9 @@ func main() {
s4 := strutil.CamelCase("foo bar")
fmt.Println(s4) //fooBar
s4 := strutil.CamelCase("Foo-#1😄$_%^&*(1bar")
fmt.Println(s4) //foo11Bar
}
```
@@ -262,7 +266,7 @@ func main() {
### <span id="KebabCase">KebabCase</span>
<p>将字符串转换为kebab-case</p>
<p>将字符串转换为kebab-case, 非字母和数字会被忽略</p>
<b>函数签名:</b>
@@ -288,7 +292,40 @@ func main() {
fmt.Println(s3) //foo-bar
s4 := strutil.KebabCase("__FOO_BAR__")
fmt.Println(s4) //f-o-o-b-a-r
fmt.Println(s4) //foo-bar
}
```
### <span id="UpperKebabCase">UpperKebabCase</span>
<p>将字符串转换为大写KEBAB-CASE, 非字母和数字会被忽略</p>
<b>函数签名:</b>
```go
func KebabCase(s string) string
```
<b>例子:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
s1 := strutil.UpperKebabCase("Foo Bar-")
fmt.Println(s1) //FOO-BAR
s2 := strutil.UpperKebabCase("foo_Bar")
fmt.Println(s2) //FOO-BAR
s3 := strutil.UpperKebabCase("fooBar")
fmt.Println(s3) //FOO-BAR
s4 := strutil.UpperKebabCase("__FOO_BAR__")
fmt.Println(s4) //FOO-BAR
}
```
@@ -459,7 +496,7 @@ func main() {
### <span id="SnakeCase">SnakeCase</span>
<p>将字符串转换为snake_case形式</p>
<p>将字符串转换为snake_case形式, 非字母和数字会被忽略</p>
<b>函数签名:</b>
@@ -485,10 +522,45 @@ func main() {
fmt.Println(s3) //foo_bar
s4 := strutil.SnakeCase("__FOO_BAR__")
fmt.Println(s4) //f_o_o_b_a_r
fmt.Println(s4) //foo_bar
s5 := strutil.SnakeCase("aBbc-s$@a&%_B.B^C")
fmt.Println(s5) //a_bbc_s_a_b_b_c
s5 := strutil.SnakeCase("Foo-#1😄$_%^&*(1bar")
fmt.Println(s5) //foo_1_1_bar
}
```
### <span id="UpperSnakeCase">UpperSnakeCase</span>
<p>将字符串转换为大写SNAKE_CASE形式, 非字母和数字会被忽略</p>
<b>函数签名:</b>
```go
func SnakeCase(s string) string
```
<b>例子:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
s1 := strutil.UpperSnakeCase("Foo Bar-")
fmt.Println(s1) //FOO_BAR
s2 := strutil.UpperSnakeCase("foo_Bar")
fmt.Println(s2) //FOO_BAR
s3 := strutil.UpperSnakeCase("fooBar")
fmt.Println(s3) //FOO_BAR
s4 := strutil.UpperSnakeCase("__FOO_BAR__")
fmt.Println(s4) //FOO_BAR
s5 := strutil.UpperSnakeCase("Foo-#1😄$_%^&*(1bar")
fmt.Println(s5) //FOO_1_1_BAR
}
```

View File

@@ -211,7 +211,7 @@ func main() {
### <span id="ExecCommand">CompareOsEnv</span>
<p>Use shell /bin/bash -c(linux) or cmd (windows) to execute command.</p>
<p>Execute shell command, return the stdout and stderr string of command, and error if error occur. param `command` is a complete command string, like, ls -a (linux), dir(windows), ping 127.0.0.1. In linux, use /bin/bash -c to execute command, In windows, use powershell.exe to execute command.</p>
<b>Signature:</b>
@@ -227,10 +227,24 @@ import (
)
func main() {
out, errout, err := system.ExecCommand("ls")
fmt.Println(out)
fmt.Println(errout)
fmt.Println(err)
// linux or mac
stdout, stderr, err := system.ExecCommand("ls")
fmt.Println("std out: ", stdout)
fmt.Println("std err: ", stderr)
assert.Equal("", stderr)
// windows
stdout, stderr, err = system.ExecCommand("dir")
fmt.Println("std out: ", stdout)
fmt.Println("std err: ", stderr)
// error command
stdout, stderr, err = system.ExecCommand("abc")
fmt.Println("std out: ", stdout)
fmt.Println("std err: ", stderr)
if err != nil {
fmt.Println(err.Error())
}
}
```

View File

@@ -212,7 +212,7 @@ func main() {
### <span id="ExecCommand">ExecCommand</span>
<p>使用shell /bin/bash -c(linux) 或 cmd (windows) 执行shell命令</p>
<p>执行shell命令返回命令的stdout和stderr字符串如果出现错误则返回错误。参数`command`是一个完整的命令字符串如ls-alinuxdirwindowsping 127.0.0.1。在linux中使用/bin/bash-c执行命令在windows中使用powershell.exe执行命令</p>
<b>Signature:</b>
@@ -228,10 +228,24 @@ import (
)
func main() {
out, errout, err := system.ExecCommand("ls")
fmt.Println(out)
fmt.Println(errout)
fmt.Println(err)
// linux or mac
stdout, stderr, err := system.ExecCommand("ls")
fmt.Println("std out: ", stdout)
fmt.Println("std err: ", stderr)
assert.Equal("", stderr)
// windows
stdout, stderr, err = system.ExecCommand("dir")
fmt.Println("std out: ", stdout)
fmt.Println("std err: ", stderr)
// error command
stdout, stderr, err = system.ExecCommand("abc")
fmt.Println("std out: ", stdout)
fmt.Println("std err: ", stderr)
if err != nil {
fmt.Println(err.Error())
}
}
```

View File

@@ -34,7 +34,6 @@ import (
- [IsCreditCard](#IsCreditCard)
- [IsDns](#IsDns)
- [IsEmail](#IsEmail)
- [IsEmptyString](#IsEmptyString)
- [IsFloatStr](#IsFloatStr)
- [IsNumberStr](#IsNumberStr)
@@ -48,6 +47,7 @@ import (
- [IsUrl](#IsUrl)
- [IsWeakPassword](#IsWeakPassword)
- [IsZeroValue](#IsZeroValue)
- [IsGBK](#IsGBK)
<div STYLE="page-break-after: always;"></div>
@@ -822,7 +822,34 @@ func main() {
```
### <span id="IsGBK">IsGBK</span>
<p>Checks if data encoding is gbk(Chinese character internal code extension specification). this function is implemented by whether double bytes fall within the encoding range of gbk,while each byte of utf-8 encoding format falls within the encoding range of gbk.Therefore, utf8.valid() should be called first to check whether it is not utf-8 encoding and then call IsGBK() to check gbk encoding. like the example.</p>
<b>Signature:</b>
```go
func IsGBK(data []byte) bool
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
data := []byte("你好")
// check utf8 first
if utf8.Valid(data) {
fmt.Println("data encoding is utf-8")
}else if(validator.IsGBK(data)) {
fmt.Println("data encoding is GBK")
}
fmt.Println("data encoding is unknown")
}
```

View File

@@ -34,7 +34,6 @@ import (
- [IsCreditCard](#IsCreditCard)
- [IsDns](#IsDns)
- [IsEmail](#IsEmail)
- [IsEmptyString](#IsEmptyString)
- [IsFloatStr](#IsFloatStr)
- [IsNumberStr](#IsNumberStr)
@@ -48,6 +47,7 @@ import (
- [IsUrl](#IsUrl)
- [IsWeakPassword](#IsWeakPassword)
- [IsZeroValue](#IsZeroValue)
- [IsGBK](#IsGBK)
<div STYLE="page-break-after: always;"></div>
@@ -821,3 +821,31 @@ func main() {
}
```
### <span id="IsGBK">IsGBK</span>
<p>检查数据编码是否为gbk汉字内部代码扩展规范。该函数的实现取决于双字节是否在gbk的编码范围内而utf-8编码格式的每个字节都在gbk编码范围内。因此应该首先调用utf8.valid检查它是否是utf-8编码然后调用IsGBK检查gbk编码。如示例所示。</p>
<b>函数签名:</b>
```go
func IsGBK(data []byte) bool
```
<b>例子:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
data := []byte("你好")
// 先检查utf8编码
if utf8.Valid(data) {
fmt.Println("data encoding is utf-8")
}else if(validator.IsGBK(data)) {
fmt.Println("data encoding is GBK")
}
fmt.Println("data encoding is unknown")
}
```

View File

@@ -11,7 +11,6 @@ import (
"fmt"
"io"
"io/fs"
"io/ioutil"
"net/http"
"os"
"path"
@@ -78,13 +77,16 @@ func CopyFile(srcFilePath string, dstFilePath string) error {
var tmp = make([]byte, 1024*4)
for {
n, err := srcFile.Read(tmp)
distFile.Write(tmp[:n])
if err != nil {
if err == io.EOF {
return nil
}
return err
}
_, err = distFile.Write(tmp[:n])
if err != nil {
return err
}
}
}
@@ -102,7 +104,7 @@ func ClearFile(path string) error {
//ReadFileToString return string of file content
func ReadFileToString(path string) (string, error) {
bytes, err := ioutil.ReadFile(path)
bytes, err := os.ReadFile(path)
if err != nil {
return "", err
}
@@ -141,7 +143,7 @@ func ListFileNames(path string) ([]string, error) {
return []string{}, nil
}
fs, err := ioutil.ReadDir(path)
fs, err := os.ReadDir(path)
if err != nil {
return []string{}, err
}
@@ -172,7 +174,7 @@ func Zip(fpath string, destPath string) error {
archive := zip.NewWriter(zipFile)
defer archive.Close()
filepath.Walk(fpath, func(path string, info os.FileInfo, err error) error {
err = filepath.Walk(fpath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
@@ -209,6 +211,10 @@ func Zip(fpath string, destPath string) error {
return nil
})
if err != nil {
return err
}
return nil
}
@@ -222,8 +228,6 @@ func UnZip(zipFile string, destPath string) error {
defer zipReader.Close()
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 {
@@ -231,9 +235,13 @@ func UnZip(zipFile string, destPath string) error {
}
if f.FileInfo().IsDir() {
os.MkdirAll(path, os.ModePerm)
err = os.MkdirAll(path, os.ModePerm)
if err != nil {
return err
}
} else {
if err = os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
err = os.MkdirAll(filepath.Dir(path), os.ModePerm)
if err != nil {
return err
}

View File

@@ -25,9 +25,10 @@ func TestCreateFile(t *testing.T) {
f := "./text.txt"
if CreateFile(f) {
file, err := os.Open(f)
defer file.Close()
assert.IsNil(err)
assert.Equal(f, file.Name())
defer file.Close()
} else {
t.FailNow()
}
@@ -113,7 +114,11 @@ func TestReadFileToString(t *testing.T) {
f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777)
defer f.Close()
f.WriteString("hello world")
_, err := f.WriteString("hello world")
if err != nil {
t.Log(err)
}
content, _ := ReadFileToString(path)
assert.Equal("hello world", content)
@@ -130,9 +135,12 @@ func TestClearFile(t *testing.T) {
f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777)
defer f.Close()
f.WriteString("hello world")
_, err := f.WriteString("hello world")
if err != nil {
t.Log(err)
}
err := ClearFile(path)
err = ClearFile(path)
assert.IsNil(err)
content, _ := ReadFileToString(path)
@@ -148,8 +156,13 @@ func TestReadFileByLine(t *testing.T) {
CreateFile(path)
f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777)
defer f.Close()
f.WriteString("hello\nworld")
_, err := f.WriteString("hello\nworld")
if err != nil {
t.Log(err)
}
expected := []string{"hello", "world"}
actual, _ := ReadFileByLine(path)
@@ -166,10 +179,14 @@ func TestZipAndUnZip(t *testing.T) {
file, _ := os.OpenFile(srcFile, os.O_WRONLY|os.O_TRUNC, 0777)
defer file.Close()
file.WriteString("hello\nworld")
_, err := file.WriteString("hello\nworld")
if err != nil {
t.Fail()
}
zipFile := "./text.zip"
err := Zip(srcFile, zipFile)
err = Zip(srcFile, zipFile)
assert.IsNil(err)
unZipPath := "./unzip"

View File

@@ -4,14 +4,25 @@
// Package formatter implements some functions to format string, struct.
package formatter
import "strings"
import (
"strings"
"golang.org/x/exp/constraints"
)
// Comma add comma to a number value by every 3 numbers from right. ahead by symbol char.
// if value is invalid number string eg "aa", return empty string
// Comma("12345", "$") => "$12,345", Comma(12345, "$") => "$12,345"
func Comma[T constraints.Float | constraints.Integer | string](value T, symbol string) string {
s, err := numberToString(value)
if err != nil {
return ""
}
// Comma add comma to number by every 3 numbers from right. ahead by symbol char
func Comma(v any, symbol string) string {
s := numString(v)
dotIndex := strings.Index(s, ".")
if dotIndex != -1 {
return symbol + commaString(s[:dotIndex]) + s[dotIndex:]
}
return symbol + commaString(s)
}

View File

@@ -14,27 +14,34 @@ func commaString(s string) string {
return commaString(s[:len(s)-3]) + "," + commaString(s[len(s)-3:])
}
func numString(value any) string {
func numberToString(value any) (string, error) {
switch reflect.TypeOf(value).Kind() {
case reflect.Int, reflect.Int64, reflect.Float32, reflect.Float64:
return fmt.Sprintf("%v", value)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return fmt.Sprintf("%v", value), nil
// todo: need to handle 12345678.9 => 1.23456789e+07
case reflect.Float32, reflect.Float64:
return fmt.Sprintf("%v", value), nil
case reflect.String:
{
sv := fmt.Sprintf("%v", value)
if strings.Contains(sv, ".") {
_, err := strconv.ParseFloat(sv, 64)
if err == nil {
return sv
if err != nil {
return "", err
}
return sv, nil
} else {
_, err := strconv.ParseInt(sv, 10, 64)
if err == nil {
return sv
if err != nil {
return "", nil
}
return sv, nil
}
}
default:
return ""
return "", nil
}
return ""
}

View File

@@ -12,12 +12,16 @@ func TestComma(t *testing.T) {
assert.Equal("", Comma("", ""))
assert.Equal("", Comma("aa", ""))
assert.Equal("", Comma("aa.a", ""))
assert.Equal("", Comma([]int{1}, ""))
assert.Equal("123", Comma("123", ""))
assert.Equal("12,345", Comma("12345", ""))
assert.Equal("12,345.6789", Comma("12345.6789", ""))
assert.Equal("123,456,789,000", Comma("123456789000", ""))
assert.Equal("12,345", Comma(12345, ""))
assert.Equal("$12,345", Comma(12345, "$"))
assert.Equal("¥12,345", Comma(12345, "¥"))
assert.Equal("12,345.6789", Comma(12345.6789, ""))
assert.Equal("12,345.6789", Comma(+12345.6789, ""))
// assert.Equal("12,345,678.9", Comma(12345678.9, ""))
assert.Equal("123,456,789,000", Comma(123456789000, ""))
}

View File

@@ -84,10 +84,8 @@ func Debounced(fn func(), duration time.Duration) func() {
go func() {
for {
select {
case <-timer.C:
go fn()
}
<-timer.C
go fn()
}
}()

5
go.mod
View File

@@ -1,3 +1,8 @@
module github.com/duke-git/lancet/v2
go 1.18
require (
golang.org/x/exp v0.0.0-20221208152030-732eee02a75a
golang.org/x/text v0.5.0
)

4
go.sum Normal file
View File

@@ -0,0 +1,4 @@
golang.org/x/exp v0.0.0-20221208152030-732eee02a75a h1:4iLhBPcpqFmylhnkbY3W0ONLUYYkDAW9xMFLfxgsvCw=
golang.org/x/exp v0.0.0-20221208152030-732eee02a75a/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM=
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=

173
iterator/iterator.go Normal file
View File

@@ -0,0 +1,173 @@
// 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 package 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 (
"golang.org/x/exp/constraints"
)
// 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 constraints.Integer | constraints.Float](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 constraints.Integer | constraints.Float] struct {
start, end, step T
}
func (iter *rangeIterator[T]) HasNext() bool {
return iter.start < iter.end
}
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
View 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
View 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 package 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()
}

View File

@@ -11,8 +11,3 @@ type Comparator interface {
// Descending order: should return 1 -> v1 < v2, 0 -> v1 = v2, -1 -> v1 > v2
Compare(v1, v2 any) int
}
// Number contains all types of number and uintptr, used for generics constraint
type Number interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~float32 | ~float64
}

View File

@@ -85,7 +85,7 @@ func Intersect[K comparable, V any](maps ...map[K]V) map[K]V {
return m
}
reduceMaps := make([]map[K]V, 2, 2)
reduceMaps := make([]map[K]V, 2)
result = reducer(maps[0], maps[1])
for i := 2; i < len(maps); i++ {

View File

@@ -10,7 +10,7 @@ import (
"strconv"
"strings"
"github.com/duke-git/lancet/v2/lancetconstraints"
"golang.org/x/exp/constraints"
)
// Exponent calculate x^n
@@ -94,7 +94,7 @@ func TruncRound(x float64, n int) float64 {
}
// Max return max value of params
func Max[T lancetconstraints.Number](numbers ...T) T {
func Max[T constraints.Integer | constraints.Float](numbers ...T) T {
max := numbers[0]
for _, v := range numbers {
@@ -128,7 +128,7 @@ func MaxBy[T any](slice []T, comparator func(T, T) bool) T {
}
// Min return min value of params
func Min[T lancetconstraints.Number](numbers ...T) T {
func Min[T constraints.Integer | constraints.Float](numbers ...T) T {
min := numbers[0]
for _, v := range numbers {
@@ -161,8 +161,8 @@ func MinBy[T any](slice []T, comparator func(T, T) bool) T {
return min
}
// Average return average value of params
func Average[T lancetconstraints.Number](numbers ...T) T {
// Average return average value of numbers
func Average[T constraints.Integer | constraints.Float](numbers ...T) T {
var sum T
n := T(len(numbers))

View File

@@ -6,7 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"reflect"
@@ -108,7 +108,11 @@ func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, err
client.setTLS(rawUrl)
client.setHeader(req, request.Headers)
client.setQueryParam(req, rawUrl, request.QueryParams)
err = client.setQueryParam(req, rawUrl, request.QueryParams)
if err != nil {
return nil, err
}
if request.FormData != nil {
client.setFormData(req, request.FormData)
@@ -177,7 +181,7 @@ func (client *HttpClient) setQueryParam(req *http.Request, reqUrl string, queryP
func (client *HttpClient) setFormData(req *http.Request, values url.Values) {
formData := []byte(values.Encode())
req.Body = ioutil.NopCloser(bytes.NewReader(formData))
req.Body = io.NopCloser(bytes.NewReader(formData))
req.ContentLength = int64(len(formData))
}

View File

@@ -1,7 +1,7 @@
package netutil
import (
"io/ioutil"
"io"
"log"
"net/http"
"net/url"
@@ -32,7 +32,10 @@ func TestHttpClient_Get(t *testing.T) {
}
var todo Todo
httpClient.DecodeResponse(resp, &todo)
err = httpClient.DecodeResponse(resp, &todo)
if err != nil {
t.Log(err)
}
assert.Equal(1, todo.Id)
}
@@ -58,7 +61,7 @@ func TestHttpClent_Post(t *testing.T) {
log.Fatal(err)
}
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
t.Log("response: ", resp.StatusCode, string(body))
}
@@ -90,6 +93,6 @@ func TestStructToUrlValues(t *testing.T) {
log.Fatal(err)
}
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
t.Log("response: ", string(body))
}

View File

@@ -2,8 +2,9 @@ package netutil
import (
"encoding/json"
"io/ioutil"
"io"
"log"
"net/url"
"testing"
"github.com/duke-git/lancet/v2/internal"
@@ -20,7 +21,7 @@ func TestHttpGet(t *testing.T) {
log.Fatal(err)
}
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
t.Log("response: ", resp.StatusCode, string(body))
}
@@ -40,7 +41,7 @@ func TestHttpPost(t *testing.T) {
if err != nil {
log.Fatal(err)
}
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
t.Log("response: ", resp.StatusCode, string(body))
}
@@ -50,23 +51,20 @@ func TestHttpPostFormData(t *testing.T) {
// "Content-Type": "application/x-www-form-urlencoded",
"Content-Type": "multipart/form-data",
}
type Todo struct {
UserId int `json:"userId"`
Title string `json:"title"`
}
// postData := url.Values{}
// postData.Add("userId", "1")
// postData.Add("title", "TestAddToDo")
postData := make(map[string]string)
postData["userId"] = "1"
postData["title"] = "title"
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, postData, nil)
if err != nil {
log.Fatal(err)
}
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
t.Log("response: ", resp.StatusCode, string(body))
}
@@ -87,7 +85,7 @@ func TestHttpPut(t *testing.T) {
if err != nil {
log.Fatal(err)
}
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
t.Log("response: ", resp.StatusCode, string(body))
}
@@ -108,7 +106,7 @@ func TestHttpPatch(t *testing.T) {
if err != nil {
log.Fatal(err)
}
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
t.Log("response: ", resp.StatusCode, string(body))
}
@@ -118,7 +116,7 @@ func TestHttpDelete(t *testing.T) {
if err != nil {
log.Fatal(err)
}
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
t.Log("response: ", resp.StatusCode, string(body))
}

View File

@@ -2,7 +2,7 @@ package netutil
import (
"encoding/json"
"io/ioutil"
"io"
"net"
"net/http"
"net/url"
@@ -55,7 +55,7 @@ func GetPublicIpInfo() (*PublicIpInfo, error) {
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

View File

@@ -3,7 +3,7 @@ package netutil
import (
"bytes"
"errors"
"io/ioutil"
"io"
"net/http"
"net/url"
"strings"
@@ -173,7 +173,7 @@ func setBodyByte(req *http.Request, body any) error {
if body != nil {
switch b := body.(type) {
case []byte:
req.Body = ioutil.NopCloser(bytes.NewReader(b))
req.Body = io.NopCloser(bytes.NewReader(b))
req.ContentLength = int64(len(b))
default:
return errors.New("body type should be []byte")

View File

@@ -10,22 +10,31 @@ import (
"math/rand"
"reflect"
"sort"
"golang.org/x/exp/constraints"
)
// Contain check if the value is in the slice or not
func Contain[T comparable](slice []T, value T) bool {
set := make(map[T]struct{}, len(slice))
for _, v := range slice {
set[v] = struct{}{}
// Create a static variable to store the hash table.
// This variable has the same lifetime as the entire program and can be shared by functions that are called more than once.
var (
memoryHashMap = make(map[string]map[any]int)
memoryHashCounter = make(map[string]int)
)
// Contain check if the target value is in the slice or not
func Contain[T comparable](slice []T, target T) bool {
for _, item := range slice {
if item == target {
return true
}
}
_, ok := set[value]
return ok
return false
}
// ContainSubSlice check if the slice contain subslice or not
func ContainSubSlice[T comparable](slice, subslice []T) bool {
for _, v := range subslice {
// ContainSubSlice check if the slice contain a given subslice or not
func ContainSubSlice[T comparable](slice, subSlice []T) bool {
for _, v := range subSlice {
if !Contain(slice, v) {
return false
}
@@ -34,7 +43,7 @@ func ContainSubSlice[T comparable](slice, subslice []T) bool {
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 {
result := [][]T{}
@@ -42,39 +51,27 @@ func Chunk[T any](slice []T, size int) [][]T {
return result
}
length := len(slice)
if size == 1 || size >= length {
for _, v := range slice {
var tmp []T
tmp = append(tmp, v)
result = append(result, tmp)
for _, item := range slice {
l := len(result)
if l == 0 || len(result[l-1]) == size {
result = append(result, []T{})
l++
}
return result
}
// divide slice equally
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])
}
result[l-1] = append(result[l-1], item)
}
return result
}
// Compact creates an slice with all falsey values removed. The values false, nil, 0, and "" are falsey
func Compact[T any](slice []T) []T {
result := make([]T, 0)
func Compact[T comparable](slice []T) []T {
var zero T
result := []T{}
for _, v := range slice {
if !reflect.DeepEqual(v, nil) &&
!reflect.DeepEqual(v, false) &&
!reflect.DeepEqual(v, "") &&
!reflect.DeepEqual(v, 0) {
if v != zero {
result = append(result, v)
}
}
@@ -82,11 +79,11 @@ func Compact[T any](slice []T) []T {
return result
}
// Concat creates a new slice concatenating slice with any additional slices and/or values.
func Concat[T any](slice []T, values ...[]T) []T {
// Concat creates a new slice concatenating slice with any additional slices.
func Concat[T any](slice []T, slices ...[]T) []T {
result := append([]T{}, slice...)
for _, v := range values {
for _, v := range slices {
result = append(result, v...)
}
@@ -123,8 +120,8 @@ func DifferenceBy[T comparable](slice []T, comparedSlice []T, iteratee func(inde
return result
}
//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(value1, value2 T) bool) []T {
// 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(item1, item2 T) bool) []T {
result := make([]T, 0)
getIndex := func(arr []T, item T, comparison func(v1, v2 T) bool) int {
@@ -169,9 +166,8 @@ func EqualWith[T, U any](slice1 []T, slice2 []U, comparator func(T, U) bool) boo
return false
}
for i, v1 := range slice1 {
v2 := slice2[i]
if !comparator(v1, v2) {
for i, v := range slice1 {
if !comparator(v, slice2[i]) {
return false
}
}
@@ -181,33 +177,29 @@ 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.
func Every[T any](slice []T, predicate func(index int, item T) bool) bool {
var currentLength int
for i, v := range slice {
if predicate(i, v) {
currentLength++
if !predicate(i, v) {
return false
}
}
return currentLength == len(slice)
return true
}
// 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 {
var currentLength int
l := 0
for i, v := range slice {
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.
func Some[T any](slice []T, predicate func(index int, item T) bool) bool {
for i, v := range slice {
if predicate(i, v) {
return true
@@ -220,6 +212,7 @@ func Some[T any](slice []T, predicate func(index int, item T) bool) bool {
// 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 {
result := make([]T, 0)
for i, v := range slice {
if predicate(i, v) {
result = append(result, v)
@@ -229,13 +222,23 @@ func Filter[T any](slice []T, predicate func(index int, item T) bool) []T {
return result
}
// Count iterates over elements of slice, returns a count of all matched elements
func Count[T any](slice []T, predicate func(index int, item T) bool) int {
if len(slice) == 0 {
return 0
// Count returns the number of occurrences of the given item in the slice
func Count[T comparable](slice []T, item T) int {
count := 0
for _, v := range slice {
if item == v {
count++
}
}
var count int
return count
}
// 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 {
if predicate(i, v) {
count++
@@ -284,11 +287,8 @@ func GroupWith[T any, U comparable](slice []T, iteratee func(item T) U) map[U][]
// 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
func Find[T any](slice []T, predicate func(index int, item T) bool) (*T, bool) {
if len(slice) == 0 {
return nil, false
}
index := -1
for i, v := range slice {
if predicate(i, v) {
index = i
@@ -306,11 +306,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.
// 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) {
if len(slice) == 0 {
return nil, false
}
index := -1
for i := len(slice) - 1; i >= 0; i-- {
if predicate(i, slice[i]) {
index = i
@@ -356,8 +353,11 @@ func Flatten(slice any) any {
func FlattenDeep(slice any) any {
sv := sliceValue(slice)
st := sliceElemType(sv.Type())
tmp := reflect.MakeSlice(reflect.SliceOf(st), 0, 0)
result := flattenRecursive(sv, tmp)
return result.Interface()
}
@@ -386,6 +386,7 @@ 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.
func Map[T any, U any](slice []T, iteratee func(index int, item T) U) []U {
result := make([]U, len(slice), cap(slice))
for i, v := range slice {
result[i] = iteratee(i, v)
}
@@ -395,7 +396,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.
func Reduce[T any](slice []T, iteratee func(index int, item1, item2 T) T, initial T) T {
if len(slice) == 0 {
return initial
}
@@ -432,7 +432,19 @@ func ReplaceAll[T comparable](slice []T, old T, new T) []T {
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.
// This function is deprecated, use generics feature of go1.18+ for replacement
func InterfaceSlice(slice any) []any {
sv := sliceValue(slice)
if sv.IsNil() {
@@ -448,35 +460,37 @@ func InterfaceSlice(slice any) []any {
}
// StringSlice convert param to slice of string.
// This function is deprecated, use generics feature of go1.18+ for replacement
func StringSlice(slice any) []string {
v := sliceValue(slice)
out := make([]string, v.Len())
result := make([]string, v.Len())
for i := 0; i < v.Len(); i++ {
v, ok := v.Index(i).Interface().(string)
if !ok {
panic("invalid element type")
}
out[i] = v
result[i] = v
}
return out
return result
}
// IntSlice convert param to slice of int.
// This function is deprecated, use generics feature of go1.18+ for replacement
func IntSlice(slice any) []int {
sv := sliceValue(slice)
out := make([]int, sv.Len())
result := make([]int, sv.Len())
for i := 0; i < sv.Len(); i++ {
v, ok := sv.Index(i).Interface().(int)
if !ok {
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.
@@ -594,7 +608,7 @@ func UniqueBy[T comparable](slice []T, iteratee func(item T) T) []T {
return Unique(result)
}
// Union creates a slice of unique values, in order, from all given slices.
// Union creates a slice of unique elements, in order, from all given slices.
func Union[T comparable](slices ...[]T) []T {
result := []T{}
contain := map[T]struct{}{}
@@ -629,7 +643,18 @@ func UnionBy[T any, V comparable](predicate func(item T) V, slices ...[]T) []T {
return result
}
// Intersection creates a slice of unique values that included by all slices.
// 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 {
if len(slices) == 0 {
return []T{}
@@ -640,8 +665,8 @@ func Intersection[T comparable](slices ...[]T) []T {
reducer := func(sliceA, sliceB []T) []T {
hashMap := make(map[T]int)
for _, val := range sliceA {
hashMap[val] = 1
for _, v := range sliceA {
hashMap[v] = 1
}
out := make([]T, 0)
@@ -709,9 +734,26 @@ func Shuffle[T any](slice []T) []T {
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 constraints.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
// 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
// This function is deprecated, use Sort and SortBy for replacement
func SortByField(slice any, field string, sortType ...string) error {
sv := sliceValue(slice)
t := sv.Type().Elem()
@@ -781,15 +823,15 @@ func SortByField(slice any, field string, sortType ...string) error {
return nil
}
// Without creates a slice excluding all given values
func Without[T comparable](slice []T, values ...T) []T {
if len(values) == 0 || len(slice) == 0 {
// Without creates a slice excluding all given items
func Without[T comparable](slice []T, items ...T) []T {
if len(items) == 0 || len(slice) == 0 {
return slice
}
result := make([]T, 0, len(slice))
for _, v := range slice {
if !Contain(values, v) {
if !Contain(items, v) {
result = append(result, v)
}
}
@@ -797,23 +839,51 @@ func Without[T comparable](slice []T, values ...T) []T {
return result
}
// IndexOf returns the index at which the first occurrence of a value is found in a slice or return -1
// if the value cannot be found.
func IndexOf[T comparable](slice []T, value T) int {
for i, v := range slice {
if v == value {
return i
// IndexOf returns the index at which the first occurrence of an item is found in a slice or return -1 if the item cannot be found.
func IndexOf[T comparable](arr []T, val T) int {
limit := 10
// gets the hash value of the array as the key of the hash table.
key := fmt.Sprintf("%p", arr)
// determines whether the hash table is empty. If so, the hash table is created.
if memoryHashMap[key] == nil {
memoryHashMap[key] = make(map[any]int)
// iterate through the array, adding the value and index of each element to the hash table.
for i := len(arr) - 1; i >= 0; i-- {
memoryHashMap[key][arr[i]] = i
}
}
// update the hash table counter.
memoryHashCounter[key]++
// use the hash table to find the specified value. If found, the index is returned.
if index, ok := memoryHashMap[key][val]; ok {
// calculate the memory usage of the hash table.
size := len(memoryHashMap)
// If the memory usage of the hash table exceeds the memory limit, the hash table with the lowest counter is cleared.
if size > limit {
var minKey string
var minVal int
for k, v := range memoryHashCounter {
if k == key {
continue
}
if minVal == 0 || v < minVal {
minKey = k
minVal = v
}
}
delete(memoryHashMap, minKey)
delete(memoryHashCounter, minKey)
}
return index
}
return -1
}
// LastIndexOf returns the index at which the last occurrence of a value is found in a slice or return -1
// if the value cannot be found.
func LastIndexOf[T comparable](slice []T, value T) int {
// 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.
func LastIndexOf[T comparable](slice []T, item T) int {
for i := len(slice) - 1; i > 0; i-- {
if value == slice[i] {
if item == slice[i] {
return i
}
}
@@ -822,27 +892,27 @@ func LastIndexOf[T comparable](slice []T, value T) int {
}
// ToSlicePointer returns a pointer to the slices of a variable parameter transformation
func ToSlicePointer[T any](value ...T) []*T {
result := make([]*T, len(value))
for i := range value {
result[i] = &value[i]
func ToSlicePointer[T any](items ...T) []*T {
result := make([]*T, len(items))
for i := range items {
result[i] = &items[i]
}
return result
}
// ToSlice returns a slices of a variable parameter transformation
func ToSlice[T any](value ...T) []T {
result := make([]T, len(value))
copy(result, value)
func ToSlice[T any](items ...T) []T {
result := make([]T, len(items))
copy(result, items)
return result
}
// AppendIfAbsent only absent append the value
func AppendIfAbsent[T comparable](slice []T, value T) []T {
if !Contain(slice, value) {
slice = append(slice, value)
// AppendIfAbsent only absent append the item
func AppendIfAbsent[T comparable](slice []T, item T) []T {
if !Contain(slice, item) {
slice = append(slice, item)
}
return slice
}

View File

@@ -3,6 +3,8 @@ package slice
import (
"fmt"
"reflect"
"golang.org/x/exp/constraints"
)
// sliceValue return the reflect value of a slice
@@ -24,3 +26,66 @@ func sliceElemType(reflectType reflect.Type) reflect.Type {
reflectType = reflectType.Elem()
}
}
func quickSort[T constraints.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 constraints.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]
}

View File

@@ -1,6 +1,7 @@
package slice
import (
"fmt"
"math"
"testing"
@@ -31,6 +32,10 @@ func TestChunk(t *testing.T) {
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"}}
assert.Equal(r1, Chunk(arr, 1))
@@ -43,8 +48,11 @@ func TestChunk(t *testing.T) {
r4 := [][]string{{"a", "b", "c", "d"}, {"e"}}
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))
r6 := [][]string{{"a", "b", "c", "d", "e"}}
assert.Equal(r6, Chunk(arr, 6))
}
func TestCompact(t *testing.T) {
@@ -186,13 +194,22 @@ func TestGroupWith(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}
evenFunc := func(i, num int) bool {
return (num % 2) == 0
}
assert := internal.NewAssert(t, "TestCount")
assert.Equal(3, Count(nums, evenFunc))
assert := internal.NewAssert(t, "TestCountBy")
assert.Equal(3, CountBy(nums, evenFunc))
}
func TestFind(t *testing.T) {
@@ -439,6 +456,18 @@ func TestUnionBy(t *testing.T) {
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) {
s1 := []int{1, 2, 2, 3}
s2 := []int{1, 2, 3, 4}
@@ -519,6 +548,56 @@ func TestDifferenceBy(t *testing.T) {
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) {
assert := internal.NewAssert(t, "TestSortByFielDesc")
@@ -591,8 +670,39 @@ func TestIndexOf(t *testing.T) {
assert := internal.NewAssert(t, "TestIndexOf")
arr := []string{"a", "a", "b", "c"}
key := fmt.Sprintf("%p", arr)
assert.Equal(0, IndexOf(arr, "a"))
assert.Equal(-1, IndexOf(arr, "d"))
assert.Equal(2, memoryHashCounter[key])
arr1 := []int{1, 2, 3, 4, 5}
key1 := fmt.Sprintf("%p", arr1)
assert.Equal(3, IndexOf(arr1, 4))
assert.Equal(-1, IndexOf(arr1, 6))
assert.Equal(2, memoryHashCounter[key1])
arr2 := []float64{1.1, 2.2, 3.3, 4.4, 5.5}
key2 := fmt.Sprintf("%p", arr2)
assert.Equal(2, IndexOf(arr2, 3.3))
assert.Equal(3, IndexOf(arr2, 4.4))
assert.Equal(-1, IndexOf(arr2, 6.6))
assert.Equal(3, memoryHashCounter[key2])
for i := 0; i < 6; i++ {
a := []string{"a", "b", "c"}
IndexOf(a, "a")
IndexOf(a, "b")
}
minArr := []string{"c", "b", "a"}
minKey := fmt.Sprintf("%p", minArr)
assert.Equal(0, IndexOf(minArr, "c"))
arr3 := []string{"q", "w", "e"}
key3 := fmt.Sprintf("%p", arr3)
assert.Equal(1, IndexOf(arr3, "w"))
assert.Equal(-1, IndexOf(arr3, "r"))
assert.Equal(2, memoryHashCounter[key3])
assert.Equal(0, memoryHashCounter[minKey])
}
func TestLastIndexOf(t *testing.T) {
@@ -665,3 +775,11 @@ func TestKeyBy(t *testing.T) {
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"})
}

View File

@@ -4,53 +4,41 @@
package strutil
import (
"regexp"
"strings"
"unicode"
"unicode/utf8"
)
// CamelCase covert string to camelCase string.
// non letters and numbers will be ignored
// eg. "Foo-#1😄$_%^&*(1bar" => "foo11Bar"
func CamelCase(s string) string {
if len(s) == 0 {
return ""
}
var builder strings.Builder
result := ""
blankSpace := " "
regex, _ := regexp.Compile("[-_&]+")
ss := regex.ReplaceAllString(s, blankSpace)
for i, v := range strings.Split(ss, blankSpace) {
vv := []rune(v)
strs := splitIntoStrings(s, false)
for i, str := range strs {
if i == 0 {
if vv[i] >= 65 && vv[i] <= 96 {
vv[0] += 32
}
result += string(vv)
builder.WriteString(strings.ToLower(str))
} else {
result += Capitalize(v)
builder.WriteString(Capitalize(str))
}
}
return result
return builder.String()
}
// Capitalize converts the first character of a string to upper case and the remaining to lower case.
func Capitalize(s string) string {
if len(s) == 0 {
return ""
}
out := make([]rune, len(s))
result := make([]rune, len(s))
for i, v := range s {
if i == 0 {
out[i] = unicode.ToUpper(v)
result[i] = unicode.ToUpper(v)
} else {
out[i] = unicode.ToLower(v)
result[i] = unicode.ToLower(v)
}
}
return string(out)
return string(result)
}
// UpperFirst converts the first character of string to upper case.
@@ -116,46 +104,34 @@ func PadStart(source string, size int, padStr string) string {
}
// KebabCase covert string to kebab-case
// non letters and numbers will be ignored
// eg. "Foo-#1😄$_%^&*(1bar" => "foo-1-1-bar"
func KebabCase(s string) string {
if len(s) == 0 {
return ""
}
regex := regexp.MustCompile(`[\W|_]+`)
blankSpace := " "
match := regex.ReplaceAllString(s, blankSpace)
rs := strings.Split(match, blankSpace)
var result []string
for _, v := range rs {
splitWords := splitWordsToLower(v)
if len(splitWords) > 0 {
result = append(result, splitWords...)
}
}
result := splitIntoStrings(s, false)
return strings.Join(result, "-")
}
// UpperKebabCase covert string to upper KEBAB-CASE
// non letters and numbers will be ignored
// eg. "Foo-#1😄$_%^&*(1bar" => "FOO-1-1-BAR"
func UpperKebabCase(s string) string {
result := splitIntoStrings(s, true)
return strings.Join(result, "-")
}
// SnakeCase covert string to snake_case
// non letters and numbers will be ignored
// eg. "Foo-#1😄$_%^&*(1bar" => "foo_1_1_bar"
func SnakeCase(s string) string {
if len(s) == 0 {
return ""
}
regex := regexp.MustCompile(`[\W|_]+`)
blankSpace := " "
match := regex.ReplaceAllString(s, blankSpace)
rs := strings.Split(match, blankSpace)
var result []string
for _, v := range rs {
splitWords := splitWordsToLower(v)
if len(splitWords) > 0 {
result = append(result, splitWords...)
}
}
result := splitIntoStrings(s, false)
return strings.Join(result, "_")
}
// UpperSnakeCase covert string to upper SNAKE_CASE
// non letters and numbers will be ignored
// eg. "Foo-#1😄$_%^&*(1bar" => "FOO_1_1_BAR"
func UpperSnakeCase(s string) string {
result := splitIntoStrings(s, true)
return strings.Join(result, "_")
}

View File

@@ -1,40 +1,100 @@
package strutil
import "strings"
import (
"unicode"
)
// splitWordsToLower split a string into worlds by uppercase char
func splitWordsToLower(s string) []string {
var result []string
func splitIntoStrings(s string, upperCase bool) []string {
var runes [][]rune
lastCharType := 0
charType := 0
upperIndexes := upperIndex(s)
l := len(upperIndexes)
if upperIndexes == nil || l == 0 {
if s != "" {
result = append(result, s)
// split into fields based on type of unicode character
for _, r := range s {
switch true {
case isLower(r):
charType = 1
case isUpper(r):
charType = 2
case isDigit(r):
charType = 3
default:
charType = 4
}
return result
}
for i := 0; i < l; i++ {
if i < l-1 {
result = append(result, strings.ToLower(s[upperIndexes[i]:upperIndexes[i+1]]))
if charType == lastCharType {
runes[len(runes)-1] = append(runes[len(runes)-1], r)
} else {
result = append(result, strings.ToLower(s[upperIndexes[i]:]))
runes = append(runes, []rune{r})
}
lastCharType = charType
}
return result
}
// upperIndex get a int slice which elements are all the uppercase char index of a string
func upperIndex(s string) []int {
var result []int
for i := 0; i < len(s); i++ {
if 64 < s[i] && s[i] < 91 {
result = append(result, i)
for i := 0; i < len(runes)-1; i++ {
if isUpper(runes[i][0]) && isLower(runes[i+1][0]) {
runes[i+1] = append([]rune{runes[i][len(runes[i])-1]}, runes[i+1]...)
runes[i] = runes[i][:len(runes[i])-1]
}
}
if len(s) > 0 && result != nil && result[0] != 0 {
result = append([]int{0}, result...)
// filter all none letters and none digit
var result []string
for _, rs := range runes {
if len(rs) > 0 && (unicode.IsLetter(rs[0]) || isDigit(rs[0])) {
if upperCase {
result = append(result, string(toUpperAll(rs)))
} else {
result = append(result, string(toLowerAll(rs)))
}
}
}
return result
}
// isDigit checks if a character is digit ('0' to '9')
func isDigit(r rune) bool {
return r >= '0' && r <= '9'
}
// isLower checks if a character is lower case ('a' to 'z')
func isLower(r rune) bool {
return r >= 'a' && r <= 'z'
}
// isUpper checks if a character is upper case ('A' to 'Z')
func isUpper(r rune) bool {
return r >= 'A' && r <= 'Z'
}
// toLower converts a character 'A' to 'Z' to its lower case
func toLower(r rune) rune {
if r >= 'A' && r <= 'Z' {
return r + 32
}
return r
}
// toLowerAll converts a character 'A' to 'Z' to its lower case
func toLowerAll(rs []rune) []rune {
for i := range rs {
rs[i] = toLower(rs[i])
}
return rs
}
// toUpper converts a character 'a' to 'z' to its upper case
func toUpper(r rune) rune {
if r >= 'a' && r <= 'z' {
return r - 32
}
return r
}
// toUpperAll converts a character 'a' to 'z' to its upper case
func toUpperAll(rs []rune) []rune {
for i := range rs {
rs[i] = toUpper(rs[i])
}
return rs
}

View File

@@ -9,67 +9,163 @@ import (
func TestCamelCase(t *testing.T) {
assert := internal.NewAssert(t, "TestCamelCase")
assert.Equal("fooBar", CamelCase("foo_bar"))
assert.Equal("fooBar", CamelCase("Foo-Bar"))
assert.Equal("fooBar", CamelCase("Foo&bar"))
assert.Equal("fooBar", CamelCase("foo bar"))
cases := map[string]string{
"": "",
"foobar": "foobar",
"&FOO:BAR$BAZ": "fooBarBaz",
"fooBar": "fooBar",
"FOObar": "foObar",
"$foo%": "foo",
" $#$Foo 22 bar ": "foo22Bar",
"Foo-#1😄$_%^&*(1bar": "foo11Bar",
}
assert.NotEqual("FooBar", CamelCase("foo_bar"))
for k, v := range cases {
assert.Equal(v, CamelCase(k))
}
}
func TestCapitalize(t *testing.T) {
assert := internal.NewAssert(t, "TestCapitalize")
assert.Equal("Foo", Capitalize("foo"))
assert.Equal("Foo", Capitalize("Foo"))
assert.Equal("Foo", Capitalize("Foo"))
cases := map[string]string{
"": "",
"Foo": "Foo",
"_foo": "_foo",
"foobar": "Foobar",
"fooBar": "Foobar",
"foo Bar": "Foo bar",
"foo-bar": "Foo-bar",
"$foo%": "$foo%",
}
assert.NotEqual("foo", Capitalize("Foo"))
for k, v := range cases {
assert.Equal(v, Capitalize(k))
}
}
func TestKebabCase(t *testing.T) {
assert := internal.NewAssert(t, "TestKebabCase")
assert.Equal("foo-bar", KebabCase("Foo Bar-"))
assert.Equal("foo-bar", KebabCase("foo_Bar"))
assert.Equal("foo-bar", KebabCase("fooBar"))
assert.Equal("f-o-o-b-a-r", KebabCase("__FOO_BAR__"))
cases := map[string]string{
"": "",
"foo-bar": "foo-bar",
"--Foo---Bar-": "foo-bar",
"Foo Bar-": "foo-bar",
"foo_Bar": "foo-bar",
"fooBar": "foo-bar",
"FOOBAR": "foobar",
"FOO_BAR": "foo-bar",
"__FOO_BAR__": "foo-bar",
"$foo@Bar": "foo-bar",
" $#$Foo 22 bar ": "foo-22-bar",
"Foo-#1😄$_%^&*(1bar": "foo-1-1-bar",
}
assert.NotEqual("foo_bar", KebabCase("fooBar"))
for k, v := range cases {
assert.Equal(v, KebabCase(k))
}
}
func TestUpperKebabCase(t *testing.T) {
assert := internal.NewAssert(t, "TestUpperKebabCase")
cases := map[string]string{
"": "",
"foo-bar": "FOO-BAR",
"--Foo---Bar-": "FOO-BAR",
"Foo Bar-": "FOO-BAR",
"foo_Bar": "FOO-BAR",
"fooBar": "FOO-BAR",
"FOOBAR": "FOOBAR",
"FOO_BAR": "FOO-BAR",
"__FOO_BAR__": "FOO-BAR",
"$foo@Bar": "FOO-BAR",
" $#$Foo 22 bar ": "FOO-22-BAR",
"Foo-#1😄$_%^&*(1bar": "FOO-1-1-BAR",
}
for k, v := range cases {
assert.Equal(v, UpperKebabCase(k))
}
}
func TestSnakeCase(t *testing.T) {
assert := internal.NewAssert(t, "TestSnakeCase")
assert.Equal("foo_bar", SnakeCase("Foo Bar-"))
assert.Equal("foo_bar", SnakeCase("foo_Bar"))
assert.Equal("foo_bar", SnakeCase("fooBar"))
assert.Equal("f_o_o_b_a_r", SnakeCase("__FOO_BAR__"))
assert.Equal("a_bbc_s_a_b_b_c", SnakeCase("aBbc-s$@a&%_B.B^C"))
cases := map[string]string{
"": "",
"foo-bar": "foo_bar",
"--Foo---Bar-": "foo_bar",
"Foo Bar-": "foo_bar",
"foo_Bar": "foo_bar",
"fooBar": "foo_bar",
"FOOBAR": "foobar",
"FOO_BAR": "foo_bar",
"__FOO_BAR__": "foo_bar",
"$foo@Bar": "foo_bar",
" $#$Foo 22 bar ": "foo_22_bar",
"Foo-#1😄$_%^&*(1bar": "foo_1_1_bar",
}
assert.NotEqual("foo-bar", SnakeCase("foo_Bar"))
for k, v := range cases {
assert.Equal(v, SnakeCase(k))
}
}
func TestUpperSnakeCase(t *testing.T) {
assert := internal.NewAssert(t, "TestUpperSnakeCase")
cases := map[string]string{
"": "",
"foo-bar": "FOO_BAR",
"--Foo---Bar-": "FOO_BAR",
"Foo Bar-": "FOO_BAR",
"foo_Bar": "FOO_BAR",
"fooBar": "FOO_BAR",
"FOOBAR": "FOOBAR",
"FOO_BAR": "FOO_BAR",
"__FOO_BAR__": "FOO_BAR",
"$foo@Bar": "FOO_BAR",
" $#$Foo 22 bar ": "FOO_22_BAR",
"Foo-#1😄$_%^&*(1bar": "FOO_1_1_BAR",
}
for k, v := range cases {
assert.Equal(v, UpperSnakeCase(k))
}
}
func TestUpperFirst(t *testing.T) {
assert := internal.NewAssert(t, "TestLowerFirst")
assert.Equal("Foo", UpperFirst("foo"))
assert.Equal("BAR", UpperFirst("bAR"))
assert.Equal("FOo", UpperFirst("FOo"))
assert.Equal("FOo大", UpperFirst("fOo大"))
cases := map[string]string{
"": "",
"foo": "Foo",
"bAR": "BAR",
"FOo": "FOo",
"fOo大": "FOo大",
}
assert.NotEqual("Bar", UpperFirst("BAR"))
for k, v := range cases {
assert.Equal(v, UpperFirst(k))
}
}
func TestLowerFirst(t *testing.T) {
assert := internal.NewAssert(t, "TestLowerFirst")
assert.Equal("foo", LowerFirst("foo"))
assert.Equal("bAR", LowerFirst("BAR"))
assert.Equal("fOo", LowerFirst("FOo"))
assert.Equal("fOo大", LowerFirst("FOo大"))
cases := map[string]string{
"": "",
"foo": "foo",
"bAR": "bAR",
"FOo": "fOo",
"fOo大": "fOo大",
}
assert.NotEqual("Bar", LowerFirst("BAR"))
for k, v := range cases {
assert.Equal(v, LowerFirst(k))
}
}
func TestPadEnd(t *testing.T) {

View File

@@ -9,6 +9,10 @@ import (
"os"
"os/exec"
"runtime"
"unicode/utf8"
"github.com/duke-git/lancet/v2/validator"
"golang.org/x/text/encoding/simplifiedchinese"
)
// IsWindows check if current os is windows
@@ -50,27 +54,61 @@ func CompareOsEnv(key, comparedEnv string) bool {
return env == comparedEnv
}
// ExecCommand use shell /bin/bash -c to execute command
// ExecCommand execute command, return the stdout and stderr string of command, and error if error occur
// param `command` is a complete command string, like, ls -a (linux), dir(windows), ping 127.0.0.1
// in linux, use /bin/bash -c to execute command
// in windows, use powershell.exe to execute command
func ExecCommand(command string) (stdout, stderr string, err error) {
var out bytes.Buffer
var errout bytes.Buffer
var errOut bytes.Buffer
cmd := exec.Command("/bin/bash", "-c", command)
if IsWindows() {
cmd = exec.Command("cmd")
cmd = exec.Command("powershell.exe", command)
}
cmd.Stdout = &out
cmd.Stderr = &errout
cmd.Stderr = &errOut
err = cmd.Run()
if err != nil {
stderr = string(errout.Bytes())
if utf8.Valid(errOut.Bytes()) {
stderr = byteToString(errOut.Bytes(), "UTF8")
} else if validator.IsGBK(errOut.Bytes()) {
stderr = byteToString(errOut.Bytes(), "GBK")
}
return
}
data := out.Bytes()
if utf8.Valid(data) {
stdout = byteToString(data, "UTF8")
} else if validator.IsGBK(data) {
stdout = byteToString(data, "GBK")
}
stdout = string(out.Bytes())
return
}
func byteToString(data []byte, charset string) string {
var result string
switch charset {
case "GBK":
decodeBytes, _ := simplifiedchinese.GBK.NewDecoder().Bytes(data)
result = string(decodeBytes)
case "GB18030":
decodeBytes, _ := simplifiedchinese.GB18030.NewDecoder().Bytes(data)
result = string(decodeBytes)
case "UTF8":
fallthrough
default:
result = string(data)
}
return result
}
// GetOsBits get this system bits 32bit or 64bit
// return bit int (32/64)
func GetOsBits() int {

View File

@@ -11,10 +11,10 @@ func TestOsDetection(t *testing.T) {
assert := internal.NewAssert(t, "TestOsJudgment")
osType, _, _ := ExecCommand("echo $OSTYPE")
if strings.Index(osType, "linux") != -1 {
if strings.Contains(osType, "linux") {
assert.Equal(true, IsLinux())
}
if strings.Index(osType, "darwin") != -1 {
if strings.Contains(osType, "darwin") {
assert.Equal(true, IsMac())
}
}
@@ -25,7 +25,9 @@ func TestOsEnvOperation(t *testing.T) {
envNotExist := GetOsEnv("foo")
assert.Equal("", envNotExist)
SetOsEnv("foo", "foo_value")
err := SetOsEnv("foo", "foo_value")
assert.IsNil(err)
envExist := GetOsEnv("foo")
assert.Equal("foo_value", envExist)
@@ -34,7 +36,7 @@ func TestOsEnvOperation(t *testing.T) {
assert.Equal(false, CompareOsEnv("abc", "abc"))
assert.Equal(false, CompareOsEnv("abc", "abc"))
err := RemoveOsEnv("foo")
err = RemoveOsEnv("foo")
if err != nil {
t.Fail()
}
@@ -44,21 +46,26 @@ func TestOsEnvOperation(t *testing.T) {
func TestExecCommand(t *testing.T) {
assert := internal.NewAssert(t, "TestExecCommand")
out, errout, err := ExecCommand("ls")
t.Log("std out: ", out)
t.Log("std err: ", errout)
// linux or mac
stdout, stderr, err := ExecCommand("ls")
t.Log("std out: ", stdout)
t.Log("std err: ", stderr)
assert.Equal("", stderr)
assert.IsNil(err)
out, errout, err = ExecCommand("abc")
t.Log("std out: ", out)
t.Log("std err: ", errout)
if err != nil {
t.Logf("error: %v\n", err)
// windows
stdout, stderr, err = ExecCommand("dir")
t.Log("std out: ", stdout)
t.Log("std err: ", stderr)
if IsWindows() {
assert.IsNil(err)
}
if !IsWindows() {
assert.IsNotNil(err)
}
// error command
stdout, stderr, err = ExecCommand("abc")
t.Log("std out: ", stdout)
t.Log("std err: ", stderr)
assert.IsNotNil(err)
}
func TestGetOsBits(t *testing.T) {

View File

@@ -15,11 +15,24 @@ import (
"unicode"
)
var isAlphaRegexMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]+$`)
var (
alphaMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]+$`)
letterRegexMatcher *regexp.Regexp = regexp.MustCompile(`[a-zA-Z]`)
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-Z]([a-zA-Z0-9\-]+[\.]?)*[a-zA-Z0-9]$`)
emailMatcher *regexp.Regexp = regexp.MustCompile(`\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*`)
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(`^[1-9]\d{5}(18|19|20|21|22)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$`)
chineseMatcher *regexp.Regexp = regexp.MustCompile("[\u4e00-\u9fa5]")
chinesePhoneMatcher *regexp.Regexp = regexp.MustCompile(`\d{3}-\d{8}|\d{4}-\d{7}`)
creditCardMatcher *regexp.Regexp = regexp.MustCompile(`^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\\d{3})\\d{11}|6[27][0-9]{14})$`)
base64Matcher *regexp.Regexp = regexp.MustCompile(`^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$`)
)
// IsAlpha checks if the string contains only letters (a-zA-Z)
func IsAlpha(str string) bool {
return isAlphaRegexMatcher.MatchString(str)
return alphaMatcher.MatchString(str)
}
// IsAllUpper check if the string is all upper case letters A-Z
@@ -62,11 +75,9 @@ func ContainLower(str string) bool {
return false
}
var containLetterRegexMatcher *regexp.Regexp = regexp.MustCompile(`[a-zA-Z]`)
// ContainLetter check if the string contain at least one letter
func ContainLetter(str string) bool {
return containLetterRegexMatcher.MatchString(str)
return letterRegexMatcher.MatchString(str)
}
// IsJSON checks if the string is valid JSON
@@ -86,11 +97,9 @@ func IsFloatStr(str string) bool {
return e == nil
}
var isIntStrRegexMatcher *regexp.Regexp = regexp.MustCompile(`^[\+-]?\d+$`)
// IsIntStr check if the string can convert to a integer.
func IsIntStr(str string) bool {
return isIntStrRegexMatcher.MatchString(str)
return intStrMatcher.MatchString(str)
}
// IsIp check if the string is a ip address.
@@ -125,8 +134,6 @@ func IsPort(str string) bool {
return false
}
var isUrlRegexMatcher *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]*)?$`)
// IsUrl check if the string is url.
func IsUrl(str string) bool {
if str == "" || len(str) >= 2083 || len(str) <= 3 || strings.HasPrefix(str, ".") {
@@ -143,64 +150,48 @@ func IsUrl(str string) bool {
return false
}
return isUrlRegexMatcher.MatchString(str)
return urlMatcher.MatchString(str)
}
var isDnsRegexMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]([a-zA-Z0-9\-]+[\.]?)*[a-zA-Z0-9]$`)
// IsDns check if the string is dns.
func IsDns(dns string) bool {
return isDnsRegexMatcher.MatchString(dns)
return dnsMatcher.MatchString(dns)
}
var isEmailRegexMatcher *regexp.Regexp = regexp.MustCompile(`\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*`)
// IsEmail check if the string is a email address.
func IsEmail(email string) bool {
return isEmailRegexMatcher.MatchString(email)
return emailMatcher.MatchString(email)
}
var isChineseMobileRegexMatcher *regexp.Regexp = regexp.MustCompile("^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$")
// IsChineseMobile check if the string is chinese mobile number.
func IsChineseMobile(mobileNum string) bool {
return isChineseMobileRegexMatcher.MatchString(mobileNum)
return chineseMobileMatcher.MatchString(mobileNum)
}
var isChineseIdNumRegexMatcher *regexp.Regexp = regexp.MustCompile(`^[1-9]\d{5}(18|19|20|21|22)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$`)
// IsChineseIdNum check if the string is chinese id number.
func IsChineseIdNum(id string) bool {
return isChineseIdNumRegexMatcher.MatchString(id)
return chineseIdMatcher.MatchString(id)
}
var containChineseRegexMatcher *regexp.Regexp = regexp.MustCompile("[\u4e00-\u9fa5]")
// ContainChinese check if the string contain mandarin chinese.
func ContainChinese(s string) bool {
return containChineseRegexMatcher.MatchString(s)
return chineseMatcher.MatchString(s)
}
var isChinesePhoneRegexMatcher *regexp.Regexp = regexp.MustCompile(`\d{3}-\d{8}|\d{4}-\d{7}`)
// IsChinesePhone check if the string is chinese phone number.
// Valid chinese phone is xxx-xxxxxxxx or xxxx-xxxxxxx
func IsChinesePhone(phone string) bool {
return isChinesePhoneRegexMatcher.MatchString(phone)
return chinesePhoneMatcher.MatchString(phone)
}
var isCreditCardRegexMatcher *regexp.Regexp = regexp.MustCompile(`^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\\d{3})\\d{11}|6[27][0-9]{14})$`)
// IsCreditCard check if the string is credit card.
func IsCreditCard(creditCart string) bool {
return isCreditCardRegexMatcher.MatchString(creditCart)
return creditCardMatcher.MatchString(creditCart)
}
var isBase64RegexMatcher *regexp.Regexp = regexp.MustCompile(`^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$`)
// IsBase64 check if the string is base64 string.
func IsBase64(base64 string) bool {
return isBase64RegexMatcher.MatchString(base64)
return base64Matcher.MatchString(base64)
}
// IsEmptyString check if the string is empty.
@@ -283,3 +274,40 @@ func IsZeroValue(value any) bool {
return reflect.DeepEqual(rv.Interface(), reflect.Zero(rv.Type()).Interface())
}
// IsGBK check if data encoding is gbk
// Note: this function is implemented by whether double bytes fall within the encoding range of gbk,
// while each byte of utf-8 encoding format falls within the encoding range of gbk.
// Therefore, utf8.valid() should be called first to check whether it is not utf-8 encoding,
// and then call IsGBK() to check gbk encoding. like below
/**
data := []byte("你好")
if utf8.Valid(data) {
fmt.Println("data encoding is utf-8")
}else if(IsGBK(data)) {
fmt.Println("data encoding is GBK")
}
fmt.Println("data encoding is unknown")
**/
func IsGBK(data []byte) bool {
i := 0
for i < len(data) {
if data[i] <= 0xff {
i++
continue
} else {
if data[i] >= 0x81 &&
data[i] <= 0xfe &&
data[i+1] >= 0x40 &&
data[i+1] <= 0xfe &&
data[i+1] != 0xf7 {
i += 2
continue
} else {
return false
}
}
}
return true
}

View File

@@ -4,8 +4,10 @@ import (
"fmt"
"testing"
"time"
"unicode/utf8"
"github.com/duke-git/lancet/v2/internal"
"golang.org/x/text/encoding/simplifiedchinese"
)
func TestIsAllUpper(t *testing.T) {
@@ -388,3 +390,13 @@ func TestIsZeroValue(t *testing.T) {
assert.Equal(false, IsZeroValue(value))
}
}
func TestIsGBK(t *testing.T) {
assert := internal.NewAssert(t, "TestIsGBK")
str := "你好"
gbkData, _ := simplifiedchinese.GBK.NewEncoder().Bytes([]byte(str))
assert.Equal(true, IsGBK(gbkData))
assert.Equal(false, utf8.Valid(gbkData))
}