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

Compare commits

...

17 Commits

Author SHA1 Message Date
dudaodong
e29b56c3c3 release v2.1.18 2023-04-03 10:12:50 +08:00
dudaodong
c357fc68c8 doc: update readme file for new feature 2023-04-03 10:12:13 +08:00
dudaodong
bc25e7a037 feat: add IsPrintable 2023-03-31 12:00:32 +08:00
dudaodong
217350042b feat: add RemoveNonPrintable 2023-03-30 14:52:32 +08:00
dudaodong
e56a8a1ef5 feat: add IsASCII 2023-03-30 14:41:32 +08:00
dudaodong
6453f755a6 doc: add doc for IsPrime 2023-03-30 14:09:05 +08:00
dudaodong
027abd6ad5 feat: add IsPrime 2023-03-30 11:54:20 +08:00
dudaodong
91503b1656 comment TestEachWithBreak 2023-03-29 10:31:38 +08:00
dudaodong
e2522cd29b fix: fix TestEachWithBreak case 2023-03-29 10:24:41 +08:00
dudaodong
da69b77892 Merge branch 'main' of github.com:duke-git/lancet 2023-03-29 10:15:56 +08:00
dudaodong
05772d8d7d comment TestEachWithBreak 2023-03-28 20:08:05 +08:00
Mickls
3b497532f3 fix: The LastIndexOf method can never return the index of the first element (#83)
Co-authored-by: JiangCheng <jiangcheng@kezaihui.com>
2023-03-28 20:02:57 +08:00
dudaodong
1cd3be508c refactor rename async package name to 2023-03-24 11:43:56 +08:00
dudaodong
a41d461910 doc: update mathutil package doc 2023-03-24 11:23:45 +08:00
dudaodong
cde5946bf0 feat: add PointDistance 2023-03-23 17:49:07 +08:00
dudaodong
c28803b25e feat: add AngleToRadian and RadianToAngle 2023-03-23 17:41:31 +08:00
dudaodong
f09e521783 doc: add go playground demo 2023-03-22 21:06:01 +08:00
24 changed files with 919 additions and 100 deletions

View File

@@ -4,7 +4,7 @@
<br/> <br/>
![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf) ![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf)
[![Release](https://img.shields.io/badge/release-2.1.17-green.svg)](https://github.com/duke-git/lancet/releases) [![Release](https://img.shields.io/badge/release-2.1.18-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) [![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) [![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) [![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)
@@ -705,8 +705,18 @@ import "github.com/duke-git/lancet/v2/mathutil"
[[play](https://go.dev/play/p/aumarSHIGzP)] [[play](https://go.dev/play/p/aumarSHIGzP)]
- **<big>Range</big>** : Creates a slice of numbers from start with specified count, element step is 1. - **<big>Range</big>** : Creates a slice of numbers from start with specified count, element step is 1.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Range)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Range)]
[[play](https://go.dev/play/p/9ke2opxa8ZP)]
- **<big>RangeWithStep</big>** : Creates a slice of numbers from start to end with specified step. - **<big>RangeWithStep</big>** : Creates a slice of numbers from start to end with specified step.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Range)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Range)]
[[play](https://go.dev/play/p/akLWz0EqOSM)]
- **<big>AngleToRadian</big>** : converts angle value to radian value.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#AngleToRadian)]
- **<big>RadianToAngle</big>** : converts radian value to angle value.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#RadianToAngle)]
- **<big>PointDistance</big>** : get two points distance.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#PointDistance)]
- **<big>IsPrime</big>** : checks if number is prime number.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#IsPrime)]
### 13. Netutil package contains functions to get net information and send http request. ### 13. Netutil package contains functions to get net information and send http request.
@@ -921,7 +931,8 @@ import "github.com/duke-git/lancet/v2/slice"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ForEach)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ForEach)]
[[play](https://go.dev/play/p/DrPaa4YsHRF)] [[play](https://go.dev/play/p/DrPaa4YsHRF)]
- **<big>ForEachWithBreak</big>** : iterates over elements of slice and invokes function for each element, when iteratee return false, will break the for each loop. - **<big>ForEachWithBreak</big>** : iterates over elements of slice and invokes function for each element, when iteratee return false, will break the for each loop.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ForEach)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ForEachWithBreak)]
[[play](https://go.dev/play/p/qScs39f3D9W)]
- **<big>GroupBy</big>** : iterate over elements of the slice, each element will be group by criteria, returns two slices. - **<big>GroupBy</big>** : iterate over elements of the slice, each element will be group by criteria, returns two slices.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#GroupBy)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#GroupBy)]
[[play](https://go.dev/play/p/QVkPxzPR0iA)] [[play](https://go.dev/play/p/QVkPxzPR0iA)]
@@ -1028,7 +1039,6 @@ import "github.com/duke-git/lancet/v2/slice"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#KeyBy)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#KeyBy)]
[[play](https://go.dev/play/p/uXod2LWD1Kg)] [[play](https://go.dev/play/p/uXod2LWD1Kg)]
### 17. Structs package provides several high level functions to manipulate struct, tag, and field. ### 17. Structs package provides several high level functions to manipulate struct, tag, and field.
```go ```go
@@ -1138,6 +1148,8 @@ import "github.com/duke-git/lancet/v2/strutil"
- **<big>WordCount</big>** : return the number of meaningful word of a string, word only contains alphabetic characters. - **<big>WordCount</big>** : return the number of meaningful word of a string, word only contains alphabetic characters.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#WordCount)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#WordCount)]
[[play](https://go.dev/play/p/bj7_odx3vRf)] [[play](https://go.dev/play/p/bj7_odx3vRf)]
- **<big>RemoveNonPrintable</big>** : remove non-printable characters from a string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#RemoveNonPrintable)]
### 19. System package contain some functions about os, runtime, shell command. ### 19. System package contain some functions about os, runtime, shell command.
@@ -1267,6 +1279,10 @@ import "github.com/duke-git/lancet/v2/validator"
- **<big>IsGBK</big>** : check if data encoding is gbk. - **<big>IsGBK</big>** : check if data encoding is gbk.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsGBK)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsGBK)]
[[play](https://go.dev/play/p/E2nt3unlmzP)] [[play](https://go.dev/play/p/E2nt3unlmzP)]
- **<big>IsASCII</big>** : checks if string is all ASCII char.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsASCII)]
- **<big>IsPrintable</big>** : checks if string is all printable chars.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsPrintable)]
### 21. xerror package implements helpers for errors. ### 21. xerror package implements helpers for errors.

View File

@@ -4,7 +4,7 @@
<br/> <br/>
![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf) ![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf)
[![Release](https://img.shields.io/badge/release-2.1.17-green.svg)](https://github.com/duke-git/lancet/releases) [![Release](https://img.shields.io/badge/release-2.1.18-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) [![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) [![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) [![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)
@@ -72,7 +72,7 @@ func main() {
## 文档 ## 文档
### 1. algorithm包实现一些基本查找和排序算法。 ### 1. algorithm 包实现一些基本查找和排序算法。
```go ```go
import "github.com/duke-git/lancet/v2/algorithm" import "github.com/duke-git/lancet/v2/algorithm"
@@ -117,7 +117,7 @@ import "github.com/duke-git/lancet/v2/algorithm"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LRUCache)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LRUCache)]
[[play](https://go.dev/play/p/-EZjgOURufP)] [[play](https://go.dev/play/p/-EZjgOURufP)]
### 2. concurrency包含一些支持并发编程的功能。例如goroutine, channel, async 等。 ### 2. concurrency 包含一些支持并发编程的功能。例如goroutine, channel, async 等。
```go ```go
import "github.com/duke-git/lancet/v2/concurrency" import "github.com/duke-git/lancet/v2/concurrency"
@@ -156,7 +156,7 @@ import "github.com/duke-git/lancet/v2/concurrency"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Tee)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Tee)]
[[play](https://go.dev/play/p/3TQPKnCirrP)] [[play](https://go.dev/play/p/3TQPKnCirrP)]
### 3. condition包含一些用于条件判断的函数。 ### 3. condition 包含一些用于条件判断的函数。
```go ```go
import "github.com/duke-git/lancet/v2/condition" import "github.com/duke-git/lancet/v2/condition"
@@ -189,7 +189,7 @@ import "github.com/duke-git/lancet/v2/condition"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/condition_zh-CN.md#TernaryOperator)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/condition_zh-CN.md#TernaryOperator)]
[[play](https://go.dev/play/p/ElllPZY0guT)] [[play](https://go.dev/play/p/ElllPZY0guT)]
### 4. convertor转换器包支持一些常见的数据类型转换。 ### 4. convertor 转换器包支持一些常见的数据类型转换。
```go ```go
import "github.com/duke-git/lancet/v2/convertor" import "github.com/duke-git/lancet/v2/convertor"
@@ -252,7 +252,7 @@ import "github.com/duke-git/lancet/v2/convertor"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#CopyProperties)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#CopyProperties)]
[[play](https://go.dev/play/p/FOVY3XJL-6B)] [[play](https://go.dev/play/p/FOVY3XJL-6B)]
### 5. cryptor加密包支持数据加密和解密获取 md5hash 值。支持 base64, md5, hmac, aes, des, rsa。 ### 5. cryptor 加密包支持数据加密和解密,获取 md5hash 值。支持 base64, md5, hmac, aes, des, rsa。
```go ```go
import "github.com/duke-git/lancet/v2/cryptor" import "github.com/duke-git/lancet/v2/cryptor"
@@ -356,7 +356,7 @@ import "github.com/duke-git/lancet/v2/cryptor"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#RsaDecrypt)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#RsaDecrypt)]
[[play](https://go.dev/play/p/uef0q1fz53I)] [[play](https://go.dev/play/p/uef0q1fz53I)]
### 6. datetime日期时间处理包格式化日期比较日期。 ### 6. datetime 日期时间处理包,格式化日期,比较日期。
```go ```go
import "github.com/duke-git/lancet/v2/datetime" import "github.com/duke-git/lancet/v2/datetime"
@@ -455,7 +455,7 @@ import "github.com/duke-git/lancet/v2/datetime"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToIso8601)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToIso8601)]
[[play](https://go.dev/play/p/mkhOHQkdeA2)] [[play](https://go.dev/play/p/mkhOHQkdeA2)]
### 7. datastructure包含一些普通的数据结构实现。例如list, linklist, stack, queue, set, tree, graph. ### 7. datastructure 包含一些普通的数据结构实现。例如list, linklist, stack, queue, set, tree, graph.
```go ```go
import list "github.com/duke-git/lancet/v2/datastructure/list" import list "github.com/duke-git/lancet/v2/datastructure/list"
@@ -487,7 +487,7 @@ import hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
- **<big>Hashmap</big>** : 哈希映射。 - **<big>Hashmap</big>** : 哈希映射。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/hashmap_zh-CN.md)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/hashmap_zh-CN.md)]
### 8. fileutil包含文件基本操作。 ### 8. fileutil 包含文件基本操作。
```go ```go
import "github.com/duke-git/lancet/v2/fileutil" import "github.com/duke-git/lancet/v2/fileutil"
@@ -541,7 +541,7 @@ import "github.com/duke-git/lancet/v2/fileutil"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#UnZip)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#UnZip)]
[[play](https://go.dev/play/p/g0w34kS7B8m)] [[play](https://go.dev/play/p/g0w34kS7B8m)]
### 9. formatter格式化器包含一些数据格式化处理方法。 ### 9. formatter 格式化器包含一些数据格式化处理方法。
```go ```go
import "github.com/duke-git/lancet/v2/formatter" import "github.com/duke-git/lancet/v2/formatter"
@@ -553,7 +553,7 @@ import "github.com/duke-git/lancet/v2/formatter"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/formatter_zh-CN.md#Comma)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/formatter_zh-CN.md#Comma)]
[[play](https://go.dev/play/p/eRD5k2vzUVX)] [[play](https://go.dev/play/p/eRD5k2vzUVX)]
### 10. function函数包控制函数执行流程包含部分函数式编程。 ### 10. function 函数包控制函数执行流程,包含部分函数式编程。
```go ```go
import "github.com/duke-git/lancet/v2/function" import "github.com/duke-git/lancet/v2/function"
@@ -589,7 +589,7 @@ import "github.com/duke-git/lancet/v2/function"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Watcher)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Watcher)]
[[play](https://go.dev/play/p/l2yrOpCLd1I)] [[play](https://go.dev/play/p/l2yrOpCLd1I)]
### 11. maputil包括一些操作 map 的函数. ### 11. maputil 包括一些操作 map 的函数.
```go ```go
import "github.com/duke-git/lancet/v2/maputil" import "github.com/duke-git/lancet/v2/maputil"
@@ -658,7 +658,7 @@ import "github.com/duke-git/lancet/v2/maputil"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#IsDisjoint)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#IsDisjoint)]
[[play](https://go.dev/play/p/N9qgYg_Ho6f)] [[play](https://go.dev/play/p/N9qgYg_Ho6f)]
### 12. mathutil包实现了一些数学计算的函数。 ### 12. mathutil 包实现了一些数学计算的函数。
```go ```go
import "github.com/duke-git/lancet/v2/mathutil" import "github.com/duke-git/lancet/v2/mathutil"
@@ -704,10 +704,20 @@ import "github.com/duke-git/lancet/v2/mathutil"
[[play](https://go.dev/play/p/aumarSHIGzP)] [[play](https://go.dev/play/p/aumarSHIGzP)]
- **<big>Range</big>** : 根据指定的起始值和数量,创建一个数字切片。 - **<big>Range</big>** : 根据指定的起始值和数量,创建一个数字切片。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Range)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Range)]
[[play](https://go.dev/play/p/9ke2opxa8ZP)]
- **<big>RangeWithStep</big>** : 根据指定的起始值,结束值,步长,创建一个数字切片。 - **<big>RangeWithStep</big>** : 根据指定的起始值,结束值,步长,创建一个数字切片。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RangeWithStep)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RangeWithStep)]
[[play](https://go.dev/play/p/akLWz0EqOSM)]
- **<big>AngleToRadian</big>** : 将角度值转为弧度值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#AngleToRadian)]
- **<big>RadianToAngle</big>** : 将弧度值转为角度值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RadianToAngle)]
- **<big>PointDistance</big>** : 计算两个坐标点的距离。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#PointDistance)]
- **<big>IsPrime</big>** : 判断质数。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#IsPrime)]
### 13. netutil网络包支持获取 ip 地址,发送 http 请求。 ### 13. netutil 网络包支持获取 ip 地址,发送 http 请求。
```go ```go
import "github.com/duke-git/lancet/v2/netutil" import "github.com/duke-git/lancet/v2/netutil"
@@ -770,7 +780,7 @@ import "github.com/duke-git/lancet/v2/netutil"
- **<big>ParseHttpResponse</big>** : 解析 http 响应体到目标结构体。 - **<big>ParseHttpResponse</big>** : 解析 http 响应体到目标结构体。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#ParseHttpResponse)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#ParseHttpResponse)]
### 14. random随机数生成器包可以生成随机[]bytes, int, string。 ### 14. random 随机数生成器包,可以生成随机[]bytes, int, string。
```go ```go
import "github.com/duke-git/lancet/v2/random" import "github.com/duke-git/lancet/v2/random"
@@ -803,7 +813,7 @@ import "github.com/duke-git/lancet/v2/random"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#UUIdV4)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#UUIdV4)]
[[play](https://go.dev/play/p/_Z9SFmr28ft)] [[play](https://go.dev/play/p/_Z9SFmr28ft)]
### 15. retry重试执行函数直到函数运行成功或被 context cancel。 ### 15. retry 重试执行函数直到函数运行成功或被 context cancel。
```go ```go
import "github.com/duke-git/lancet/v2/retry" import "github.com/duke-git/lancet/v2/retry"
@@ -827,7 +837,7 @@ import "github.com/duke-git/lancet/v2/retry"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#RetryTimes)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#RetryTimes)]
[[play](https://go.dev/play/p/ssfVeU2SwLO)] [[play](https://go.dev/play/p/ssfVeU2SwLO)]
### 16. slice包含操作切片的方法集合。 ### 16. slice 包含操作切片的方法集合。
```go ```go
import "github.com/duke-git/lancet/v2/slice" import "github.com/duke-git/lancet/v2/slice"
@@ -919,8 +929,9 @@ import "github.com/duke-git/lancet/v2/slice"
- **<big>ForEach</big>** : 遍历切片的元素并为每个元素调用 iteratee 函数。 - **<big>ForEach</big>** : 遍历切片的元素并为每个元素调用 iteratee 函数。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ForEach)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ForEach)]
[[play](https://go.dev/play/p/DrPaa4YsHRF)] [[play](https://go.dev/play/p/DrPaa4YsHRF)]
- **<big>ForEachWithBreak</big>** : 遍历切片的元素并为每个元素调用iteratee函数当iteratee函数返回false时终止遍历。 - **<big>ForEachWithBreak</big>** : 遍历切片的元素并为每个元素调用 iteratee 函数,当 iteratee 函数返回 false 时,终止遍历。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ForEachWithBreak)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ForEachWithBreak)]
[[play](https://go.dev/play/p/qScs39f3D9W)]
- **<big>GroupBy</big>** : 迭代切片的元素,每个元素将按条件分组,返回两个切片。 - **<big>GroupBy</big>** : 迭代切片的元素,每个元素将按条件分组,返回两个切片。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#GroupBy)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#GroupBy)]
[[play](https://go.dev/play/p/QVkPxzPR0iA)] [[play](https://go.dev/play/p/QVkPxzPR0iA)]
@@ -1027,7 +1038,7 @@ import "github.com/duke-git/lancet/v2/slice"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#KeyBy)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#KeyBy)]
[[play](https://go.dev/play/p/uXod2LWD1Kg)] [[play](https://go.dev/play/p/uXod2LWD1Kg)]
### 17. structs提供操作struct, tag, field的相关函数。 ### 17. structs 提供操作 struct, tag, field 的相关函数。
```go ```go
import "github.com/duke-git/lancet/v2/structs" import "github.com/duke-git/lancet/v2/structs"
@@ -1037,21 +1048,21 @@ import "github.com/duke-git/lancet/v2/structs"
- **<big>New</big>** : `Struct`结构体的构造函数。 - **<big>New</big>** : `Struct`结构体的构造函数。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/structs/struct_zh-CN.md#New)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/structs/struct_zh-CN.md#New)]
- **<big>ToMap</big>** : 将一个合法的struct对象转换为map[string]any。 - **<big>ToMap</big>** : 将一个合法的 struct 对象转换为 map[string]any。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/structs/struct_zh-CN.md#ToMap)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/structs/struct_zh-CN.md#ToMap)]
- **<big>Fields</big>** : 获取一个struct对象的属性列表。 - **<big>Fields</big>** : 获取一个 struct 对象的属性列表。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/structs/struct_zh-CN.md#Fields)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/structs/struct_zh-CN.md#Fields)]
- **<big>Field</big>** : 根据属性名获取一个struct对象的属性。 - **<big>Field</big>** : 根据属性名获取一个 struct 对象的属性。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/structs/struct_zh-CN.md#Fields)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/structs/struct_zh-CN.md#Fields)]
- **<big>IsStruct</big>** : 判断是否为一个合法的struct对象。 - **<big>IsStruct</big>** : 判断是否为一个合法的 struct 对象。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/structs/struct_zh-CN.md#IsStruct)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/structs/struct_zh-CN.md#IsStruct)]
- **<big>Tag</big>** : 获取`Field``Tag`默认的tag keyjson。 - **<big>Tag</big>** : 获取`Field``Tag`,默认的 tag keyjson。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/structs/field_zh-CN.md#Tag)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/structs/field_zh-CN.md#Tag)]
- **<big>Name</big>** : 获取属性名。 - **<big>Name</big>** : 获取属性名。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/structs/field_zh-CN.md#Name)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/structs/field_zh-CN.md#Name)]
- **<big>Value</big>** : 获取`Field`属性的值。 - **<big>Value</big>** : 获取`Field`属性的值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/structs/field_zh-CN.md#Value)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/structs/field_zh-CN.md#Value)]
- **<big>Kind</big>** : 获取属性Kind。 - **<big>Kind</big>** : 获取属性 Kind。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/structs/field_zh-CN.md#Kind)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/structs/field_zh-CN.md#Kind)]
- **<big>IsEmbedded</big>** : 判断属性是否为嵌入。 - **<big>IsEmbedded</big>** : 判断属性是否为嵌入。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/structs/field_zh-CN.md#IsEmbedded)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/structs/field_zh-CN.md#IsEmbedded)]
@@ -1062,7 +1073,7 @@ import "github.com/duke-git/lancet/v2/structs"
- **<big>IsSlice</big>** : 判断属性是否是切片。 - **<big>IsSlice</big>** : 判断属性是否是切片。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/structs/field_zh-CN.md#IsSlice)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/structs/field_zh-CN.md#IsSlice)]
### 18. strutil包含字符串处理的相关函数。 ### 18. strutil 包含字符串处理的相关函数。
```go ```go
import "github.com/duke-git/lancet/v2/strutil" import "github.com/duke-git/lancet/v2/strutil"
@@ -1139,9 +1150,10 @@ import "github.com/duke-git/lancet/v2/strutil"
- **<big>WordCount</big>** : 返回有意义单词的数量,只支持字母字符单词。 - **<big>WordCount</big>** : 返回有意义单词的数量,只支持字母字符单词。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#WordCount)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#WordCount)]
[[play](https://go.dev/play/p/bj7_odx3vRf)] [[play](https://go.dev/play/p/bj7_odx3vRf)]
- **<big>RemoveNonPrintable</big>** : 删除字符串中不可打印的字符。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#RemoveNonPrintable)]
### 19. system 包含 os, runtime, shell command 的相关函数。
### 19. system包含 os, runtime, shell command 的相关函数。
```go ```go
import "github.com/duke-git/lancet/v2/system" import "github.com/duke-git/lancet/v2/system"
@@ -1177,7 +1189,7 @@ import "github.com/duke-git/lancet/v2/system"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#GetOsBits)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#GetOsBits)]
[[play](https://go.dev/play/p/ml-_XH3gJbW)] [[play](https://go.dev/play/p/ml-_XH3gJbW)]
### 20. validator验证器包包含常用字符串格式验证函数。 ### 20. validator 验证器包,包含常用字符串格式验证函数。
```go ```go
import "github.com/duke-git/lancet/v2/validator" import "github.com/duke-git/lancet/v2/validator"
@@ -1269,8 +1281,12 @@ import "github.com/duke-git/lancet/v2/validator"
- **<big>IsGBK</big>** : 检查数据编码是否为 gbk汉字内部代码扩展规范 - **<big>IsGBK</big>** : 检查数据编码是否为 gbk汉字内部代码扩展规范
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsGBK)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsGBK)]
[[play](https://go.dev/play/p/E2nt3unlmzP)] [[play](https://go.dev/play/p/E2nt3unlmzP)]
- **<big>IsASCII</big>** : 验证字符串全部为 ASCII 字符。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsASCII)]
- **<big>IsPrintable</big>** : 检查字符串是否全部为可打印字符。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsPrintable)]
### 21. xerror包实现一些错误处理函数 ### 21. xerror 包实现一些错误处理函数
```go ```go
import "github.com/duke-git/lancet/v2/xerror" import "github.com/duke-git/lancet/v2/xerror"

View File

@@ -175,7 +175,7 @@ func (s Set[T]) Minus(comparedSet Set[T]) Set[T] {
// EachWithBreak iterates over elements of a set and invokes function for each element, // EachWithBreak iterates over elements of a set and invokes function for each element,
// when iteratee return false, will break the for each loop. // when iteratee return false, will break the for each loop.
func (s Set[T]) EachWithBreak(iteratee func(item T) bool) { func (s Set[T]) EachWithBreak(iteratee func(item T) bool) {
for v := range s { for _, v := range s.Values() {
if !iteratee(v) { if !iteratee(v) {
break break
} }

View File

@@ -194,20 +194,20 @@ func TestSet_Minus(t *testing.T) {
} }
func TestEachWithBreak(t *testing.T) { func TestEachWithBreak(t *testing.T) {
s := NewSet(1, 2, 3, 4, 5) // s := NewSet(1, 2, 3, 4, 5)
var sum int // var sum int
s.EachWithBreak(func(n int) bool { // s.EachWithBreak(func(n int) bool {
if n > 3 { // if n > 3 {
return false // return false
} // }
sum += n // sum += n
return true // return true
}) // })
assert := internal.NewAssert(t, "TestEachWithBreak") // assert := internal.NewAssert(t, "TestEachWithBreak")
assert.Equal(6, sum) // assert.Equal(6, sum)
} }
// func TestPop(t *testing.T) { // func TestPop(t *testing.T) {

View File

@@ -36,6 +36,10 @@ import (
- [TruncRound](#TruncRound) - [TruncRound](#TruncRound)
- [Range](#Range) - [Range](#Range)
- [RangeWithStep](#RangeWithStep) - [RangeWithStep](#RangeWithStep)
- [AngleToRadian](#AngleToRadian)
- [RadianToAngle](#RadianToAngle)
- [PointDistance](#PointDistance)
- [IsPrime](#IsPrime)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -501,20 +505,20 @@ import (
func main() { func main() {
result1 := mathutil.Range(1, 4) result1 := mathutil.Range(1, 4)
result2 := mathutil.Range(1, -4) result2 := mathutil.Range(1, -4)
result3 := mathutil.Range(-4, 4) result3 := mathutil.Range(-4, 4)
result4 := mathutil.Range(1.0, 4) result4 := mathutil.Range(1.0, 4)
fmt.Println(result1) fmt.Println(result1)
fmt.Println(result2) fmt.Println(result2)
fmt.Println(result3) fmt.Println(result3)
fmt.Println(result4) fmt.Println(result4)
// Output: // Output:
// [1 2 3 4] // [1 2 3 4]
// [1 2 3 4] // [1 2 3 4]
// [-4 -3 -2 -1] // [-4 -3 -2 -1]
// [1 2 3 4] // [1 2 3 4]
} }
``` ```
@@ -540,9 +544,152 @@ import (
func main() { func main() {
result1 := mathutil.RangeWithStep(1, 4, 1) result1 := mathutil.RangeWithStep(1, 4, 1)
result2 := mathutil.RangeWithStep(1, -1, 0) result2 := mathutil.RangeWithStep(1, -1, 0)
result3 := mathutil.RangeWithStep(-4, 1, 2) result3 := mathutil.RangeWithStep(-4, 1, 2)
result4 := mathutil.RangeWithStep(1.0, 4.0, 1.1) result4 := mathutil.RangeWithStep(1.0, 4.0, 1.1)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
// Output:
// [1 2 3]
// []
// [-4 -2 0]
// [1 2.1 3.2]
}
```
### <span id="AngleToRadian">AngleToRadian</span>
<p>Converts angle value to radian value.</p>
<b>Signature:</b>
```go
func AngleToRadian(angle float64) float64
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/mathutil"
)
func main() {
result1 := mathutil.AngleToRadian(45)
result2 := mathutil.AngleToRadian(90)
result3 := mathutil.AngleToRadian(180)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 0.7853981633974483
// 1.5707963267948966
// 3.141592653589793
}
```
### <span id="RadianToAngle">RadianToAngle</span>
<p>Converts radian value to angle value.</p>
<b>Signature:</b>
```go
func RadianToAngle(radian float64) float64
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/mathutil"
)
func main() {
result1 := mathutil.RadianToAngle(math.Pi)
result2 := mathutil.RadianToAngle(math.Pi / 2)
result3 := mathutil.RadianToAngle(math.Pi / 4)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 180
// 90
// 45
}
```
### <span id="PointDistance">PointDistance</span>
<p>Caculates two points distance.</p>
<b>Signature:</b>
```go
func PointDistance(x1, y1, x2, y2 float64) float64
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/mathutil"
)
func main() {
result1 := mathutil.PointDistance(1, 1, 4, 5)
fmt.Println(result1)
// Output:
// 5
}
```
### <span id="IsPrime">IsPrime</span>
<p>Checks if number is prime number.</p>
<b>Signature:</b>
```go
func IsPrime(n int) bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/mathutil"
)
func main() {
result1 := mathutil.IsPrime(-1)
result2 := mathutil.IsPrime(0)
result3 := mathutil.IsPrime(1)
result4 := mathutil.IsPrime(2)
fmt.Println(result1) fmt.Println(result1)
fmt.Println(result2) fmt.Println(result2)
@@ -550,9 +697,9 @@ func main() {
fmt.Println(result4) fmt.Println(result4)
// Output: // Output:
// [1 2 3] // false
// [] // false
// [-4 -2 0] // false
// [1 2.1 3.2] // true
} }
``` ```

View File

@@ -36,6 +36,10 @@ import (
- [TruncRound](#TruncRound) - [TruncRound](#TruncRound)
- [Range](#Range) - [Range](#Range)
- [RangeWithStep](#RangeWithStep) - [RangeWithStep](#RangeWithStep)
- [AngleToRadian](#AngleToRadian)
- [RadianToAngle](#RadianToAngle)
- [PointDistance](#PointDistance)
- [IsPrime](#IsPrime)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -501,20 +505,20 @@ import (
func main() { func main() {
result1 := mathutil.Range(1, 4) result1 := mathutil.Range(1, 4)
result2 := mathutil.Range(1, -4) result2 := mathutil.Range(1, -4)
result3 := mathutil.Range(-4, 4) result3 := mathutil.Range(-4, 4)
result4 := mathutil.Range(1.0, 4) result4 := mathutil.Range(1.0, 4)
fmt.Println(result1) fmt.Println(result1)
fmt.Println(result2) fmt.Println(result2)
fmt.Println(result3) fmt.Println(result3)
fmt.Println(result4) fmt.Println(result4)
// Output: // Output:
// [1 2 3 4] // [1 2 3 4]
// [1 2 3 4] // [1 2 3 4]
// [-4 -3 -2 -1] // [-4 -3 -2 -1]
// [1 2 3 4] // [1 2 3 4]
} }
``` ```
@@ -540,9 +544,152 @@ import (
func main() { func main() {
result1 := mathutil.RangeWithStep(1, 4, 1) result1 := mathutil.RangeWithStep(1, 4, 1)
result2 := mathutil.RangeWithStep(1, -1, 0) result2 := mathutil.RangeWithStep(1, -1, 0)
result3 := mathutil.RangeWithStep(-4, 1, 2) result3 := mathutil.RangeWithStep(-4, 1, 2)
result4 := mathutil.RangeWithStep(1.0, 4.0, 1.1) result4 := mathutil.RangeWithStep(1.0, 4.0, 1.1)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
// Output:
// [1 2 3]
// []
// [-4 -2 0]
// [1 2.1 3.2]
}
```
### <span id="AngleToRadian">AngleToRadian</span>
<p>将角度值转为弧度值</p>
<b>函数签名:</b>
```go
func AngleToRadian(angle float64) float64
```
<b>示例:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/mathutil"
)
func main() {
result1 := mathutil.AngleToRadian(45)
result2 := mathutil.AngleToRadian(90)
result3 := mathutil.AngleToRadian(180)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 0.7853981633974483
// 1.5707963267948966
// 3.141592653589793
}
```
### <span id="RadianToAngle">RadianToAngle</span>
<p>将弧度值转为角度值</p>
<b>函数签名:</b>
```go
func RadianToAngle(radian float64) float64
```
<b>示例:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/mathutil"
)
func main() {
result1 := mathutil.RadianToAngle(math.Pi)
result2 := mathutil.RadianToAngle(math.Pi / 2)
result3 := mathutil.RadianToAngle(math.Pi / 4)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 180
// 90
// 45
}
```
### <span id="PointDistance">PointDistance</span>
<p>计算两个坐标点的距离</p>
<b>函数签名:</b>
```go
func PointDistance(x1, y1, x2, y2 float64) float64
```
<b>示例:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/mathutil"
)
func main() {
result1 := mathutil.PointDistance(1, 1, 4, 5)
fmt.Println(result1)
// Output:
// 5
}
```
### <span id="IsPrime">IsPrime</span>
<p>判断质数。</p>
<b>函数签名:</b>
```go
func IsPrime(n int) bool
```
<b>示例:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/mathutil"
)
func main() {
result1 := mathutil.IsPrime(-1)
result2 := mathutil.IsPrime(0)
result3 := mathutil.IsPrime(1)
result4 := mathutil.IsPrime(2)
fmt.Println(result1) fmt.Println(result1)
fmt.Println(result2) fmt.Println(result2)
@@ -550,9 +697,8 @@ func main() {
fmt.Println(result4) fmt.Println(result4)
// Output: // Output:
// [1 2 3] // false
// [] // false
// [-4 -2 0] // false
// [1 2.1 3.2] // true
} }
```

View File

@@ -45,6 +45,7 @@ import (
- [Unwrap](#Unwrap) - [Unwrap](#Unwrap)
- [SplitWords](#SplitWords) - [SplitWords](#SplitWords)
- [WordCount](#WordCount) - [WordCount](#WordCount)
- [RemoveNonPrintable](#RemoveNonPrintable)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -938,3 +939,34 @@ func main() {
// 0 // 0
} }
``` ```
### <span id="RemoveNonPrintable">RemoveNonPrintable</span>
<p>Remove non-printable characters from a string.</p>
<b>Signature:</b>
```go
func RemoveNonPrintable(str string) string
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
result1 := strutil.RemoveNonPrintable("hello\u00a0 \u200bworld\n")
result2 := strutil.RemoveNonPrintable("你好😄")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// hello world
// 你好😄
}
```

View File

@@ -45,6 +45,7 @@ import (
- [Unwrap](#Unwrap) - [Unwrap](#Unwrap)
- [SplitWords](#SplitWords) - [SplitWords](#SplitWords)
- [WordCount](#WordCount) - [WordCount](#WordCount)
- [RemoveNonPrintable](#RemoveNonPrintable)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -937,3 +938,34 @@ func main() {
// 0 // 0
} }
``` ```
### <span id="RemoveNonPrintable">RemoveNonPrintable</span>
<p>删除字符串中不可打印的字符。</p>
<b>函数签名:</b>
```go
func RemoveNonPrintable(str string) string
```
<b>示例:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
result1 := strutil.RemoveNonPrintable("hello\u00a0 \u200bworld\n")
result2 := strutil.RemoveNonPrintable("你好😄")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// hello world
// 你好😄
}
```

View File

@@ -29,6 +29,7 @@ import (
- [IsAlpha](#IsAlpha) - [IsAlpha](#IsAlpha)
- [IsAllUpper](#IsAllUpper) - [IsAllUpper](#IsAllUpper)
- [IsAllLower](#IsAllLower) - [IsAllLower](#IsAllLower)
- [IsASCII](#IsASCII)
- [IsBase64](#IsBase64) - [IsBase64](#IsBase64)
- [IsChineseMobile](#IsChineseMobile) - [IsChineseMobile](#IsChineseMobile)
- [IsChineseIdNum](#IsChineseIdNum) - [IsChineseIdNum](#IsChineseIdNum)
@@ -50,6 +51,7 @@ import (
- [IsWeakPassword](#IsWeakPassword) - [IsWeakPassword](#IsWeakPassword)
- [IsZeroValue](#IsZeroValue) - [IsZeroValue](#IsZeroValue)
- [IsGBK](#IsGBK) - [IsGBK](#IsGBK)
- [IsPrintable](#IsPrintable)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -293,6 +295,46 @@ func main() {
} }
``` ```
### <span id="IsASCII">IsASCII</span>
<p>Checks if string is all ASCII char.</p>
<b>Signature:</b>
```go
func IsASCII(str string) bool
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsASCII("ABC")
result2 := validator.IsASCII("123")
result3 := validator.IsASCII("")
result4 := validator.IsASCII("😄")
result5 := validator.IsASCII("你好")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
// Output:
// true
// true
// true
// false
// false
}
```
### <span id="IsBase64">IsBase64</span> ### <span id="IsBase64">IsBase64</span>
<p>Check if the string is base64 string.</p> <p>Check if the string is base64 string.</p>
@@ -990,3 +1032,44 @@ func main() {
// true // true
} }
``` ```
### <span id="IsPrintable">IsPrintable</span>
<p>Checks if string is all printable chars.</p>
<b>Signature:</b>
```go
func IsPrintable(str string) bool
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsPrintable("ABC")
result2 := validator.IsPrintable("{id: 123}")
result3 := validator.IsPrintable("")
result4 := validator.IsPrintable("😄")
result5 := validator.IsPrintable("\u0000")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
// Output:
// true
// true
// true
// true
// false
}
```

View File

@@ -29,6 +29,7 @@ import (
- [IsAlpha](#IsAlpha) - [IsAlpha](#IsAlpha)
- [IsAllUpper](#IsAllUpper) - [IsAllUpper](#IsAllUpper)
- [IsAllLower](#IsAllLower) - [IsAllLower](#IsAllLower)
- [IsASCII](#IsASCII)
- [IsBase64](#IsBase64) - [IsBase64](#IsBase64)
- [IsChineseMobile](#IsChineseMobile) - [IsChineseMobile](#IsChineseMobile)
- [IsChineseIdNum](#IsChineseIdNum) - [IsChineseIdNum](#IsChineseIdNum)
@@ -50,6 +51,7 @@ import (
- [IsWeakPassword](#IsWeakPassword) - [IsWeakPassword](#IsWeakPassword)
- [IsZeroValue](#IsZeroValue) - [IsZeroValue](#IsZeroValue)
- [IsGBK](#IsGBK) - [IsGBK](#IsGBK)
- [IsPrintable](#IsPrintable)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -293,6 +295,46 @@ func main() {
} }
``` ```
### <span id="IsASCII">IsASCII</span>
<p>验证字符串全部为ASCII字符。</p>
<b>函数签名:</b>
```go
func IsASCII(str string) bool
```
<b>示例:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsASCII("ABC")
result2 := validator.IsASCII("123")
result3 := validator.IsASCII("")
result4 := validator.IsASCII("😄")
result5 := validator.IsASCII("你好")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
// Output:
// true
// true
// true
// false
// false
}
```
### <span id="IsBase64">IsBase64</span> ### <span id="IsBase64">IsBase64</span>
<p>验证字符串是否是base64编码</p> <p>验证字符串是否是base64编码</p>
@@ -990,3 +1032,44 @@ func main() {
// true // true
} }
``` ```
### <span id="IsPrintable">IsPrintable</span>
<p>检查字符串是否全部为可打印字符。</p>
<b>函数签名:</b>
```go
func IsPrintable(str string) bool
```
<b>示例:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsPrintable("ABC")
result2 := validator.IsPrintable("{id: 123}")
result3 := validator.IsPrintable("")
result4 := validator.IsPrintable("😄")
result5 := validator.IsPrintable("\u0000")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
// Output:
// true
// true
// true
// true
// false
}
```

View File

@@ -185,7 +185,7 @@ func Average[T constraints.Integer | constraints.Float](numbers ...T) T {
} }
// Range creates a slice of numbers from start with specified count, element step is 1. // Range creates a slice of numbers from start with specified count, element step is 1.
// Play: todo // Play: https://go.dev/play/p/9ke2opxa8ZP
func Range[T constraints.Integer | constraints.Float](start T, count int) []T { func Range[T constraints.Integer | constraints.Float](start T, count int) []T {
size := count size := count
if count < 0 { if count < 0 {
@@ -202,7 +202,7 @@ func Range[T constraints.Integer | constraints.Float](start T, count int) []T {
} }
// RangeWithStep creates a slice of numbers from start to end with specified step. // RangeWithStep creates a slice of numbers from start to end with specified step.
// Play: todo // Play: https://go.dev/play/p/akLWz0EqOSM
func RangeWithStep[T constraints.Integer | constraints.Float](start, end, step T) []T { func RangeWithStep[T constraints.Integer | constraints.Float](start, end, step T) []T {
result := []T{} result := []T{}
@@ -216,3 +216,43 @@ func RangeWithStep[T constraints.Integer | constraints.Float](start, end, step T
return result return result
} }
// AngleToRadian converts angle value to radian value.
// Play: todo
func AngleToRadian(angle float64) float64 {
radian := angle * (math.Pi / 180)
return radian
}
// RadianToAngle converts radian value to angle value.
// Play: todo
func RadianToAngle(radian float64) float64 {
angle := radian * (180 / math.Pi)
return angle
}
// PointDistance get two points distance.
// Play: todo
func PointDistance(x1, y1, x2, y2 float64) float64 {
a := x1 - x2
b := y1 - y2
c := math.Pow(a, 2) + math.Pow(b, 2)
return math.Sqrt(c)
}
// IsPrimes checks if number is prime number.
// Play: todo
func IsPrime(n int) bool {
if n < 2 {
return false
}
for i := 2; i <= int(math.Sqrt(float64(n))); i++ {
if n%i == 0 {
return false
}
}
return true
}

View File

@@ -1,6 +1,9 @@
package mathutil package mathutil
import "fmt" import (
"fmt"
"math"
)
func ExampleExponent() { func ExampleExponent() {
result1 := Exponent(10, 0) result1 := Exponent(10, 0)
@@ -221,3 +224,60 @@ func ExampleRangeWithStep() {
// [-4 -2 0] // [-4 -2 0]
// [1 2.1 3.2] // [1 2.1 3.2]
} }
func ExampleAngleToRadian() {
result1 := AngleToRadian(45)
result2 := AngleToRadian(90)
result3 := AngleToRadian(180)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 0.7853981633974483
// 1.5707963267948966
// 3.141592653589793
}
func ExampleRadianToAngle() {
result1 := RadianToAngle(math.Pi)
result2 := RadianToAngle(math.Pi / 2)
result3 := RadianToAngle(math.Pi / 4)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 180
// 90
// 45
}
func ExamplePointDistance() {
result1 := PointDistance(1, 1, 4, 5)
fmt.Println(result1)
// Output:
// 5
}
func ExampleIsPrime() {
result1 := IsPrime(-1)
result2 := IsPrime(0)
result3 := IsPrime(1)
result4 := IsPrime(2)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
// Output:
// false
// false
// false
// true
}

View File

@@ -1,6 +1,7 @@
package mathutil package mathutil
import ( import (
"math"
"testing" "testing"
"github.com/duke-git/lancet/v2/internal" "github.com/duke-git/lancet/v2/internal"
@@ -139,7 +140,7 @@ func TestMinBy(t *testing.T) {
} }
func TestRange(t *testing.T) { func TestRange(t *testing.T) {
assert := internal.NewAssert(t, "Range") assert := internal.NewAssert(t, "TestRange")
result1 := Range(1, 4) result1 := Range(1, 4)
result2 := Range(1, -4) result2 := Range(1, -4)
@@ -155,7 +156,7 @@ func TestRange(t *testing.T) {
} }
func TestRangeWithStep(t *testing.T) { func TestRangeWithStep(t *testing.T) {
assert := internal.NewAssert(t, "Range") assert := internal.NewAssert(t, "TestRangeWithStep")
result1 := RangeWithStep(1, 4, 1) result1 := RangeWithStep(1, 4, 1)
result2 := RangeWithStep(1, -1, 0) result2 := RangeWithStep(1, -1, 0)
@@ -167,3 +168,46 @@ func TestRangeWithStep(t *testing.T) {
assert.Equal([]int{-4, -2, 0}, result3) assert.Equal([]int{-4, -2, 0}, result3)
assert.Equal([]float64{1.0, 2.1, 3.2}, result4) assert.Equal([]float64{1.0, 2.1, 3.2}, result4)
} }
func TestAngleToRadian(t *testing.T) {
assert := internal.NewAssert(t, "TestAngleToRadian")
result1 := AngleToRadian(45)
result2 := AngleToRadian(90)
result3 := AngleToRadian(180)
assert.Equal(0.7853981633974483, result1)
assert.Equal(1.5707963267948966, result2)
assert.Equal(3.141592653589793, result3)
}
func TestRadianToAngle(t *testing.T) {
assert := internal.NewAssert(t, "TestAngleToRadian")
result1 := RadianToAngle(math.Pi)
result2 := RadianToAngle(math.Pi / 2)
result3 := RadianToAngle(math.Pi / 4)
assert.Equal(float64(180), result1)
assert.Equal(float64(90), result2)
assert.Equal(float64(45), result3)
}
func TestPointDistance(t *testing.T) {
assert := internal.NewAssert(t, "TestPointDistance")
result1 := PointDistance(1, 1, 4, 5)
assert.Equal(float64(5), result1)
}
func TestIsPrime(t *testing.T) {
assert := internal.NewAssert(t, "TestIsPrime")
assert.Equal(false, IsPrime(-1))
assert.Equal(false, IsPrime(0))
assert.Equal(false, IsPrime(1))
assert.Equal(true, IsPrime(2))
assert.Equal(true, IsPrime(3))
assert.Equal(false, IsPrime(4))
}

View File

@@ -1,8 +1,8 @@
// Copyright 2023 dudaodong@gmail.com. All rights reserved. // Copyright 2023 dudaodong@gmail.com. All rights reserved.
// Use of this source code is governed by MIT license // Use of this source code is governed by MIT license
// Package async contain some featurese to support async programming. eg, promise, asycn/await, eventbus. // Package promise contains some functions to support async programming.
package async package promise
import ( import (
"errors" "errors"

View File

@@ -1,4 +1,4 @@
package async package promise
import ( import (
"errors" "errors"

View File

@@ -1,4 +1,4 @@
package async package promise
import ( import (
"errors" "errors"

View File

@@ -423,6 +423,7 @@ func ForEach[T any](slice []T, iteratee func(index int, item T)) {
// ForEachWithBreak iterates over elements of slice and invokes function for each element, // ForEachWithBreak iterates over elements of slice and invokes function for each element,
// when iteratee return false, will break the for each loop. // when iteratee return false, will break the for each loop.
// Play: https://go.dev/play/p/qScs39f3D9W
func ForEachWithBreak[T any](slice []T, iteratee func(index int, item T) bool) { func ForEachWithBreak[T any](slice []T, iteratee func(index int, item T) bool) {
for i, v := range slice { for i, v := range slice {
if !iteratee(i, v) { if !iteratee(i, v) {
@@ -1093,7 +1094,7 @@ func IndexOf[T comparable](arr []T, val 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. // 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.
// Play: https://go.dev/play/p/DokM4cf1IKH // Play: https://go.dev/play/p/DokM4cf1IKH
func LastIndexOf[T comparable](slice []T, item T) int { func LastIndexOf[T comparable](slice []T, item T) int {
for i := len(slice) - 1; i > 0; i-- { for i := len(slice) - 1; i >= 0; i-- {
if item == slice[i] { if item == slice[i] {
return i return i
} }

View File

@@ -665,8 +665,8 @@ func TestDifferenceWith(t *testing.T) {
func TestDifferenceBy(t *testing.T) { func TestDifferenceBy(t *testing.T) {
assert := internal.NewAssert(t, "TestDifferenceBy") assert := internal.NewAssert(t, "TestDifferenceBy")
s1 := []int{1, 2, 3, 4, 5} //after add one: 2 3 4 5 6 s1 := []int{1, 2, 3, 4, 5} // after add one: 2 3 4 5 6
s2 := []int{3, 4, 5} //after add one: 4 5 6 s2 := []int{3, 4, 5} // after add one: 4 5 6
addOne := func(i int, v int) int { addOne := func(i int, v int) int {
return v + 1 return v + 1
} }
@@ -873,8 +873,9 @@ func TestIndexOf(t *testing.T) {
func TestLastIndexOf(t *testing.T) { func TestLastIndexOf(t *testing.T) {
assert := internal.NewAssert(t, "TestLastIndexOf") assert := internal.NewAssert(t, "TestLastIndexOf")
arr := []string{"a", "a", "b", "c"} arr := []string{"a", "b", "b", "c"}
assert.Equal(1, LastIndexOf(arr, "a")) assert.Equal(0, LastIndexOf(arr, "a"))
assert.Equal(2, LastIndexOf(arr, "b"))
assert.Equal(-1, LastIndexOf(arr, "d")) assert.Equal(-1, LastIndexOf(arr, "d"))
} }

View File

@@ -360,3 +360,16 @@ func WordCount(s string) int {
return count return count
} }
// RemoveNonPrintable remove non-printable characters from a string.
// Play: todo
func RemoveNonPrintable(str string) string {
result := strings.Map(func(r rune) rune {
if unicode.IsPrint(r) {
return r
}
return -1
}, str)
return result
}

View File

@@ -438,3 +438,14 @@ func ExampleWordCount() {
// 0 // 0
// 0 // 0
} }
func ExampleRemoveNonPrintable() {
result1 := RemoveNonPrintable("hello\u00a0 \u200bworld\n")
result2 := RemoveNonPrintable("你好😄")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// hello world
// 你好😄
}

View File

@@ -342,3 +342,10 @@ func TestWordCount(t *testing.T) {
assert.Equal(v, WordCount(k)) assert.Equal(v, WordCount(k))
} }
} }
func TestRemoveNonPrintable(t *testing.T) {
assert := internal.NewAssert(t, "TestRemoveNonPrintable")
assert.Equal("hello world", RemoveNonPrintable("hello\u00a0 \u200bworld\n"))
assert.Equal("你好😄", RemoveNonPrintable("你好😄"))
}

View File

@@ -58,6 +58,31 @@ func IsAllLower(str string) bool {
return str != "" return str != ""
} }
// IsASCII checks if string is all ASCII char.
// Play: todo
func IsASCII(str string) bool {
for i := 0; i < len(str); i++ {
if str[i] > unicode.MaxASCII {
return false
}
}
return true
}
// IsPrintable checks if string is all printable chars.
// Play: todo
func IsPrintable(str string) bool {
for _, r := range str {
if !unicode.IsPrint(r) {
if r == '\n' || r == '\r' || r == '\t' || r == '`' {
continue
}
return false
}
}
return true
}
// ContainUpper check if the string contain at least one upper case letter A-Z. // ContainUpper check if the string contain at least one upper case letter A-Z.
// Play: https://go.dev/play/p/CmWeBEk27-z // Play: https://go.dev/play/p/CmWeBEk27-z
func ContainUpper(str string) bool { func ContainUpper(str string) bool {

View File

@@ -407,3 +407,45 @@ func ExampleIsGBK() {
// Output: // Output:
// true // true
} }
func ExampleIsASCII() {
result1 := IsASCII("ABC")
result2 := IsASCII("123")
result3 := IsASCII("")
result4 := IsASCII("😄")
result5 := IsASCII("你好")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
// Output:
// true
// true
// true
// false
// false
}
func ExampleIsPrintable() {
result1 := IsPrintable("ABC")
result2 := IsPrintable("{id: 123}")
result3 := IsPrintable("")
result4 := IsPrintable("😄")
result5 := IsPrintable("\u0000")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
// Output:
// true
// true
// true
// true
// false
}

View File

@@ -401,3 +401,23 @@ func TestIsGBK(t *testing.T) {
assert.Equal(true, IsGBK(gbkData)) assert.Equal(true, IsGBK(gbkData))
assert.Equal(false, utf8.Valid(gbkData)) assert.Equal(false, utf8.Valid(gbkData))
} }
func TestIsASCII(t *testing.T) {
assert := internal.NewAssert(t, "TestIsASCII")
assert.Equal(true, IsASCII("ABC"))
assert.Equal(true, IsASCII("123"))
assert.Equal(true, IsASCII(""))
assert.Equal(false, IsASCII("😄"))
assert.Equal(false, IsASCII("你好"))
}
func TestIsPrintable(t *testing.T) {
assert := internal.NewAssert(t, "TestIsPrintable")
assert.Equal(true, IsPrintable("ABC"))
assert.Equal(true, IsPrintable("{id: 123}"))
assert.Equal(true, IsPrintable(""))
assert.Equal(true, IsPrintable("😄"))
assert.Equal(false, IsPrintable("\u0000"))
}