1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-09 15:12:26 +08:00

Compare commits

...

5 Commits

Author SHA1 Message Date
dudaodong
279d0754ba release v1.3.4 2022-11-17 16:14:42 +08:00
dudaodong
f133b32faa fix: issue#62: fix ZipSlip bug 2022-11-17 16:14:03 +08:00
dudaodong
b98d5edbb5 release v1.3.3 2022-11-08 15:14:34 +08:00
dudaodong
9e39c31087 docs: update doc for random package 2022-11-08 15:03:42 +08:00
dudaodong
26bc40c614 feat: add new random function 2022-11-08 14:54:17 +08:00
7 changed files with 862 additions and 522 deletions

View File

@@ -4,7 +4,7 @@
<br/> <br/>
![Go version](https://img.shields.io/badge/go-v1.16-9cf) ![Go version](https://img.shields.io/badge/go-v1.16-9cf)
[![Release](https://img.shields.io/badge/release-1.3.2-green.svg)](https://github.com/duke-git/lancet/releases) [![Release](https://img.shields.io/badge/release-1.3.4-green.svg)](https://github.com/duke-git/lancet/releases)
[![GoDoc](https://godoc.org/github.com//duke-git/lancet?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet) [![GoDoc](https://godoc.org/github.com//duke-git/lancet?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet)
[![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet)](https://goreportcard.com/report/github.com/duke-git/lancet) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet)](https://goreportcard.com/report/github.com/duke-git/lancet)
[![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)
@@ -62,12 +62,15 @@ func main() {
``` ```
## API Documentation ## API Documentation
### 1. Convertor package contains some functions for data convertion. ### 1. Convertor package contains some functions for data convertion.
```go ```go
import "github.com/duke-git/lancet/convertor" import "github.com/duke-git/lancet/convertor"
``` ```
#### Function list: #### Function list:
- [ColorHexToRGB](https://github.com/duke-git/lancet/blob/v1/docs/convertor.md#ColorHexToRGB) - [ColorHexToRGB](https://github.com/duke-git/lancet/blob/v1/docs/convertor.md#ColorHexToRGB)
- [ColorRGBToHex](https://github.com/duke-git/lancet/blob/v1/docs/convertor.md#ColorRGBToHex) - [ColorRGBToHex](https://github.com/duke-git/lancet/blob/v1/docs/convertor.md#ColorRGBToHex)
- [ToBool](https://github.com/duke-git/lancet/blob/v1/docs/convertor.md#ToBool) - [ToBool](https://github.com/duke-git/lancet/blob/v1/docs/convertor.md#ToBool)
@@ -88,6 +91,7 @@ import "github.com/duke-git/lancet/cryptor"
``` ```
#### Function list: #### Function list:
- [AesEcbEncrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor.md#AesEcbEncrypt) - [AesEcbEncrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor.md#AesEcbEncrypt)
- [AesEcbDecrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor.md#AesEcbDecrypt) - [AesEcbDecrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor.md#AesEcbDecrypt)
- [AesCbcEncrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor.md#AesCbcEncrypt) - [AesCbcEncrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor.md#AesCbcEncrypt)
@@ -123,11 +127,12 @@ import "github.com/duke-git/lancet/cryptor"
### 3. Datetime package supports date and time format and compare. ### 3. Datetime package supports date and time format and compare.
```go ```go
import "github.com/duke-git/lancet/datetime" import "github.com/duke-git/lancet/datetime"
``` ```
#### Function list: #### Function list:
- [AddDay](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#AddDay) - [AddDay](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#AddDay)
- [AddHour](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#AddHour) - [AddHour](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#AddHour)
- [AddMinute](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#AddMinute) - [AddMinute](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#AddMinute)
@@ -188,7 +193,9 @@ import "github.com/duke-git/lancet/fileutil"
```go ```go
import "github.com/duke-git/lancet/formatter" import "github.com/duke-git/lancet/formatter"
``` ```
#### Function list: #### Function list:
- [Comma](https://github.com/duke-git/lancet/blob/v1/docs/formatter.md#Comma) - [Comma](https://github.com/duke-git/lancet/blob/v1/docs/formatter.md#Comma)
### Function package can control the flow of function execution and support part of functional programming ### Function package can control the flow of function execution and support part of functional programming
@@ -198,6 +205,7 @@ import "github.com/duke-git/lancet/function"
``` ```
#### Function list: #### Function list:
- [After](https://github.com/duke-git/lancet/blob/v1/docs/function.md#After) - [After](https://github.com/duke-git/lancet/blob/v1/docs/function.md#After)
- [Before](https://github.com/duke-git/lancet/blob/v1/docs/function.md#Before) - [Before](https://github.com/duke-git/lancet/blob/v1/docs/function.md#Before)
- [Curry](https://github.com/duke-git/lancet/blob/v1/docs/function.md#Curry) - [Curry](https://github.com/duke-git/lancet/blob/v1/docs/function.md#Curry)
@@ -206,7 +214,6 @@ import "github.com/duke-git/lancet/function"
- [Delay](https://github.com/duke-git/lancet/blob/v1/docs/function.md#Delay) - [Delay](https://github.com/duke-git/lancet/blob/v1/docs/function.md#Delay)
- [Watcher](https://github.com/duke-git/lancet/blob/v1/docs/function.md#Watcher) - [Watcher](https://github.com/duke-git/lancet/blob/v1/docs/function.md#Watcher)
### 6. Mathutil package implements some functions for math calculation. ### 6. Mathutil package implements some functions for math calculation.
```go ```go
@@ -214,6 +221,7 @@ import "github.com/duke-git/lancet/mathutil"
``` ```
#### Function list: #### Function list:
- [Exponent](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#Exponent) - [Exponent](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#Exponent)
- [Fibonacci](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#Fibonacci) - [Fibonacci](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#Fibonacci)
- [Factorial](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#Factorial) - [Factorial](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#Factorial)
@@ -222,7 +230,6 @@ import "github.com/duke-git/lancet/mathutil"
- [RoundToString](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#RoundToString) - [RoundToString](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#RoundToString)
- [TruncRound](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#TruncRound) - [TruncRound](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#TruncRound)
### 7. Netutil package contains functions to get net information and send http request. ### 7. Netutil package contains functions to get net information and send http request.
```go ```go
@@ -230,6 +237,7 @@ import "github.com/duke-git/lancet/netutil"
``` ```
#### Function list: #### Function list:
- [ConvertMapToQueryString](https://github.com/duke-git/lancet/blob/v1/docs/netutil.md#ConvertMapToQueryString) - [ConvertMapToQueryString](https://github.com/duke-git/lancet/blob/v1/docs/netutil.md#ConvertMapToQueryString)
- [EncodeUrl](https://github.com/duke-git/lancet/blob/v1/docs/netutil.md#EncodeUrl) - [EncodeUrl](https://github.com/duke-git/lancet/blob/v1/docs/netutil.md#EncodeUrl)
- [GetInternalIp](https://github.com/duke-git/lancet/blob/v1/docs/netutil.md#GetInternalIp) - [GetInternalIp](https://github.com/duke-git/lancet/blob/v1/docs/netutil.md#GetInternalIp)
@@ -246,7 +254,6 @@ import "github.com/duke-git/lancet/netutil"
- [HttpPatch](https://github.com/duke-git/lancet/blob/v1/docs/netutil.md#HttpPatch) - [HttpPatch](https://github.com/duke-git/lancet/blob/v1/docs/netutil.md#HttpPatch)
- [ParseHttpResponse](https://github.com/duke-git/lancet/blob/v1/docs/netutil.md#ParseHttpResponse) - [ParseHttpResponse](https://github.com/duke-git/lancet/blob/v1/docs/netutil.md#ParseHttpResponse)
### 8. Random package implements some basic functions to generate random int and string. ### 8. Random package implements some basic functions to generate random int and string.
```go ```go
@@ -254,9 +261,14 @@ import "github.com/duke-git/lancet/random"
``` ```
#### Function list: #### Function list:
- [RandBytes](https://github.com/duke-git/lancet/blob/v1/docs/random.md#RandBytes) - [RandBytes](https://github.com/duke-git/lancet/blob/v1/docs/random.md#RandBytes)
- [RandInt](https://github.com/duke-git/lancet/blob/v1/docs/random.md#RandInt) - [RandInt](https://github.com/duke-git/lancet/blob/v1/docs/random.md#RandInt)
- [RandString](https://github.com/duke-git/lancet/blob/v1/docs/random.md#RandString) - [RandString](https://github.com/duke-git/lancet/blob/v1/docs/random.md#RandString)
- [RandUpper](https://github.com/duke-git/lancet/blob/v1/docs/random.md#RandUpper)
- [RandLower](https://github.com/duke-git/lancet/blob/v1/docs/random.md#RandLower)
- [RandNumeral](https://github.com/duke-git/lancet/blob/v1/docs/random.md#RandNumeral)
- [RandNumeralOrLetter](https://github.com/duke-git/lancet/blob/v1/docs/random.md#RandNumeralOrLetter)
- [UUIdV4](https://github.com/duke-git/lancet/blob/v1/docs/random.md#UUIdV4) - [UUIdV4](https://github.com/duke-git/lancet/blob/v1/docs/random.md#UUIdV4)
### 9. Retry package is for executing a function repeatedly until it was successful or canceled by the context. ### 9. Retry package is for executing a function repeatedly until it was successful or canceled by the context.
@@ -266,6 +278,7 @@ import "github.com/duke-git/lancet/retry"
``` ```
#### Function list: #### Function list:
- [Context](https://github.com/duke-git/lancet/blob/v1/docs/retry.md#Context) - [Context](https://github.com/duke-git/lancet/blob/v1/docs/retry.md#Context)
- [Retry](https://github.com/duke-git/lancet/blob/v1/docs/retry.md#Retry) - [Retry](https://github.com/duke-git/lancet/blob/v1/docs/retry.md#Retry)
- [RetryFunc](https://github.com/duke-git/lancet/blob/v1/docs/retry.md#RetryFunc) - [RetryFunc](https://github.com/duke-git/lancet/blob/v1/docs/retry.md#RetryFunc)
@@ -279,6 +292,7 @@ import "github.com/duke-git/lancet/slice"
``` ```
#### Function list: #### Function list:
- [AppendIfAbsent](https://github.com/duke-git/lancet/blob/v1/docs/slice.md#AppendIfAbsent) - [AppendIfAbsent](https://github.com/duke-git/lancet/blob/v1/docs/slice.md#AppendIfAbsent)
- [Contain](https://github.com/duke-git/lancet/blob/v1/docs/slice.md#Contain) - [Contain](https://github.com/duke-git/lancet/blob/v1/docs/slice.md#Contain)
- [ContainSubSlice](https://github.com/duke-git/lancet/blob/v1/docs/slice.md#ContainSubSlice) - [ContainSubSlice](https://github.com/duke-git/lancet/blob/v1/docs/slice.md#ContainSubSlice)
@@ -353,6 +367,7 @@ import "github.com/duke-git/lancet/system"
``` ```
#### Function list: #### Function list:
- [IsWindows](https://github.com/duke-git/lancet/blob/v1/docs/system.md#IsWindows) - [IsWindows](https://github.com/duke-git/lancet/blob/v1/docs/system.md#IsWindows)
- [IsLinux](https://github.com/duke-git/lancet/blob/v1/docs/system.md#IsLinux) - [IsLinux](https://github.com/duke-git/lancet/blob/v1/docs/system.md#IsLinux)
- [IsMac](https://github.com/duke-git/lancet/blob/v1/docs/system.md#IsMac) - [IsMac](https://github.com/duke-git/lancet/blob/v1/docs/system.md#IsMac)
@@ -368,6 +383,7 @@ import "github.com/duke-git/lancet/system"
```go ```go
import "github.com/duke-git/lancet/validator" import "github.com/duke-git/lancet/validator"
``` ```
#### Function list: #### Function list:
- [ContainChinese](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#ContainChinese) - [ContainChinese](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#ContainChinese)
@@ -398,7 +414,6 @@ import "github.com/duke-git/lancet/validator"
- [IsWeakPassword](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsWeakPassword) - [IsWeakPassword](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsWeakPassword)
- [IsZeroValue](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsZeroValue) - [IsZeroValue](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsZeroValue)
## How to Contribute ## How to Contribute
I really appreciate any code commits which make lancet lib powerful. Please follow the rules below to create your pull request. I really appreciate any code commits which make lancet lib powerful. Please follow the rules below to create your pull request.

View File

@@ -4,7 +4,7 @@
<br/> <br/>
![Go version](https://img.shields.io/badge/go-v1.16-9cf) ![Go version](https://img.shields.io/badge/go-v1.16-9cf)
[![Release](https://img.shields.io/badge/release-1.3.2-green.svg)](https://github.com/duke-git/lancet/releases) [![Release](https://img.shields.io/badge/release-1.3.4-green.svg)](https://github.com/duke-git/lancet/releases)
[![GoDoc](https://godoc.org/github.com//duke-git/lancet?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet) [![GoDoc](https://godoc.org/github.com//duke-git/lancet?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet)
[![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet)](https://goreportcard.com/report/github.com/duke-git/lancet) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet)](https://goreportcard.com/report/github.com/duke-git/lancet)
[![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)
@@ -20,7 +20,6 @@
简体中文 | [English](./README.md) 简体中文 | [English](./README.md)
## 特性 ## 特性
- 👏 全面、高效、可复用 - 👏 全面、高效、可复用
@@ -62,12 +61,15 @@ func main() {
``` ```
## API 文档 ## API 文档
### 1. convertor 转换器包支持一些常见的数据类型转换。 ### 1. convertor 转换器包支持一些常见的数据类型转换。
```go ```go
import "github.com/duke-git/lancet/convertor" import "github.com/duke-git/lancet/convertor"
``` ```
#### 函数列表: #### 函数列表:
- [ColorHexToRGB](https://github.com/duke-git/lancet/blob/v1/docs/convertor_zh-CN.md#ColorHexToRGB) - [ColorHexToRGB](https://github.com/duke-git/lancet/blob/v1/docs/convertor_zh-CN.md#ColorHexToRGB)
- [ColorRGBToHex](https://github.com/duke-git/lancet/blob/v1/docs/convertor_zh-CN.md#ColorRGBToHex) - [ColorRGBToHex](https://github.com/duke-git/lancet/blob/v1/docs/convertor_zh-CN.md#ColorRGBToHex)
- [ToBool](https://github.com/duke-git/lancet/blob/v1/docs/convertor_zh-CN.md#ToBool) - [ToBool](https://github.com/duke-git/lancet/blob/v1/docs/convertor_zh-CN.md#ToBool)
@@ -88,6 +90,7 @@ import "github.com/duke-git/lancet/cryptor"
``` ```
#### 函数列表: #### 函数列表:
- [AesEcbEncrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor_zh-CN.md#AesEcbEncrypt) - [AesEcbEncrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor_zh-CN.md#AesEcbEncrypt)
- [AesEcbDecrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor_zh-CN.md#AesEcbDecrypt) - [AesEcbDecrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor_zh-CN.md#AesEcbDecrypt)
- [AesCbcEncrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor_zh-CN.md#AesCbcEncrypt) - [AesCbcEncrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor_zh-CN.md#AesCbcEncrypt)
@@ -123,11 +126,12 @@ import "github.com/duke-git/lancet/cryptor"
### 3. datetime 日期时间处理包,格式化日期,比较日期。 ### 3. datetime 日期时间处理包,格式化日期,比较日期。
```go ```go
import "github.com/duke-git/lancet/datetime" import "github.com/duke-git/lancet/datetime"
``` ```
#### 函数列表: #### 函数列表:
- [AddDay](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#AddDay) - [AddDay](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#AddDay)
- [AddHour](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#AddHour) - [AddHour](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#AddHour)
- [AddMinute](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#AddMinute) - [AddMinute](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#AddMinute)
@@ -158,6 +162,7 @@ import "github.com/duke-git/lancet/datetime"
- [ToFormat](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#ToFormat) - [ToFormat](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#ToFormat)
- [ToFormatForTpl](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#ToFormatForTpl) - [ToFormatForTpl](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#ToFormatForTpl)
- [ToIso8601](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#ToIso8601) - [ToIso8601](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#ToIso8601)
### 4. fileutil 包支持文件基本操作。 ### 4. fileutil 包支持文件基本操作。
```go ```go
@@ -187,9 +192,10 @@ import "github.com/duke-git/lancet/fileutil"
```go ```go
import "github.com/duke-git/lancet/formatter" import "github.com/duke-git/lancet/formatter"
``` ```
#### 函数列表:
- [Comma](https://github.com/duke-git/lancet/blob/v1/docs/formatter_zh-CN.md#Comma)
#### 函数列表:
- [Comma](https://github.com/duke-git/lancet/blob/v1/docs/formatter_zh-CN.md#Comma)
### function 函数包控制函数执行流程,包含部分函数式编程。 ### function 函数包控制函数执行流程,包含部分函数式编程。
@@ -198,6 +204,7 @@ import "github.com/duke-git/lancet/function"
``` ```
#### 函数列表: #### 函数列表:
- [After](https://github.com/duke-git/lancet/blob/v1/docs/function_zh-CN.md#After) - [After](https://github.com/duke-git/lancet/blob/v1/docs/function_zh-CN.md#After)
- [Before](https://github.com/duke-git/lancet/blob/v1/docs/function_zh-CN.md#Before) - [Before](https://github.com/duke-git/lancet/blob/v1/docs/function_zh-CN.md#Before)
- [Curry](https://github.com/duke-git/lancet/blob/v1/docs/function_zh-CN.md#Curry) - [Curry](https://github.com/duke-git/lancet/blob/v1/docs/function_zh-CN.md#Curry)
@@ -213,6 +220,7 @@ import "github.com/duke-git/lancet/mathutil"
``` ```
#### Function list: #### Function list:
- [Exponent](https://github.com/duke-git/lancet/blob/v1/docs/mathutil_zh-CN.md#Exponent) - [Exponent](https://github.com/duke-git/lancet/blob/v1/docs/mathutil_zh-CN.md#Exponent)
- [Fibonacci](https://github.com/duke-git/lancet/blob/v1/docs/mathutil_zh-CN.md#Fibonacci) - [Fibonacci](https://github.com/duke-git/lancet/blob/v1/docs/mathutil_zh-CN.md#Fibonacci)
- [Factorial](https://github.com/duke-git/lancet/blob/v1/docs/mathutil_zh-CN.md#Factorial) - [Factorial](https://github.com/duke-git/lancet/blob/v1/docs/mathutil_zh-CN.md#Factorial)
@@ -228,6 +236,7 @@ import "github.com/duke-git/lancet/netutil"
``` ```
#### 函数列表: #### 函数列表:
- [ConvertMapToQueryString](https://github.com/duke-git/lancet/blob/v1/docs/netutil_zh-CN.md#ConvertMapToQueryString) - [ConvertMapToQueryString](https://github.com/duke-git/lancet/blob/v1/docs/netutil_zh-CN.md#ConvertMapToQueryString)
- [EncodeUrl](https://github.com/duke-git/lancet/blob/v1/docs/netutil_zh-CN.md#EncodeUrl) - [EncodeUrl](https://github.com/duke-git/lancet/blob/v1/docs/netutil_zh-CN.md#EncodeUrl)
- [GetInternalIp](https://github.com/duke-git/lancet/blob/v1/docs/netutil_zh-CN.md#GetInternalIp) - [GetInternalIp](https://github.com/duke-git/lancet/blob/v1/docs/netutil_zh-CN.md#GetInternalIp)
@@ -251,10 +260,16 @@ import "github.com/duke-git/lancet/random"
``` ```
#### 函数列表: #### 函数列表:
- [RandBytes](https://github.com/duke-git/lancet/blob/v1/docs/random_zh-CN.md#RandBytes) - [RandBytes](https://github.com/duke-git/lancet/blob/v1/docs/random_zh-CN.md#RandBytes)
- [RandInt](https://github.com/duke-git/lancet/blob/v1/docs/random_zh-CN.md#RandInt) - [RandInt](https://github.com/duke-git/lancet/blob/v1/docs/random_zh-CN.md#RandInt)
- [RandString](https://github.com/duke-git/lancet/blob/v1/docs/random_zh-CN.md#RandString) - [RandString](https://github.com/duke-git/lancet/blob/v1/docs/random_zh-CN.md#RandString)
- [RandUpper](https://github.com/duke-git/lancet/blob/v1/docs/random_zh-CN.md#RandUpper)
- [RandLower](https://github.com/duke-git/lancet/blob/v1/docs/random_zh-CN.md#RandLower)
- [RandNumeral](https://github.com/duke-git/lancet/blob/v1/docs/random_zh-CN.md#RandNumeral)
- [RandNumeralOrLetter](https://github.com/duke-git/lancet/blob/v1/docs/random_zh-CN.md#RandNumeralOrLetter)
- [UUIdV4](https://github.com/duke-git/lancet/blob/v1/docs/random.md#UUIdV4) - [UUIdV4](https://github.com/duke-git/lancet/blob/v1/docs/random.md#UUIdV4)
### 9. retry 重试执行函数直到函数运行成功或被 context cancel。 ### 9. retry 重试执行函数直到函数运行成功或被 context cancel。
```go ```go
@@ -262,13 +277,13 @@ import "github.com/duke-git/lancet/retry"
``` ```
#### 函数列表: #### 函数列表:
- [Context](https://github.com/duke-git/lancet/blob/v1/docs/retry_zh-CN.md#Context) - [Context](https://github.com/duke-git/lancet/blob/v1/docs/retry_zh-CN.md#Context)
- [Retry](https://github.com/duke-git/lancet/blob/v1/docs/retry_zh-CN.md#Retry) - [Retry](https://github.com/duke-git/lancet/blob/v1/docs/retry_zh-CN.md#Retry)
- [RetryFunc](https://github.com/duke-git/lancet/blob/v1/docs/retry_zh-CN.md#RetryFunc) - [RetryFunc](https://github.com/duke-git/lancet/blob/v1/docs/retry_zh-CN.md#RetryFunc)
- [RetryDuration](https://github.com/duke-git/lancet/blob/v1/docs/retry_zh-CN.md#RetryDuration) - [RetryDuration](https://github.com/duke-git/lancet/blob/v1/docs/retry_zh-CN.md#RetryDuration)
- [RetryTimes](https://github.com/duke-git/lancet/blob/v1/docs/retry_zh-CN.md#RetryTimes) - [RetryTimes](https://github.com/duke-git/lancet/blob/v1/docs/retry_zh-CN.md#RetryTimes)
### 10. slice 包包含操作切片的方法集合。 ### 10. slice 包包含操作切片的方法集合。
```go ```go
@@ -276,6 +291,7 @@ import "github.com/duke-git/lancet/slice"
``` ```
#### 函数列表: #### 函数列表:
- [AppendIfAbsent](https://github.com/duke-git/lancet/blob/v1/docs/slice_zh-CN.md#AppendIfAbsent) - [AppendIfAbsent](https://github.com/duke-git/lancet/blob/v1/docs/slice_zh-CN.md#AppendIfAbsent)
- [Contain](https://github.com/duke-git/lancet/blob/v1/docs/slice_zh-CN.md#Contain) - [Contain](https://github.com/duke-git/lancet/blob/v1/docs/slice_zh-CN.md#Contain)
- [ContainSubSlice](https://github.com/duke-git/lancet/blob/v1/docs/slice_zh-CN.md#ContainSubSlice) - [ContainSubSlice](https://github.com/duke-git/lancet/blob/v1/docs/slice_zh-CN.md#ContainSubSlice)
@@ -317,7 +333,6 @@ import "github.com/duke-git/lancet/slice"
- [UpdateByIndex](https://github.com/duke-git/lancet/blob/v1/docs/slice_zh-CN.md#UpdateByIndex) - [UpdateByIndex](https://github.com/duke-git/lancet/blob/v1/docs/slice_zh-CN.md#UpdateByIndex)
- [Without](https://github.com/duke-git/lancet/blob/v1/docs/slice_zh-CN.md#Without) - [Without](https://github.com/duke-git/lancet/blob/v1/docs/slice_zh-CN.md#Without)
### 12. strutil 包含处理字符串的相关函数。 ### 12. strutil 包含处理字符串的相关函数。
```go ```go
@@ -344,7 +359,6 @@ import "github.com/duke-git/lancet/strutil"
- [Wrap](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#Wrap) - [Wrap](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#Wrap)
- [Unwrap](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#Unwrap) - [Unwrap](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#Unwrap)
### 13. system 包含 os, runtime, shell command 相关函数。 ### 13. system 包含 os, runtime, shell command 相关函数。
```go ```go
@@ -352,6 +366,7 @@ import "github.com/duke-git/lancet/system"
``` ```
#### 函数列表: #### 函数列表:
- [IsWindows](https://github.com/duke-git/lancet/blob/v1/docs/system_zh-CN.md#IsWindows) - [IsWindows](https://github.com/duke-git/lancet/blob/v1/docs/system_zh-CN.md#IsWindows)
- [IsLinux](https://github.com/duke-git/lancet/blob/v1/docs/system_zh-CN.md#IsLinux) - [IsLinux](https://github.com/duke-git/lancet/blob/v1/docs/system_zh-CN.md#IsLinux)
- [IsMac](https://github.com/duke-git/lancet/blob/v1/docs/system_zh-CN.md#IsMac) - [IsMac](https://github.com/duke-git/lancet/blob/v1/docs/system_zh-CN.md#IsMac)
@@ -367,6 +382,7 @@ import "github.com/duke-git/lancet/system"
```go ```go
import "github.com/duke-git/lancet/validator" import "github.com/duke-git/lancet/validator"
``` ```
#### 函数列表: #### 函数列表:
- [ContainChinese](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#ContainChinese) - [ContainChinese](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#ContainChinese)
@@ -397,7 +413,6 @@ import "github.com/duke-git/lancet/validator"
- [IsWeakPassword](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsWeakPassword) - [IsWeakPassword](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsWeakPassword)
- [IsZeroValue](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsZeroValue) - [IsZeroValue](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsZeroValue)
## 如何贡献代码 ## 如何贡献代码
非常感激任何的代码提交以使 lancet 的功能越来越强大。创建 pull request 时请遵守以下规则。 非常感激任何的代码提交以使 lancet 的功能越来越强大。创建 pull request 时请遵守以下规则。

View File

@@ -1,4 +1,5 @@
# Random # Random
Package random implements some basic functions to generate random int and string. Package random implements some basic functions to generate random int and string.
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -7,10 +8,10 @@ Package random implements some basic functions to generate random int and string
[https://github.com/duke-git/lancet/blob/v1/random/random.go](https://github.com/duke-git/lancet/blob/v1/random/random.go) [https://github.com/duke-git/lancet/blob/v1/random/random.go](https://github.com/duke-git/lancet/blob/v1/random/random.go)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Usage: ## Usage:
```go ```go
import ( import (
"github.com/duke-git/lancet/random" "github.com/duke-git/lancet/random"
@@ -20,17 +21,23 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Index ## Index
- [RandBytes](#RandBytes) - [RandBytes](#RandBytes)
- [RandInt](#RandInt) - [RandInt](#RandInt)
- [RandString](#RandString) - [RandString](#RandString)
- [RandUpper](#RandUpper)
- [RandLower](#RandLower)
- [RandNumeral](#RandNumeral)
- [RandNumeralOrLetter](#RandNumeralOrLetter)
- [UUIdV4](#UUIdV4) - [UUIdV4](#UUIdV4)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Documentation ## Documentation
### <span id="RandBytes">RandBytes</span> ### <span id="RandBytes">RandBytes</span>
<p>Generate random byte slice.</p> <p>Generate random byte slice.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -38,6 +45,7 @@ import (
```go ```go
func RandBytes(length int) []byte func RandBytes(length int) []byte
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -54,8 +62,8 @@ func main() {
} }
``` ```
### <span id="RandInt">RandInt</span> ### <span id="RandInt">RandInt</span>
<p>Generate random int between min and max, may contain min, not max.</p> <p>Generate random int between min and max, may contain min, not max.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -63,6 +71,7 @@ func main() {
```go ```go
func RandInt(min, max int) int func RandInt(min, max int) int
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -79,16 +88,16 @@ func main() {
} }
``` ```
### <span id="RandString">RandString</span>
<p>Generate random given length string. only contains letter (a-zA-Z)</p>
### <span id="RandString">RandInt</span>
<p>Generate random given length string.</p>
<b>Signature:</b> <b>Signature:</b>
```go ```go
func RandString(length int) string func RandString(length int) string
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -101,14 +110,116 @@ import (
func main() { func main() {
randStr := random.RandString(6) randStr := random.RandString(6)
fmt.Println(randStr) fmt.Println(randStr) //pGWsze
} }
``` ```
### <span id="RandUpper">RandUpper</span>
<p>Generate a random upper case string</p>
<b>Signature:</b>
```go
func RandUpper(length int) string
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/random"
)
func main() {
randStr := random.RandString(6)
fmt.Println(randStr) //PACWGF
}
```
### <span id="RandLower">RandLower</span>
<p>Generate a random lower case string</p>
<b>Signature:</b>
```go
func RandLower(length int) string
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/random"
)
func main() {
randStr := random.RandLower(6)
fmt.Println(randStr) //siqbew
}
```
### <span id="RandNumeral">RandNumeral</span>
<p>Generate a random numeral string</p>
<b>Signature:</b>
```go
func RandNumeral(length int) string
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/random"
)
func main() {
randStr := random.RandNumeral(6)
fmt.Println(randStr) //035172
}
```
### <span id="RandNumeralOrLetter">RandNumeralOrLetter</span>
<p>generate a random numeral or letter string</p>
<b>Signature:</b>
```go
func RandNumeralOrLetter(length int) string
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/random"
)
func main() {
randStr := random.RandNumeralOrLetter(6)
fmt.Println(randStr) //0aW7cQ
}
```
### <span id="UUIdV4">UUIdV4</span> ### <span id="UUIdV4">UUIdV4</span>
<p>Generate a random UUID of version 4 according to RFC 4122.</p> <p>Generate a random UUID of version 4 according to RFC 4122.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -116,6 +227,7 @@ func main() {
```go ```go
func UUIdV4() (string, error) func UUIdV4() (string, error)
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -134,5 +246,3 @@ func main() {
fmt.Println(uuid) fmt.Println(uuid)
} }
``` ```

View File

@@ -1,4 +1,5 @@
# Random # Random
random 随机数生成器包,可以生成随机[]bytes, int, string。 random 随机数生成器包,可以生成随机[]bytes, int, string。
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -7,10 +8,10 @@ random随机数生成器包可以生成随机[]bytes, int, string。
[https://github.com/duke-git/lancet/blob/v1/random/random.go](https://github.com/duke-git/lancet/blob/v1/random/random.go) [https://github.com/duke-git/lancet/blob/v1/random/random.go](https://github.com/duke-git/lancet/blob/v1/random/random.go)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 用法: ## 用法:
```go ```go
import ( import (
"github.com/duke-git/lancet/random" "github.com/duke-git/lancet/random"
@@ -20,18 +21,23 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 目录 ## 目录
- [RandBytes](#RandBytes) - [RandBytes](#RandBytes)
- [RandInt](#RandInt) - [RandInt](#RandInt)
- [RandString](#RandString) - [RandString](#RandString)
- [UUIdV4](#UUIdV4) - [RandUpper](#RandUpper)
- [RandLower](#RandLower)
- [RandNumeral](#RandNumeral)
- [RandNumeralOrLetter](#RandNumeralOrLetter)
- [UUIdV4](#UUIdV4)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 文档 ## 文档
### <span id="RandBytes">RandBytes</span> ### <span id="RandBytes">RandBytes</span>
<p>生成随机字节切片</p> <p>生成随机字节切片</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -39,6 +45,7 @@ import (
```go ```go
func RandBytes(length int) []byte func RandBytes(length int) []byte
``` ```
<b>例子:</b> <b>例子:</b>
```go ```go
@@ -55,8 +62,8 @@ func main() {
} }
``` ```
### <span id="RandInt">RandInt</span> ### <span id="RandInt">RandInt</span>
<p>生成随机int, 范围[min, max)</p> <p>生成随机int, 范围[min, max)</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -64,6 +71,7 @@ func main() {
```go ```go
func RandInt(min, max int) int func RandInt(min, max int) int
``` ```
<b>例子:</b> <b>例子:</b>
```go ```go
@@ -80,16 +88,16 @@ func main() {
} }
``` ```
### <span id="RandString">RandString</span>
<p>生成给定长度的随机字符串,只包含字母(a-zA-Z)</p>
### <span id="RandString">RandInt</span>
<p>生成随机给定长度的随机字符串</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func RandString(length int) string func RandString(length int) string
``` ```
<b>例子:</b> <b>例子:</b>
```go ```go
@@ -102,13 +110,116 @@ import (
func main() { func main() {
randStr := random.RandString(6) randStr := random.RandString(6)
fmt.Println(randStr) fmt.Println(randStr) //pGWsze
} }
``` ```
### <span id="RandUpper">RandUpper</span>
<p>生成给定长度的随机大写字母字符串</p>
<b>函数签名:</b>
```go
func RandUpper(length int) string
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/random"
)
func main() {
randStr := random.RandString(6)
fmt.Println(randStr) //PACWGF
}
```
### <span id="RandLower">RandLower</span>
<p>生成给定长度的随机小写字母字符串</p>
<b>函数签名:</b>
```go
func RandLower(length int) string
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/random"
)
func main() {
randStr := random.RandLower(6)
fmt.Println(randStr) //siqbew
}
```
### <span id="RandNumeral">RandNumeral</span>
<p>生成给定长度的随机数字字符串</p>
<b>函数签名:</b>
```go
func RandNumeral(length int) string
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/random"
)
func main() {
randStr := random.RandNumeral(6)
fmt.Println(randStr) //035172
}
```
### <span id="RandNumeralOrLetter">RandNumeralOrLetter</span>
<p>生成给定长度的随机字符串(数字+字母)</p>
<b>函数签名:</b>
```go
func RandNumeralOrLetter(length int) string
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/random"
)
func main() {
randStr := random.RandNumeralOrLetter(6)
fmt.Println(randStr) //0aW7cQ
}
```
### <span id="UUIdV4">UUIdV4</span> ### <span id="UUIdV4">UUIdV4</span>
<p>生成UUID v4字符串</p> <p>生成UUID v4字符串</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -116,6 +227,7 @@ func main() {
```go ```go
func UUIdV4() (string, error) func UUIdV4() (string, error)
``` ```
<b>例子:</b> <b>例子:</b>
```go ```go
@@ -134,5 +246,3 @@ func main() {
fmt.Println(uuid) fmt.Println(uuid)
} }
``` ```

View File

@@ -8,6 +8,7 @@ import (
"archive/zip" "archive/zip"
"bufio" "bufio"
"errors" "errors"
"fmt"
"io" "io"
"io/fs" "io/fs"
"io/ioutil" "io/ioutil"
@@ -220,7 +221,11 @@ func UnZip(zipFile string, destPath string) error {
defer zipReader.Close() defer zipReader.Close()
for _, f := range zipReader.File { for _, f := range zipReader.File {
path := filepath.Join(destPath, f.Name) //issue#62: fix ZipSlip bug
path, err := safeFilepathJoin(destPath, f.Name)
if err != nil {
return err
}
if f.FileInfo().IsDir() { if f.FileInfo().IsDir() {
os.MkdirAll(path, os.ModePerm) os.MkdirAll(path, os.ModePerm)
} else { } else {
@@ -249,6 +254,17 @@ func UnZip(zipFile string, destPath string) error {
return nil return nil
} }
func safeFilepathJoin(path1, path2 string) (string, error) {
relPath, err := filepath.Rel(".", path2)
if err != nil || strings.HasPrefix(relPath, "..") {
return "", fmt.Errorf("(zipslip) filepath is unsafe %q: %v", path2, err)
}
if path1 == "" {
path1 = "."
}
return filepath.Join(path1, filepath.Join("/", relPath)), nil
}
// IsLink checks if a file is symbol link or not // IsLink checks if a file is symbol link or not
func IsLink(path string) bool { func IsLink(path string) bool {
fi, err := os.Lstat(path) fi, err := os.Lstat(path)

View File

@@ -12,18 +12,12 @@ import (
"time" "time"
) )
// RandString generate random string const (
// see https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-go NUMERAL = "0123456789"
func RandString(length int) string { LOWER_LETTERS = "abcdefghijklmnopqrstuvwxyz"
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" UPPER_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
LETTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
b := make([]byte, length) )
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := range b {
b[i] = letters[r.Int63()%int64(len(letters))]
}
return string(b)
}
// RandInt generate random int between min and max, maybe min, not be max // RandInt generate random int between min and max, maybe min, not be max
func RandInt(min, max int) int { func RandInt(min, max int) int {
@@ -50,6 +44,41 @@ func RandBytes(length int) []byte {
return b return b
} }
// RandString generate random string
func RandString(length int) string {
return random(LETTERS, length)
}
// RandUpper generate a random upper case string
func RandUpper(length int) string {
return random(UPPER_LETTERS, length)
}
// RandLower generate a random lower case string
func RandLower(length int) string {
return random(LOWER_LETTERS, length)
}
// RandNumeral generate a random numeral string
func RandNumeral(length int) string {
return random(NUMERAL, length)
}
// RandNumeralOrLetter generate a random numeral or letter string
func RandNumeralOrLetter(length int) string {
return random(NUMERAL+LETTERS, length)
}
// random generate a random string based on given string range
func random(s string, length int) string {
b := make([]byte, length)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := range b {
b[i] = s[r.Int63()%int64(len(s))]
}
return string(b)
}
// UUIdV4 generate a random UUID of version 4 according to RFC 4122 // UUIdV4 generate a random UUID of version 4 according to RFC 4122
func UUIdV4() (string, error) { func UUIdV4() (string, error) {
uuid := make([]byte, 16) uuid := make([]byte, 16)

View File

@@ -19,6 +19,51 @@ func TestRandString(t *testing.T) {
assert.Equal(true, reg.MatchString(randStr)) assert.Equal(true, reg.MatchString(randStr))
} }
func TestRandUpper(t *testing.T) {
pattern := `^[A-Z]+$`
reg := regexp.MustCompile(pattern)
randStr := RandUpper(6)
assert := internal.NewAssert(t, "TestRandUpper")
assert.Equal(6, len(randStr))
assert.Equal(true, reg.MatchString(randStr))
}
func TestRandLower(t *testing.T) {
pattern := `^[a-z]+$`
reg := regexp.MustCompile(pattern)
randStr := RandLower(6)
assert := internal.NewAssert(t, "TestRandLower")
assert.Equal(6, len(randStr))
assert.Equal(true, reg.MatchString(randStr))
}
func TestRandNumeral(t *testing.T) {
pattern := `^[0-9]+$`
reg := regexp.MustCompile(pattern)
randStr := RandNumeral(12)
assert := internal.NewAssert(t, "TestRandNumeral")
assert.Equal(12, len(randStr))
assert.Equal(true, reg.MatchString(randStr))
}
func TestRandNumeralOrLetter(t *testing.T) {
pattern := `^[0-9a-zA-Z]+$`
reg := regexp.MustCompile(pattern)
randStr := RandNumeralOrLetter(10)
t.Log(randStr)
assert := internal.NewAssert(t, "TestRandNumeralOrLetter")
assert.Equal(10, len(randStr))
assert.Equal(true, reg.MatchString(randStr))
}
func TestRandInt(t *testing.T) { func TestRandInt(t *testing.T) {
assert := internal.NewAssert(t, "TestRandInt") assert := internal.NewAssert(t, "TestRandInt")