mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-08 14:42:27 +08:00
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bab9b4a6b3 | ||
|
|
b3c34578bf | ||
|
|
f26477904e | ||
|
|
85886cf618 | ||
|
|
a19a861552 | ||
|
|
96d57a6d24 | ||
|
|
b48155c249 | ||
|
|
41685022c0 | ||
|
|
4e7274ce05 | ||
|
|
2384b0e51f | ||
|
|
6b6b938d1e | ||
|
|
29d8987909 |
10
README.md
10
README.md
@@ -4,7 +4,7 @@
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||

|

|
||||||
[](https://github.com/duke-git/lancet/releases)
|
[](https://github.com/duke-git/lancet/releases)
|
||||||
[](https://pkg.go.dev/github.com/duke-git/lancet)
|
[](https://pkg.go.dev/github.com/duke-git/lancet)
|
||||||
[](https://goreportcard.com/report/github.com/duke-git/lancet)
|
[](https://goreportcard.com/report/github.com/duke-git/lancet)
|
||||||
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||||
@@ -24,7 +24,7 @@ English | [简体中文](./README_zh-CN.md)
|
|||||||
## Feature
|
## Feature
|
||||||
|
|
||||||
- 👏 Comprehensive, efficient and reusable.
|
- 👏 Comprehensive, efficient and reusable.
|
||||||
- 💪 170+ common go util functions, support string, slice, datetime, net, crypt...
|
- 💪 180+ go util functions, support string, slice, datetime, net, crypt...
|
||||||
- 💅 Only depend on the go standard library.
|
- 💅 Only depend on the go standard library.
|
||||||
- 🌍 Unit test for every exported function.
|
- 🌍 Unit test for every exported function.
|
||||||
|
|
||||||
@@ -137,9 +137,9 @@ import "github.com/duke-git/lancet/datetime"
|
|||||||
- [EndOfMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfMinute)
|
- [EndOfMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfMinute)
|
||||||
- [EndOfHour](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfHour)
|
- [EndOfHour](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfHour)
|
||||||
- [EndOfDay](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfDay)
|
- [EndOfDay](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfDay)
|
||||||
- [EndOfWeek](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfWeek)
|
- [EndOfWeek](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfWeek)
|
||||||
- [EndOfMonth](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfMonth)
|
- [EndOfMonth](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfMonth)
|
||||||
- [EndOfYear](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfYear)
|
- [EndOfYear](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfYear)
|
||||||
- [GetNowDate](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNowDate)
|
- [GetNowDate](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNowDate)
|
||||||
- [GetNowTime](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNowTime)
|
- [GetNowTime](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNowTime)
|
||||||
- [GetNowDateTime](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNowDateTime)
|
- [GetNowDateTime](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNowDateTime)
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||

|

|
||||||
[](https://github.com/duke-git/lancet/releases)
|
[](https://github.com/duke-git/lancet/releases)
|
||||||
[](https://pkg.go.dev/github.com/duke-git/lancet)
|
[](https://pkg.go.dev/github.com/duke-git/lancet)
|
||||||
[](https://goreportcard.com/report/github.com/duke-git/lancet)
|
[](https://goreportcard.com/report/github.com/duke-git/lancet)
|
||||||
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||||
@@ -24,7 +24,7 @@
|
|||||||
## 特性
|
## 特性
|
||||||
|
|
||||||
- 👏 全面、高效、可复用
|
- 👏 全面、高效、可复用
|
||||||
- 💪 170+常用go工具函数,支持string、slice、datetime、net、crypt...
|
- 💪 180+常用go工具函数,支持string、slice、datetime、net、crypt...
|
||||||
- 💅 只依赖go标准库
|
- 💅 只依赖go标准库
|
||||||
- 🌍 所有导出函数单元测试覆盖率100%
|
- 🌍 所有导出函数单元测试覆盖率100%
|
||||||
|
|
||||||
@@ -137,9 +137,9 @@ import "github.com/duke-git/lancet/datetime"
|
|||||||
- [EndOfMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfMinute)
|
- [EndOfMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfMinute)
|
||||||
- [EndOfHour](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfHour)
|
- [EndOfHour](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfHour)
|
||||||
- [EndOfDay](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfDay)
|
- [EndOfDay](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfDay)
|
||||||
- [EndOfWeek](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfWeek)
|
- [EndOfWeek](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfWeek)
|
||||||
- [EndOfMonth](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfMonth)
|
- [EndOfMonth](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfMonth)
|
||||||
- [EndOfYear](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfYear)
|
- [EndOfYear](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfYear)
|
||||||
- [GetNowDate](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNowDate)
|
- [GetNowDate](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNowDate)
|
||||||
- [GetNowTime](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNowTime)
|
- [GetNowTime](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNowTime)
|
||||||
- [GetNowDateTime](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNowDateTime)
|
- [GetNowDateTime](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNowDateTime)
|
||||||
|
|||||||
@@ -32,9 +32,9 @@ import (
|
|||||||
- [EndOfMinute](#EndOfMinute)
|
- [EndOfMinute](#EndOfMinute)
|
||||||
- [EndOfHour](#EndOfHour)
|
- [EndOfHour](#EndOfHour)
|
||||||
- [EndOfDay](#EndOfDay)
|
- [EndOfDay](#EndOfDay)
|
||||||
- [EndOfWeek](#BeginOfWeek)
|
- [EndOfWeek](#EndOfWeek)
|
||||||
- [EndOfMonth](#BeginOfMonth)
|
- [EndOfMonth](#EndOfMonth)
|
||||||
- [EndOfYear](#BeginOfYear)
|
- [EndOfYear](#EndOfYear)
|
||||||
- [GetNowDate](#GetNowDate)
|
- [GetNowDate](#GetNowDate)
|
||||||
- [GetNowTime](#GetNowTime)
|
- [GetNowTime](#GetNowTime)
|
||||||
- [GetNowDateTime](#GetNowDateTime)
|
- [GetNowDateTime](#GetNowDateTime)
|
||||||
@@ -263,7 +263,7 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="BeginOfMonth">BeginOfWeek</span>
|
### <span id="BeginOfMonth">BeginOfMonth</span>
|
||||||
<p>Return beginning time of month</p>
|
<p>Return beginning time of month</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|||||||
@@ -22,12 +22,24 @@ import (
|
|||||||
- [AddDay](#AddDay)
|
- [AddDay](#AddDay)
|
||||||
- [AddHour](#AddHour)
|
- [AddHour](#AddHour)
|
||||||
- [AddMinute](#AddMinute)
|
- [AddMinute](#AddMinute)
|
||||||
|
- [BeginOfMinute](#BeginOfMinute)
|
||||||
|
- [BeginOfHour](#BeginOfHour)
|
||||||
|
- [BeginOfDay](#BeginOfDay)
|
||||||
|
- [BeginOfWeek](#BeginOfWeek)
|
||||||
|
- [BeginOfMonth](#BeginOfMonth)
|
||||||
|
- [BeginOfYear](#BeginOfYear)
|
||||||
|
|
||||||
|
- [EndOfMinute](#EndOfMinute)
|
||||||
|
- [EndOfHour](#EndOfHour)
|
||||||
|
- [EndOfDay](#EndOfDay)
|
||||||
|
- [EndOfWeek](#EndOfWeek)
|
||||||
|
- [EndOfMonth](#EndOfMonth)
|
||||||
|
- [EndOfYear](#EndOfYear)
|
||||||
- [GetNowDate](#GetNowDate)
|
- [GetNowDate](#GetNowDate)
|
||||||
- [GetNowTime](#GetNowTime)
|
- [GetNowTime](#GetNowTime)
|
||||||
- [GetNowDateTime](#GetNowDateTime)
|
- [GetNowDateTime](#GetNowDateTime)
|
||||||
- [GetZeroHourTimestamp](#GetZeroHourTimestamp)
|
- [GetZeroHourTimestamp](#GetZeroHourTimestamp)
|
||||||
- [GetNightTimestamp](#GetNightTimestamp)
|
- [GetNightTimestamp](#GetNightTimestamp)
|
||||||
|
|
||||||
- [FormatTimeToStr](#FormatTimeToStr)
|
- [FormatTimeToStr](#FormatTimeToStr)
|
||||||
- [FormatStrToTime](#FormatStrToTime)
|
- [FormatStrToTime](#FormatStrToTime)
|
||||||
|
|
||||||
@@ -250,7 +262,7 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="BeginOfMonth">BeginOfWeek</span>
|
### <span id="BeginOfMonth">BeginOfMonth</span>
|
||||||
<p>返回指定时间的当月开始时间</p>
|
<p>返回指定时间的当月开始时间</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ import (
|
|||||||
- [Compose](#Compose)
|
- [Compose](#Compose)
|
||||||
- [Debounced](#Debounced)
|
- [Debounced](#Debounced)
|
||||||
- [Delay](#Delay)
|
- [Delay](#Delay)
|
||||||
- [Delay](#Delay)
|
|
||||||
- [Watcher](#Watcher)
|
- [Watcher](#Watcher)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ import (
|
|||||||
- [Compose](#Compose)
|
- [Compose](#Compose)
|
||||||
- [Debounced](#Debounced)
|
- [Debounced](#Debounced)
|
||||||
- [Delay](#Delay)
|
- [Delay](#Delay)
|
||||||
- [Delay](#Delay)
|
|
||||||
- [Watcher](#Watcher)
|
- [Watcher](#Watcher)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import (
|
|||||||
- [RandBytes](#RandBytes)
|
- [RandBytes](#RandBytes)
|
||||||
- [RandInt](#RandInt)
|
- [RandInt](#RandInt)
|
||||||
- [RandString](#RandString)
|
- [RandString](#RandString)
|
||||||
|
- [UUIdV4](#UUIdV4)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -105,3 +106,33 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="UUIdV4">UUIdV4</span>
|
||||||
|
<p>Generate a random UUID of version 4 according to RFC 4122.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func UUIdV4() (string, error)
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/random"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
uuid, err := random.UUIdV4()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Println(uuid)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,8 @@ import (
|
|||||||
- [RandBytes](#RandBytes)
|
- [RandBytes](#RandBytes)
|
||||||
- [RandInt](#RandInt)
|
- [RandInt](#RandInt)
|
||||||
- [RandString](#RandString)
|
- [RandString](#RandString)
|
||||||
|
- [UUIdV4](#UUIdV4)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -105,3 +107,32 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="UUIdV4">UUIdV4</span>
|
||||||
|
<p>生成UUID v4字符串</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func UUIdV4() (string, error)
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/random"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
uuid, err := random.UUIdV4()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Println(uuid)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
func After(n int, fn interface{}) func(args ...interface{}) []reflect.Value {
|
func After(n int, fn interface{}) func(args ...interface{}) []reflect.Value {
|
||||||
// Catch programming error while constructing the closure
|
// Catch programming error while constructing the closure
|
||||||
mustBeFunction(fn)
|
mustBeFunction(fn)
|
||||||
|
|
||||||
return func(args ...interface{}) []reflect.Value {
|
return func(args ...interface{}) []reflect.Value {
|
||||||
n--
|
n--
|
||||||
if n < 1 {
|
if n < 1 {
|
||||||
@@ -66,12 +67,18 @@ func Compose(fnList ...func(...interface{}) interface{}) func(...interface{}) in
|
|||||||
|
|
||||||
// Delay make the function execution after delayed time
|
// Delay make the function execution after delayed time
|
||||||
func Delay(delay time.Duration, fn interface{}, args ...interface{}) {
|
func Delay(delay time.Duration, fn interface{}, args ...interface{}) {
|
||||||
|
// Catch programming error while constructing the closure
|
||||||
|
mustBeFunction(fn)
|
||||||
|
|
||||||
time.Sleep(delay)
|
time.Sleep(delay)
|
||||||
invokeFunc(fn, args...)
|
invokeFunc(fn, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Debounced creates a debounced function that delays invoking fn until after wait duration have elapsed since the last time the debounced function was invoked.
|
// Debounced creates a debounced function that delays invoking fn until after wait duration have elapsed since the last time the debounced function was invoked.
|
||||||
func Debounced(fn func(), duration time.Duration) func() {
|
func Debounced(fn func(), duration time.Duration) func() {
|
||||||
|
// Catch programming error while constructing the closure
|
||||||
|
mustBeFunction(fn)
|
||||||
|
|
||||||
timer := time.NewTimer(duration)
|
timer := time.NewTimer(duration)
|
||||||
timer.Stop()
|
timer.Stop()
|
||||||
|
|
||||||
@@ -91,6 +98,7 @@ func Debounced(fn func(), duration time.Duration) func() {
|
|||||||
func Schedule(d time.Duration, fn interface{}, args ...interface{}) chan bool {
|
func Schedule(d time.Duration, fn interface{}, args ...interface{}) chan bool {
|
||||||
// Catch programming error while constructing the closure
|
// Catch programming error while constructing the closure
|
||||||
mustBeFunction(fn)
|
mustBeFunction(fn)
|
||||||
|
|
||||||
quit := make(chan bool)
|
quit := make(chan bool)
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
|
|||||||
@@ -157,16 +157,13 @@ func setQueryParam(req *http.Request, reqUrl string, queryParam interface{}) err
|
|||||||
|
|
||||||
func setBodyByte(req *http.Request, body interface{}) error {
|
func setBodyByte(req *http.Request, body interface{}) error {
|
||||||
if body != nil {
|
if body != nil {
|
||||||
var bodyByte []byte
|
switch b := body.(type) {
|
||||||
if body != nil {
|
case []byte:
|
||||||
switch v := body.(type) {
|
req.Body = ioutil.NopCloser(bytes.NewReader(b))
|
||||||
case []byte:
|
req.ContentLength = int64(len(b))
|
||||||
bodyByte = v
|
default:
|
||||||
default:
|
return errors.New("body type should be []byte")
|
||||||
return errors.New("body type should be []byte")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
req.Body = ioutil.NopCloser(bytes.NewReader(bodyByte))
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -6,6 +6,7 @@ package random
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
crand "crypto/rand"
|
crand "crypto/rand"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"time"
|
"time"
|
||||||
@@ -48,3 +49,18 @@ func RandBytes(length int) []byte {
|
|||||||
}
|
}
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UUIdV4 generate a random UUID of version 4 according to RFC 4122
|
||||||
|
func UUIdV4() (string, error) {
|
||||||
|
uuid := make([]byte, 16)
|
||||||
|
|
||||||
|
n, err := io.ReadFull(crand.Reader, uuid)
|
||||||
|
if n != len(uuid) || err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
uuid[8] = uuid[8]&^0xc0 | 0x80
|
||||||
|
uuid[6] = uuid[6]&^0xf0 | 0x40
|
||||||
|
|
||||||
|
return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]), nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -47,3 +47,16 @@ func TestRandBytes(t *testing.T) {
|
|||||||
|
|
||||||
assert.Equal([]byte{}, RandBytes(0))
|
assert.Equal([]byte{}, RandBytes(0))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUUIdV4(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestUUIdV4")
|
||||||
|
|
||||||
|
uuid, err := UUIdV4()
|
||||||
|
if err != nil {
|
||||||
|
t.Log(err)
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
isUUiDV4 := regexp.MustCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$`)
|
||||||
|
assert.Equal(true, isUUiDV4.MatchString(uuid))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user