mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-09 15:12:26 +08:00
Compare commits
32 Commits
v2.3.7
...
6c3dc3e7d6
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6c3dc3e7d6 | ||
|
|
2a2e1ca551 | ||
|
|
e1821eed2c | ||
|
|
62f0a96d91 | ||
|
|
cbdc3971dd | ||
|
|
350450bb67 | ||
|
|
f407e51b24 | ||
|
|
3c6c3a14cf | ||
|
|
41bafdef92 | ||
|
|
fc624195c7 | ||
|
|
5b3a59e785 | ||
|
|
74abb2d3f1 | ||
|
|
3f12b34eea | ||
|
|
cd43004a91 | ||
|
|
3ac9461c00 | ||
|
|
309b07ae8a | ||
|
|
8fe56b6dc7 | ||
|
|
15a0dad0d8 | ||
|
|
93c777a418 | ||
|
|
5ff1c6578f | ||
|
|
7d4b9510a2 | ||
|
|
9f0ad2354a | ||
|
|
55b66dee99 | ||
|
|
4c64a16204 | ||
|
|
385e64cc52 | ||
|
|
be45a259db | ||
|
|
6f703fe577 | ||
|
|
cb8d93c499 | ||
|
|
ae1014c572 | ||
|
|
d5b9e67330 | ||
|
|
a81403766f | ||
|
|
83c069e234 |
33
README.md
33
README.md
@@ -1125,6 +1125,10 @@ import "github.com/duke-git/lancet/v2/maputil"
|
||||
- **<big>FindValuesBy</big>** : returns a slice of values from the map that satisfy the given predicate function.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/maputil.md#FindValuesBy)]
|
||||
[[play](https://go.dev/play/p/bvNwNBZDm6v)]
|
||||
- **<big>ToMarkdownTable</big>** : Convert a map slice data to a Markdown table string. It supports custom header display names and column display order.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/maputil.md#ToMarkdownTable)]
|
||||
[[play](https://go.dev/play/p/todo)]
|
||||
|
||||
|
||||
<h3 id="mathutil"> 13. Mathutil package implements some functions for math calculation. <a href="#index">index</a></h3>
|
||||
|
||||
@@ -1461,6 +1465,9 @@ import "github.com/duke-git/lancet/v2/slice"
|
||||
- **<big>ContainSubSlice</big>** : check if the slice contain a given subslice or not.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#ContainSubSlice)]
|
||||
[[play](https://go.dev/play/p/bcuQ3UT6Sev)]
|
||||
- **<big>ContainAny</big>** : check if the slice contains any element from the targets slice.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#ContainAny)]
|
||||
[[play](https://go.dev/play/p/4xoxhc9XSSw)]
|
||||
- **<big>Chunk</big>** : creates a slice of elements split into groups the length of size.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#Chunk)]
|
||||
[[play](https://go.dev/play/p/b4Pou5j2L_C)]
|
||||
@@ -1819,30 +1826,50 @@ import "github.com/duke-git/lancet/v2/structs"
|
||||
|
||||
- **<big>New</big>** : creates a `Struct` instance.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#New)]
|
||||
[[play](https://go.dev/play/p/O29l8kk-Z17)]
|
||||
- **<big>ToMap</big>** : converts a valid struct to a map.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#ToMap)]
|
||||
[[play](https://go.dev/play/p/qQbLySBgerZ)]
|
||||
- **<big>Fields</big>** : get all fields of a given struct, that the fields are abstract struct field.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#Fields)]
|
||||
[[play](https://go.dev/play/p/w3Kk_CyVY7D)]
|
||||
- **<big>Field</big>** : get an abstract field of a struct by given field name
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#Field)]
|
||||
[[play](https://go.dev/play/p/KocZMSYarza)]
|
||||
- **<big>IsStruct</big>** : check if the struct is valid.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#IsStruct)]
|
||||
[[play](https://go.dev/play/p/bU2FSdkbK1C)]
|
||||
- **<big>Tag</big>** : get a `Tag` of the `Field`, `Tag` is a abstract struct field tag.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#Tag)]
|
||||
[[play](https://go.dev/play/p/DVrx5HvvUJr)]
|
||||
- **<big>Name</big>** : get the field name.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#Name)]
|
||||
[[play](https://go.dev/play/p/zfIGlqsatee)]
|
||||
- **<big>Value</big>** : get the `Field` underlying value.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#Value)]
|
||||
[[play](https://go.dev/play/p/qufYEU2o4Oi)]
|
||||
- **<big>Kind</big>** : get the field's kind.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#Kind)]
|
||||
[[play](https://go.dev/play/p/wg4NlcUNG5o)]
|
||||
- **<big>IsEmbedded</big>** : check if the field is an embedded field.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#IsEmbedded)]
|
||||
[[play](https://go.dev/play/p/wV2PrbYm3Ec)]
|
||||
- **<big>IsExported</big>** : check if the field is exported.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#IsExported)]
|
||||
[[play](https://go.dev/play/p/csK4AXYaNbJ)]
|
||||
- **<big>IsZero</big>** : check if the field is zero value.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#IsZero)]
|
||||
[[play](https://go.dev/play/p/RzqpGISf87r)]
|
||||
- **<big>IsSlice</big>** : check if the field is a slice.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#IsSlice)]
|
||||
[[play](https://go.dev/play/p/MKz4CgBIUrU)]
|
||||
- **<big>IsTargetType</big>** : check if the field is target type.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#IsTargetType)]
|
||||
[[play](https://go.dev/play/p/Ig75P-agN39)]
|
||||
- **<big>TypeName</big>** : Return struct type name.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#TypeName)]
|
||||
[[play](https://go.dev/play/p/todo)]
|
||||
|
||||
|
||||
<h3 id="strutil"> 21. Strutil package contains some functions to manipulate string. <a href="#index">index</a></h3>
|
||||
|
||||
@@ -2301,6 +2328,12 @@ import "github.com/duke-git/lancet/v2/validator"
|
||||
- **<big>IsChinaUnionPay</big>** : check if a give string is a valid china union pay number or not.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/validator.md#IsChinaUnionPay)]
|
||||
[[play](https://go.dev/play/p/yafpdxLiymu)]
|
||||
- **<big>IsPassport</big>** : Passport validation(using regex).
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/validator.md#IsPassport)]
|
||||
[[play](https://go.dev/play/p/todo)]
|
||||
- **<big>IsChineseHMPassport</big>** : Mainland travel permit for Hong Kong, Macao validation (using regex).
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/validator.md#IsChineseHMPassport)]
|
||||
[[play](https://go.dev/play/p/todo)]
|
||||
|
||||
<h3 id="xerror"> 25. Xerror package implements helpers for errors. <a href="#index">index</a></h3>
|
||||
|
||||
|
||||
@@ -1135,6 +1135,9 @@ import "github.com/duke-git/lancet/v2/maputil"
|
||||
- **<big>FindValuesBy</big>** : 返回一个切片,包含满足给定谓词判断函数的 map 中的值。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/maputil.md#FindValuesBy)]
|
||||
[[play](https://go.dev/play/p/bvNwNBZDm6v)]
|
||||
- **<big>ToMarkdownTable</big>** : 将一个 map 切片数据转换为 Markdown 表格字符串。支持自定义表头显示名称和列的显示顺序。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/maputil.md#ToMarkdownTable)]
|
||||
[[play](https://go.dev/play/p/todo)]
|
||||
|
||||
<h3 id="mathutil"> 14. mathutil 包实现了一些数学计算的函数。 <a href="#index">回到目录</a></h3>
|
||||
|
||||
@@ -1468,6 +1471,9 @@ import "github.com/duke-git/lancet/v2/slice"
|
||||
- **<big>ContainSubSlice</big>** : 判断 slice 是否包含 subslice。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#ContainSubSlice)]
|
||||
[[play](https://go.dev/play/p/bcuQ3UT6Sev)]
|
||||
- **<big>ContainAny</big>** : 判断 slice 是否包含 targets 切片中的任意一个元素。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#ContainAny)]
|
||||
[[play](https://go.dev/play/p/4xoxhc9XSSw)]
|
||||
- **<big>Chunk</big>** : 按照 size 参数均分 slice。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#Chunk)]
|
||||
[[play](https://go.dev/play/p/b4Pou5j2L_C)]
|
||||
@@ -1825,32 +1831,50 @@ import "github.com/duke-git/lancet/v2/structs"
|
||||
|
||||
- **<big>New</big>** : `Struct`结构体的构造函数。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#New)]
|
||||
[[play](https://go.dev/play/p/O29l8kk-Z17)]
|
||||
- **<big>ToMap</big>** : 将一个合法的 struct 对象转换为 map[string]any。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#ToMap)]
|
||||
[[play](https://go.dev/play/p/qQbLySBgerZ)]
|
||||
- **<big>Fields</big>** : 获取一个 struct 对象的属性列表。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#Fields)]
|
||||
[[play](https://go.dev/play/p/w3Kk_CyVY7D)]
|
||||
- **<big>Field</big>** : 根据属性名获取一个 struct 对象的属性。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#Fields)]
|
||||
[[play](https://go.dev/play/p/KocZMSYarza)]
|
||||
- **<big>IsStruct</big>** : 判断是否为一个合法的 struct 对象。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#IsStruct)]
|
||||
[[play](https://go.dev/play/p/bU2FSdkbK1C)]
|
||||
- **<big>Tag</big>** : 获取`Field`的`Tag`,默认的 tag key 是 json。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#Tag)]
|
||||
[[play](https://go.dev/play/p/DVrx5HvvUJr)]
|
||||
- **<big>Name</big>** : 获取属性名。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#Name)]
|
||||
[[play](https://go.dev/play/p/zfIGlqsatee)]
|
||||
- **<big>Value</big>** : 获取`Field`属性的值。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#Value)]
|
||||
[[play](https://go.dev/play/p/qufYEU2o4Oi)]
|
||||
- **<big>Kind</big>** : 获取属性 Kind。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#Kind)]
|
||||
[[play](https://go.dev/play/p/wg4NlcUNG5o)]
|
||||
- **<big>IsEmbedded</big>** : 判断属性是否为嵌入。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#IsEmbedded)]
|
||||
[[play](https://go.dev/play/p/wV2PrbYm3Ec)]
|
||||
- **<big>IsExported</big>** : 判断属性是否导出。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#IsExported)]
|
||||
[[play](https://go.dev/play/p/csK4AXYaNbJ)]
|
||||
- **<big>IsZero</big>** : 判断属性是否为零值。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#IsZero)]
|
||||
[[play](https://go.dev/play/p/RzqpGISf87r)]
|
||||
- **<big>IsSlice</big>** : 判断属性是否是切片。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#IsSlice)]
|
||||
[[play](https://go.dev/play/p/MKz4CgBIUrU)]
|
||||
- **<big>IsTargetType</big>** : 判断属性是否是目标类型。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#IsTargetType)]
|
||||
[[play](https://go.dev/play/p/Ig75P-agN39)]
|
||||
- **<big>TypeName</big>** : 获取结构体类型名。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#TypeName)]
|
||||
[[play](https://go.dev/play/p/todo)]
|
||||
|
||||
|
||||
<h3 id="strutil"> 22. strutil 包含字符串处理的相关函数。 <a href="#index">回到目录</a></h3>
|
||||
|
||||
@@ -2310,7 +2334,12 @@ import "github.com/duke-git/lancet/v2/validator"
|
||||
- **<big>IsChinaUnionPay</big>** : 检查字符串是否是有效的中国银联卡号。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/validator.md#IsChinaUnionPay)]
|
||||
[[play](https://go.dev/play/p/yafpdxLiymu)]
|
||||
|
||||
- **<big>IsPassport</big>** : 判断护照(正则判断)。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/validator.md#IsPassport)]
|
||||
[[play](https://go.dev/play/p/todo)]
|
||||
- **<big>IsChineseHMPassport</big>** : 判断港澳台通行证(正则判断)。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/validator.md#IsChineseHMPassport)]
|
||||
[[play](https://go.dev/play/p/todo)]
|
||||
<h3 id="xerror"> 26. xerror 包实现一些错误处理函数。 <a href="#index">回到目录</a></h3>
|
||||
|
||||
```go
|
||||
|
||||
@@ -228,6 +228,42 @@ func ToPointer[T any](value T) *T {
|
||||
return &value
|
||||
}
|
||||
|
||||
// ToPointers convert a slice of values to a slice of pointers.
|
||||
// Play: todo
|
||||
func ToPointers[T any](values []T) []*T {
|
||||
result := make([]*T, len(values))
|
||||
for i := range values {
|
||||
result[i] = &values[i]
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// FromPointer returns the value pointed to by the pointer.
|
||||
// Play: todo
|
||||
func FromPointer[T any](ptr *T) T {
|
||||
if ptr == nil {
|
||||
var zeroValue T
|
||||
return zeroValue
|
||||
}
|
||||
|
||||
return *ptr
|
||||
}
|
||||
|
||||
// FromPointers convert a slice of pointers to a slice of values.
|
||||
// Play: todo
|
||||
func FromPointers[T any](pointers []*T) []T {
|
||||
result := make([]T, len(pointers))
|
||||
for i, ptr := range pointers {
|
||||
if ptr == nil {
|
||||
var zeroValue T
|
||||
result[i] = zeroValue
|
||||
} else {
|
||||
result[i] = *ptr
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// ToMap convert a slice of structs to a map based on iteratee function.
|
||||
// Play: https://go.dev/play/p/tVFy7E-t24l
|
||||
func ToMap[T any, K comparable, V any](array []T, iteratee func(T) (K, V)) map[K]V {
|
||||
@@ -406,15 +442,15 @@ func ToStdBase64(value any) string {
|
||||
if value == nil || (reflect.ValueOf(value).Kind() == reflect.Ptr && reflect.ValueOf(value).IsNil()) {
|
||||
return ""
|
||||
}
|
||||
switch value.(type) {
|
||||
switch v := value.(type) {
|
||||
case []byte:
|
||||
return base64.StdEncoding.EncodeToString(value.([]byte))
|
||||
return base64.StdEncoding.EncodeToString(v)
|
||||
case string:
|
||||
return base64.StdEncoding.EncodeToString([]byte(value.(string)))
|
||||
return base64.StdEncoding.EncodeToString([]byte(v))
|
||||
case error:
|
||||
return base64.StdEncoding.EncodeToString([]byte(value.(error).Error()))
|
||||
return base64.StdEncoding.EncodeToString([]byte(v.Error()))
|
||||
default:
|
||||
marshal, err := json.Marshal(value)
|
||||
marshal, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
@@ -428,15 +464,15 @@ func ToUrlBase64(value any) string {
|
||||
if value == nil || (reflect.ValueOf(value).Kind() == reflect.Ptr && reflect.ValueOf(value).IsNil()) {
|
||||
return ""
|
||||
}
|
||||
switch value.(type) {
|
||||
switch v := value.(type) {
|
||||
case []byte:
|
||||
return base64.URLEncoding.EncodeToString(value.([]byte))
|
||||
return base64.URLEncoding.EncodeToString(v)
|
||||
case string:
|
||||
return base64.URLEncoding.EncodeToString([]byte(value.(string)))
|
||||
return base64.URLEncoding.EncodeToString([]byte(v))
|
||||
case error:
|
||||
return base64.URLEncoding.EncodeToString([]byte(value.(error).Error()))
|
||||
return base64.URLEncoding.EncodeToString([]byte(v.Error()))
|
||||
default:
|
||||
marshal, err := json.Marshal(value)
|
||||
marshal, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
@@ -450,7 +486,7 @@ func ToRawStdBase64(value any) string {
|
||||
if value == nil || (reflect.ValueOf(value).Kind() == reflect.Ptr && reflect.ValueOf(value).IsNil()) {
|
||||
return ""
|
||||
}
|
||||
switch value.(type) {
|
||||
switch v := value.(type) {
|
||||
case []byte:
|
||||
return base64.RawStdEncoding.EncodeToString(value.([]byte))
|
||||
case string:
|
||||
@@ -458,7 +494,7 @@ func ToRawStdBase64(value any) string {
|
||||
case error:
|
||||
return base64.RawStdEncoding.EncodeToString([]byte(value.(error).Error()))
|
||||
default:
|
||||
marshal, err := json.Marshal(value)
|
||||
marshal, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
@@ -472,7 +508,7 @@ func ToRawUrlBase64(value any) string {
|
||||
if value == nil || (reflect.ValueOf(value).Kind() == reflect.Ptr && reflect.ValueOf(value).IsNil()) {
|
||||
return ""
|
||||
}
|
||||
switch value.(type) {
|
||||
switch v := value.(type) {
|
||||
case []byte:
|
||||
return base64.RawURLEncoding.EncodeToString(value.([]byte))
|
||||
case string:
|
||||
@@ -480,7 +516,7 @@ func ToRawUrlBase64(value any) string {
|
||||
case error:
|
||||
return base64.RawURLEncoding.EncodeToString([]byte(value.(error).Error()))
|
||||
default:
|
||||
marshal, err := json.Marshal(value)
|
||||
marshal, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -302,6 +302,75 @@ func TestToPointer(t *testing.T) {
|
||||
assert.Equal(*result, 123)
|
||||
}
|
||||
|
||||
func TestToPointers(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert := internal.NewAssert(t, "TestToPointers")
|
||||
|
||||
intVals := []int{1, 2, 3}
|
||||
result := ToPointers(intVals)
|
||||
|
||||
assert.Equal(3, len(result))
|
||||
assert.Equal(1, *result[0])
|
||||
assert.Equal(2, *result[1])
|
||||
assert.Equal(3, *result[2])
|
||||
|
||||
stringVals := []string{"a", "b", "c"}
|
||||
resultStr := ToPointers(stringVals)
|
||||
assert.Equal(3, len(resultStr))
|
||||
assert.Equal("a", *resultStr[0])
|
||||
assert.Equal("b", *resultStr[1])
|
||||
assert.Equal("c", *resultStr[2])
|
||||
}
|
||||
|
||||
func TestFromPointer(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert := internal.NewAssert(t, "TestFromPointer")
|
||||
|
||||
intVal := 123
|
||||
pointer := &intVal
|
||||
result := FromPointer(pointer)
|
||||
|
||||
assert.Equal(123, result)
|
||||
|
||||
stringVal := "abc"
|
||||
stringPointer := &stringVal
|
||||
resultStr := FromPointer(stringPointer)
|
||||
|
||||
assert.Equal("abc", resultStr)
|
||||
}
|
||||
|
||||
func TestFromPointers(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert := internal.NewAssert(t, "TestFromPointers")
|
||||
|
||||
intPointers := []*int{new(int), new(int), new(int)}
|
||||
*intPointers[0] = 1
|
||||
*intPointers[1] = 2
|
||||
*intPointers[2] = 3
|
||||
|
||||
result := FromPointers(intPointers)
|
||||
|
||||
assert.Equal(3, len(result))
|
||||
assert.Equal(1, result[0])
|
||||
assert.Equal(2, result[1])
|
||||
assert.Equal(3, result[2])
|
||||
|
||||
stringPointers := []*string{new(string), new(string), new(string)}
|
||||
*stringPointers[0] = "a"
|
||||
*stringPointers[1] = "b"
|
||||
*stringPointers[2] = "c"
|
||||
|
||||
resultStr := FromPointers(stringPointers)
|
||||
|
||||
assert.Equal(3, len(resultStr))
|
||||
assert.Equal("a", resultStr[0])
|
||||
assert.Equal("b", resultStr[1])
|
||||
assert.Equal("c", resultStr[2])
|
||||
}
|
||||
|
||||
func TestEncodeByte(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
@@ -1,51 +1,51 @@
|
||||
-----BEGIN rsa private key-----
|
||||
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
|
||||
-----END rsa private key-----
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
-----BEGIN rsa public key-----
|
||||
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAw0anfgtraA2uaZwoLpLB
|
||||
vo1EkfYvDBgeXoMQ4WMKbcw6jU8k18E+f3WM52I2RssEk6g0X1vIiarHZU5qyxbv
|
||||
2iNoT7EfgizzlXYvx06pM69GNBQrV46+lNhiOv4eeLZcOHBnxAyizlIKyLuwO+C1
|
||||
cX/6BxuXjX3ogw+6IaBFZN/EOMmTWc6sPKrnNCEqgCNiTbPAXb+N8j5Iv8QPs0Aw
|
||||
VTB3jC9LP0mRt2GGD0cu+QIZkoGEhyW9Dd+0tgeIK32uXCF8uDjNpUV1TxCdn5H+
|
||||
lvgddxfiaIJAdY5UaTx48XRIdULhQrVKXJNyTYWTXbezViYIf1nXOdDI2hsGgKTf
|
||||
NVCocDT1LcT5zebijFHVCWfAAKEPRLdzPZomkFIa3rQQgChgePzE8Oqsaxuwx8sU
|
||||
09NAo3giSq1OmBBwly7h60Lwtm8+XuwmWtEgZoSQy2Hm8UMAJ3guotfYuS8mPQBi
|
||||
55JZpdqVN0D5SdwuWXhHpO4xDodNYAoMMei1NNgrX2zN3FCS8PVi97wD6cQ8xsWM
|
||||
62nByrzIjmPwTfoCb3qP/928FMJMg7b3bYBrUnOrpVWZfAIDs4H7DoP8VLL4rMgB
|
||||
5PuWNQ13gtX7y5ZOyLhopiKLnar9XiN8GAMANBdKRI2anGEozrfJoelJ5POXZwSa
|
||||
tRjbL+nWO3YnBzEunCw1xmMCAwEAAQ==
|
||||
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAv25yPU83rpR+acaNoyEO
|
||||
rIBo3gBcykscNL1vbMDbYc5jYib7BvxoOkdfeIhjTIDfMVzkzBXAC0eNbMey1/nC
|
||||
cjoFNPXDShsyig8paDl7PG0IVOYpuXDQ/vi82F7rrXehESrImIBPPDTrZWeg3jqF
|
||||
VkZt6QnB/j7dMKPMeJcarLvwMDKqTM3xguPAAoCAEy9Lylu2zncI8BPyhvz+++j0
|
||||
jBdRl8RYHUyE60BIEROFcHI7lnWMoH/Uj8cWu8aTRPtNZdk4l9hBHotfi7qvO9b4
|
||||
jB/75Qko/MY5YyrohAwQN/f9zVuTtlfFxKCb1+5Lxq4v4llwUv9eA7euMJf/nDU2
|
||||
9m4Oshu7snkYgwB4Cd+bVqS8z9vW+brXtP6ZZTJls6pBX4FFukNkCUgiNBuUVDO0
|
||||
4AUNSBfAxiHXS88zLdrTRIfM9MdgoGQapqHaiieQ4GdFbndKfck0AfoSXy0NtWOX
|
||||
4/IZWUphqnyAKO1w8hzK1iydgv4mHpjKr5I3sh1ARh9baNUXLH2A9rTQvk3fXsK7
|
||||
RRkwP8gH9BKvtQiHfVVqAh9MPdPygHiOUgGgqTQj2xb1KXS1yzJVcgDFYNv8NrE1
|
||||
M8EdCaItfRWU7MkzecGriQFQ42AvIMFLMyD25hXDj/mZgaw1CgGoemMJoAm+yu1N
|
||||
RUB4dW1vHqn/UxhcWHrLhJsCAwEAAQ==
|
||||
-----END rsa public key-----
|
||||
|
||||
@@ -114,6 +114,7 @@ export const enConfig: LocaleSpecificConfig<DefaultTheme.Config> = {
|
||||
],
|
||||
},
|
||||
{ text: 'datetime', link: '/en/api/packages/datetime' },
|
||||
{ text: 'enum', link: '/en/api/packages/enum' },
|
||||
{ text: 'eventbus', link: '/en/api/packages/eventbus' },
|
||||
{ text: 'fileutil', link: '/en/api/packages/fileutil' },
|
||||
{ text: 'formatter', link: '/en/api/packages/formatter' },
|
||||
@@ -128,9 +129,9 @@ export const enConfig: LocaleSpecificConfig<DefaultTheme.Config> = {
|
||||
{ text: 'stream', link: '/en/api/packages/stream' },
|
||||
{ text: 'struct', link: '/en/api/packages/struct' },
|
||||
{ text: 'strutil', link: '/en/api/packages/strutil' },
|
||||
{ text: 'system', link: '/en/api/packages/system' },
|
||||
{ text: 'tuple', link: '/en/api/packages/tuple' },
|
||||
{ text: 'validator', link: '/en/api/packages/validator' },
|
||||
{ text: 'system', link: '/en/api/packages/system' },
|
||||
{ text: 'xerror', link: '/en/api/packages/xerror' },
|
||||
],
|
||||
},
|
||||
|
||||
@@ -128,7 +128,7 @@ export const zhConfig: LocaleSpecificConfig<DefaultTheme.Config> = {
|
||||
},
|
||||
{ text: '日期&时间', link: '/api/packages/datetime' },
|
||||
{ text: '事件总线', link: '/api/packages/eventbus' },
|
||||
{ text: '文件', link: '/api/packages/fileutil' },
|
||||
{ text: '文件处理', link: '/api/packages/fileutil' },
|
||||
{ text: '格式化工具', link: '/api/packages/formatter' },
|
||||
{ text: '函数', link: '/api/packages/function' },
|
||||
{ text: '数学工具', link: '/api/packages/mathutil' },
|
||||
@@ -141,9 +141,10 @@ export const zhConfig: LocaleSpecificConfig<DefaultTheme.Config> = {
|
||||
{ text: '流', link: '/api/packages/stream' },
|
||||
{ text: '结构体', link: '/api/packages/struct' },
|
||||
{ text: '字符串', link: '/api/packages/strutil' },
|
||||
{ text: '系统', link: '/api/packages/system' },
|
||||
{ text: '枚举', link: '/api/packages/enum' },
|
||||
{ text: '元组', link: '/api/packages/tuple' },
|
||||
{ text: '验证器', link: '/api/packages/validator' },
|
||||
{ text: '系统工具函数', link: '/api/packages/system' },
|
||||
{ text: '错误处理', link: '/api/packages/xerror' },
|
||||
],
|
||||
},
|
||||
|
||||
@@ -46,6 +46,7 @@ outline: deep
|
||||
<div class="package-cell">cryptor</div>
|
||||
<div class="package-cell">datastructure</div>
|
||||
<div class="package-cell">datetime</div>
|
||||
<div class="package-cell">enum</div>
|
||||
<div class="package-cell">eventbus</div>
|
||||
<div class="package-cell">fileutil</div>
|
||||
<div class="package-cell">formatter</div>
|
||||
|
||||
850
docs/api/packages/enum.md
Normal file
850
docs/api/packages/enum.md
Normal file
@@ -0,0 +1,850 @@
|
||||
# Enum
|
||||
|
||||
Enum实现一个简单枚举工具包。
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## 源码:
|
||||
|
||||
- [https://github.com/duke-git/lancet/blob/main/enum/enum.go](https://github.com/duke-git/lancet/blob/main/enum/enum.go)
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## 用法:
|
||||
|
||||
```go
|
||||
import (
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
```
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## 目录
|
||||
|
||||
- [NewItem](#NewItem)
|
||||
- [NewItemsFromPairs](#NewItemsFromPairs)
|
||||
- [Value](#Value)
|
||||
- [Name](#Name)
|
||||
- [Valid](#Valid)
|
||||
- [MarshalJSON](#MarshalJSON)
|
||||
- [NewRegistry](#NewRegistry)
|
||||
- [Add](#Add)
|
||||
- [Remove](#Remove)
|
||||
- [Update](#Update)
|
||||
- [GetByValue](#GetByValue)
|
||||
- [GetByName](#GetByName)
|
||||
- [Items](#Items)
|
||||
- [Contains](#Contains)
|
||||
- [Size](#Size)
|
||||
- [Range](#Range)
|
||||
- [SortedItems](#SortedItems)
|
||||
- [Filter](#Filter)
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## 文档
|
||||
|
||||
### <span id="NewItem">NewItem</span>
|
||||
|
||||
<p>创建枚举项。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func NewItem[T comparable](value T, name string) *Item[T]
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
item1 := enum.NewItem(Active, "Active")
|
||||
item2 := enum.NewItem(Inactive, "Inactive")
|
||||
|
||||
fmt.Println(item1.Name(), item1.Value())
|
||||
fmt.Println(item2.Name(), item2.Value())
|
||||
|
||||
// Output:
|
||||
// Active 1
|
||||
// Inactive 2
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="NewItemsFromPairs">NewItemsFromPairs</span>
|
||||
|
||||
<p>从Pair结构体的切片创建枚举项。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func NewItemsFromPairs[T comparable](pairs ...Pair[T]) []*Item[T]
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
items := enum.NewItemsFromPairs(
|
||||
enum.Pair[Status]{Value: Active, Name: "Active"},
|
||||
enum.Pair[Status]{Value: Inactive, Name: "Inactive"},
|
||||
)
|
||||
|
||||
fmt.Println(items[0].Name(), items[0].Value())
|
||||
fmt.Println(items[1].Name(), items[1].Value())
|
||||
|
||||
// Output:
|
||||
// Active 1
|
||||
// Inactive 2
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Value">Value</span>
|
||||
|
||||
<p>返回枚举项的值。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func (it *Item[T]) Value() T
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
items := enum.NewItemsFromPairs(
|
||||
enum.Pair[Status]{Value: Active, Name: "Active"},
|
||||
enum.Pair[Status]{Value: Inactive, Name: "Inactive"},
|
||||
)
|
||||
|
||||
fmt.Println(items[0].Name(), items[0].Value())
|
||||
fmt.Println(items[1].Name(), items[1].Value())
|
||||
|
||||
// Output:
|
||||
// Active 1
|
||||
// Inactive 2
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Name">Name</span>
|
||||
|
||||
<p>返回枚举项的名称。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func (it *Item[T]) Name() string
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
items := enum.NewItemsFromPairs(
|
||||
enum.Pair[Status]{Value: Active, Name: "Active"},
|
||||
enum.Pair[Status]{Value: Inactive, Name: "Inactive"},
|
||||
)
|
||||
|
||||
fmt.Println(items[0].Name(), items[0].Value())
|
||||
fmt.Println(items[1].Name(), items[1].Value())
|
||||
|
||||
// Output:
|
||||
// Active 1
|
||||
// Inactive 2
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Valid">Valid</span>
|
||||
|
||||
<p>检查枚举项是否有效。如果提供了自定义检查函数,将使用该函数验证值。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func (it *Item[T]) Valid(checker ...func(T) bool) bool
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
item := enum.NewItem(Active, "Active")
|
||||
fmt.Println(item.Valid())
|
||||
|
||||
invalidItem := enum.NewItem(Unknown, "")
|
||||
fmt.Println(invalidItem.Valid())
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="MarshalJSON">MarshalJSON</span>
|
||||
|
||||
<p>枚举项实现json.Marshaler 接口。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func (it *Item[T]) MarshalJSON() ([]byte, error)
|
||||
func (it *Item[T]) UnmarshalJSON(data []byte) error
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
item := enum.NewItem(Active, "Active")
|
||||
data, _ := item.MarshalJSON()
|
||||
fmt.Println(string(data))
|
||||
|
||||
var unmarshaledItem Item[Status]
|
||||
_ = unmarshaledItem.UnmarshalJSON(data)
|
||||
fmt.Println(unmarshaledItem.Name(), unmarshaledItem.Value())
|
||||
|
||||
// Output:
|
||||
// {"name":"Active","value":1}
|
||||
// Active 1
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="NewRegistry">NewRegistry</span>
|
||||
|
||||
<p>Registry 定义了一个通用的枚举注册表结构体。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func NewRegistry[T comparable](items ...*Item[T]) *Registry[T]
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
registry := enum.NewRegistry[Status]()
|
||||
item1 := enum.NewItem(Active, "Active")
|
||||
item2 := enum.NewItem(Inactive, "Inactive")
|
||||
|
||||
registry.Add(item1, item2)
|
||||
|
||||
if item, found := registry.GetByValue(Active); found {
|
||||
fmt.Println("Found by value:", item.Name())
|
||||
}
|
||||
|
||||
if item, found := registry.GetByName("Inactive"); found {
|
||||
fmt.Println("Found by name:", item.Value())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Found by value: Active
|
||||
// Found by name: 2
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Add">Add</span>
|
||||
|
||||
<p>向枚举注册表添加枚举项。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func (r *Registry[T]) Add(items ...*Item[T])
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
registry := enum.NewRegistry[Status]()
|
||||
item1 := enum.NewItem(Active, "Active")
|
||||
item2 := enum.NewItem(Inactive, "Inactive")
|
||||
|
||||
registry.Add(item1, item2)
|
||||
|
||||
if item, found := registry.GetByValue(Active); found {
|
||||
fmt.Println("Found by value:", item.Name())
|
||||
}
|
||||
|
||||
if item, found := registry.GetByName("Inactive"); found {
|
||||
fmt.Println("Found by name:", item.Value())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Found by value: Active
|
||||
// Found by name: 2
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Remove">Remove</span>
|
||||
|
||||
<p>在枚举注册表中删除枚举项。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func (r *Registry[T]) Remove(value T) bool
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
registry := enum.NewRegistry[Status]()
|
||||
item1 := enum.NewItem(Active, "Active")
|
||||
|
||||
registry.Add(item1)
|
||||
fmt.Println("Size before removal:", registry.Size())
|
||||
|
||||
removed := registry.Remove(Active)
|
||||
fmt.Println("Removed:", removed)
|
||||
fmt.Println("Size after removal:", registry.Size())
|
||||
|
||||
// Output:
|
||||
// Size before removal: 1
|
||||
// Removed: true
|
||||
// Size after removal: 0
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Update">Update</span>
|
||||
|
||||
<p>在枚举注册表中更新枚举项。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func (r *Registry[T]) Update(value T, newName string) bool
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
registry := enum.NewRegistry[Status]()
|
||||
item1 := enum.NewItem(Active, "Active")
|
||||
|
||||
registry.Add(item1)
|
||||
updated := registry.Update(Active, "Activated")
|
||||
fmt.Println("Updated:", updated)
|
||||
|
||||
if item, found := registry.GetByValue(Active); found {
|
||||
fmt.Println("New name:", item.Name())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Updated: true
|
||||
// New name: Activated
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="GetByValue">GetByValue</span>
|
||||
|
||||
<p>在枚举注册表中通过值获取枚举项。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func (r *Registry[T]) GetByValue(value T) (*Item[T], bool)
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
registry := enum.NewRegistry[Status]()
|
||||
item1 := enum.NewItem(Active, "Active")
|
||||
|
||||
registry.Add(item1, item2)
|
||||
|
||||
if item, found := registry.GetByValue(Active); found {
|
||||
fmt.Println("Found name by value:", item.Name())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Found name by value: Active
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="GetByName">GetByName</span>
|
||||
|
||||
<p>在枚举注册表中通过名称获取枚举项。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func (r *Registry[T]) GetByName(name string) (*Item[T], bool)
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
registry := enum.NewRegistry[Status]()
|
||||
item := enum.NewItem(Active, "Active")
|
||||
|
||||
registry.Add(item)
|
||||
|
||||
if item, found := registry.GetByName("Active"); found {
|
||||
fmt.Println("Found value by name:", item.Value())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Found value by name: 1
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Items">Items</span>
|
||||
|
||||
<p>返回枚举注册表中的枚举项。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func (r *Registry[T]) Items() []*Item[T]
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
registry := enum.NewRegistry[Status]()
|
||||
item1 := enum.NewItem(Active, "Active")
|
||||
item2 := enum.NewItem(Inactive, "Inactive")
|
||||
|
||||
registry.Add(item1, item2)
|
||||
|
||||
for _, item := range registry.Items() {
|
||||
fmt.Println(item.Name(), item.Value())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Active 1
|
||||
// Inactive 2
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Contains">Contains</span>
|
||||
|
||||
<p>检查注册表中是否存在具有给定值的枚举项。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func (r *Registry[T]) Contains(value T) bool
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
registry := enum.NewRegistry[Status]()
|
||||
item := enum.NewItem(Active, "Active")
|
||||
registry.Add(item)
|
||||
|
||||
fmt.Println(registry.Contains(Active))
|
||||
fmt.Println(registry.Contains(Inactive))
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Size">Size</span>
|
||||
|
||||
<p>返回注册表中枚举项的数目。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func (r *Registry[T]) Size() int
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
registry := enum.NewRegistry[Status]()
|
||||
fmt.Println("Initial size:", registry.Size())
|
||||
|
||||
item1 := enum.NewItem(Active, "Active")
|
||||
item2 := enum.NewItem(Inactive, "Inactive")
|
||||
registry.Add(item1, item2)
|
||||
|
||||
fmt.Println("Size after adding items:", registry.Size())
|
||||
|
||||
registry.Remove(Active)
|
||||
fmt.Println("Size after removing an item:", registry.Size())
|
||||
|
||||
// Output:
|
||||
// Initial size: 0
|
||||
// Size after adding items: 2
|
||||
// Size after removing an item: 1
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Range">Range</span>
|
||||
|
||||
<p>遍历注册表中的所有枚举项,并应用给定的函数。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func (r *Registry[T]) Range(fn func(*Item[T]) bool)
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
registry := enum.NewRegistry[Status]()
|
||||
item1 := enum.NewItem(Active, "Active")
|
||||
item2 := enum.NewItem(Inactive, "Inactive")
|
||||
registry.Add(item1, item2)
|
||||
|
||||
registry.Range(func(item *Item[Status]) bool {
|
||||
fmt.Println(item.Name(), item.Value())
|
||||
return true // continue iteration
|
||||
})
|
||||
|
||||
// Output:
|
||||
// Active 1
|
||||
// Inactive 2
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="SortedItems">SortedItems</span>
|
||||
|
||||
<p>返回按给定比较函数排序的所有枚举项的切片。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func (r *Registry[T]) SortedItems(less func(*Item[T], *Item[T]) bool) []*Item[T]
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
registry := enum.NewRegistry[Status]()
|
||||
item1 := enum.NewItem(Inactive, "Inactive")
|
||||
item2 := enum.NewItem(Active, "Active")
|
||||
registry.Add(item1, item2)
|
||||
|
||||
for _, item := range registry.SortedItems(func(i1, i2 *Item[Status]) bool {
|
||||
return i1.value < i2.value
|
||||
}) {
|
||||
fmt.Println(item.Name(), item.Value())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Active 1
|
||||
// Inactive 2
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Filter">Filter</span>
|
||||
|
||||
<p>返回满足给定谓词函数的枚举项切片。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func (r *Registry[T]) Filter(predicate func(*Item[T]) bool) []*Item[T]
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
registry := enum.NewRegistry[Status]()
|
||||
item1 := enum.NewItem(Active, "Active")
|
||||
item2 := enum.NewItem(Inactive, "Inactive")
|
||||
registry.Add(item1, item2)
|
||||
|
||||
activeItems := registry.Filter(func(item *Item[Status]) bool {
|
||||
return item.Value() == Active
|
||||
})
|
||||
|
||||
for _, item := range activeItems {
|
||||
fmt.Println(item.Name(), item.Value())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Active 1
|
||||
}
|
||||
```
|
||||
@@ -79,6 +79,7 @@ import (
|
||||
- [SortByKey](#SortByKey)
|
||||
- [GetOrDefault](#GetOrDefault)
|
||||
- [FindValuesBy](#FindValuesBy)
|
||||
- [ToMarkdownTable](#ToMarkdownTable)
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
@@ -2345,3 +2346,68 @@ func main() {
|
||||
// [b d]
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="ToMarkdownTable">ToMarkdownTable</span>
|
||||
|
||||
<p>将一个 map 切片数据转换为 Markdown 表格字符串。支持自定义表头显示名称和列的显示顺序。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
编辑
|
||||
func ToMarkdownTable(data []map[string]interface{}, headerMap map[string]string, columnOrder []string) string
|
||||
```
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/maputil"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 基本用法:自动从数据中提取列名作为表头
|
||||
data := []map[string]interface{}{
|
||||
{"name": "Alice", "age": 25, "salary": 50000},
|
||||
{"name": "Bob", "age": 30, "salary": 60000},
|
||||
}
|
||||
|
||||
result := maputil.ToMarkdownTable(data, nil, nil)
|
||||
fmt.Println(result)
|
||||
|
||||
// 输出:
|
||||
// |name|age|salary|
|
||||
// |---|---|---|
|
||||
// |Alice|25|50000|
|
||||
// |Bob|30|60000|
|
||||
|
||||
// 自定义表头显示名称
|
||||
headerMap := map[string]string{
|
||||
"name": "姓名",
|
||||
"age": "年龄",
|
||||
"salary": "薪资",
|
||||
}
|
||||
result = maputil.ToMarkdownTable(data, headerMap, nil)
|
||||
fmt.Println(result)
|
||||
|
||||
// 输出:
|
||||
// |姓名|年龄|薪资|
|
||||
// |---|---|---|
|
||||
// |Alice|25|50000|
|
||||
// |Bob|30|60000|
|
||||
|
||||
// 自定义列顺序
|
||||
columnOrder := []string{"salary", "name"}
|
||||
result = maputil.ToMarkdownTable(data, nil, columnOrder)
|
||||
fmt.Println(result)
|
||||
|
||||
// 输出:
|
||||
// |salary|name|
|
||||
// |---|---|
|
||||
// |50000|Alice|
|
||||
// |60000|Bob|
|
||||
}
|
||||
```
|
||||
@@ -27,6 +27,7 @@ import (
|
||||
- [Contain](#Contain)
|
||||
- [ContainBy](#ContainBy)
|
||||
- [ContainSubSlice](#ContainSubSlice)
|
||||
- [ContainAny](#ContainAny)
|
||||
- [Chunk](#Chunk)
|
||||
- [Compact](#Compact)
|
||||
- [Concat](#Concat)
|
||||
@@ -256,6 +257,43 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="ContainAny">ContainAny</span>
|
||||
|
||||
<p>判断slice是否包含targets切片中的任意一个元素</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func ContainAny[T comparable](slice []T, targets []T) bool
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/4xoxhc9XSSw)</span></b>
|
||||
|
||||
```go
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/slice"
|
||||
)
|
||||
|
||||
func main() {
|
||||
result1 := slice.ContainAny([]string{"a", "b", "c"}, []string{"a"})
|
||||
result2 := slice.ContainAny([]string{"a", "b", "c"}, []string{"d", "e"})
|
||||
result3 := slice.ContainAny([]string{"a", "b", "c"}, []string{"d", "a"})
|
||||
result4 := slice.ContainAny([]string{"a", "b", "c"}, []string{})
|
||||
|
||||
fmt.Println(result1)
|
||||
fmt.Println(result2)
|
||||
fmt.Println(result3)
|
||||
fmt.Println(result4)
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
// true
|
||||
// false
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Chunk">Chunk</span>
|
||||
|
||||
<p>按照size参数均分slice</p>
|
||||
|
||||
@@ -31,6 +31,7 @@ import (
|
||||
- [IsStruct](#IsStruct)
|
||||
- [Tag](#Tag)
|
||||
- [Name](#Name)
|
||||
- [TypeName](#TypeName)
|
||||
- [Value](#Value)
|
||||
- [Kind](#Kind)
|
||||
- [IsEmbedded](#IsEmbedded)
|
||||
@@ -53,7 +54,7 @@ import (
|
||||
func New(value any, tagName ...string) *Struct
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/O29l8kk-Z17)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -68,7 +69,11 @@ func main() {
|
||||
}
|
||||
p1 := &People{Name: "11"}
|
||||
s := structs.New(p1)
|
||||
// to do something
|
||||
|
||||
fmt.Println(s.ToMap())
|
||||
|
||||
// Output:
|
||||
// map[name:11] <nil>
|
||||
}
|
||||
```
|
||||
|
||||
@@ -88,7 +93,7 @@ func (s *Struct) ToMap() (map[string]any, error)
|
||||
func ToMap(v any) (map[string]any, error)
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/qQbLySBgerZ)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -129,7 +134,7 @@ func main() {
|
||||
func (s *Struct) Fields() []*Field
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/w3Kk_CyVY7D)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -161,10 +166,10 @@ func main() {
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func (s *Struct) Field(name string) *Field
|
||||
func (s *Struct) Field(name string) (*Field, bool)
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/KocZMSYarza)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -180,12 +185,14 @@ func main() {
|
||||
}
|
||||
p1 := &People{Name: "11"}
|
||||
s := structs.New(p1)
|
||||
f := s.Field("Name")
|
||||
f, found := s.Field("Name")
|
||||
|
||||
fmt.Println(f.Value())
|
||||
fmt.Println(found)
|
||||
|
||||
// Output:
|
||||
// 11
|
||||
// true
|
||||
}
|
||||
```
|
||||
|
||||
@@ -199,7 +206,7 @@ func main() {
|
||||
func (s *Struct) IsStruct() bool
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/bU2FSdkbK1C)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -233,7 +240,7 @@ func main() {
|
||||
func (f *Field) Tag() *Tag
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/DVrx5HvvUJr)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -270,7 +277,7 @@ func main() {
|
||||
func (f *Field) Value() any
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/qufYEU2o4Oi)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -306,7 +313,7 @@ func main() {
|
||||
func (f *Field) IsEmbedded() bool
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/wV2PrbYm3Ec)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -351,7 +358,7 @@ func main() {
|
||||
func (f *Field) IsExported() bool
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/csK4AXYaNbJ)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -390,7 +397,7 @@ func main() {
|
||||
func (f *Field) IsZero() bool
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/RzqpGISf87r)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -429,7 +436,7 @@ func main() {
|
||||
func (f *Field) Name() string
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/zfIGlqsatee)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -468,7 +475,7 @@ func main() {
|
||||
func (f *Field) Kind() reflect.Kind
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/wg4NlcUNG5o)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -497,6 +504,42 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="TypeName">TypeName</span>
|
||||
|
||||
<p>获取结构体类型名。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func (s *Struct) TypeName() string
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/structs"
|
||||
)
|
||||
|
||||
func main() {
|
||||
type Parent struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
|
||||
p := &Parent{Age: 11}
|
||||
s := structs.New(p1)
|
||||
|
||||
fmt.Println(s.TypeName())
|
||||
|
||||
// Output:
|
||||
// Parent
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="IsSlice">IsSlice</span>
|
||||
|
||||
<p>判断属性是否是切片</p>
|
||||
@@ -507,7 +550,7 @@ func main() {
|
||||
func (f *Field) IsSlice() bool
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/MKz4CgBIUrU)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -544,7 +587,7 @@ func main() {
|
||||
func (f *Field) IsTargetType(targetType reflect.Kind) bool
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/Ig75P-agN39)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
@@ -65,6 +65,8 @@ import (
|
||||
- [IsAmericanExpress](#IsAmericanExpress)
|
||||
- [IsUnionPay](#IsUnionPay)
|
||||
- [IsChinaUnionPay](#IsChinaUnionPay)
|
||||
- [IsPassport](#IsPassport)
|
||||
- [IsChineseHMPassport](#IsChineseHMPassport)
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
@@ -844,20 +846,20 @@ import (
|
||||
|
||||
func main() {
|
||||
result1 := validator.IsAlphaNumeric("ABC")
|
||||
result2 := validator.IsAlphaNumeric("123")
|
||||
result3 := validator.IsAlphaNumeric("abc123")
|
||||
result4 := validator.IsAlphaNumeric("abc123@#$")
|
||||
result2 := validator.IsAlphaNumeric("123")
|
||||
result3 := validator.IsAlphaNumeric("abc123")
|
||||
result4 := validator.IsAlphaNumeric("abc123@#$")
|
||||
|
||||
fmt.Println(result1)
|
||||
fmt.Println(result2)
|
||||
fmt.Println(result3)
|
||||
fmt.Println(result4)
|
||||
fmt.Println(result1)
|
||||
fmt.Println(result2)
|
||||
fmt.Println(result3)
|
||||
fmt.Println(result4)
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// true
|
||||
// true
|
||||
// false
|
||||
// Output:
|
||||
// true
|
||||
// true
|
||||
// true
|
||||
// false
|
||||
}
|
||||
```
|
||||
|
||||
@@ -1567,3 +1569,80 @@ func main() {
|
||||
// false
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="IsPassport">IsPassport</span>
|
||||
|
||||
<p>判断护照(正则判断)。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func IsPassport(passport, country string) bool
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/validator"
|
||||
)
|
||||
|
||||
func main() {
|
||||
result1 := validator.IsPassport("P123456789", "CN")
|
||||
result2 := validator.IsPassport("123456789", "US")
|
||||
result3 := validator.IsPassport("AB1234567", "RU")
|
||||
result4 := validator.IsPassport("123456789", "CN")
|
||||
|
||||
fmt.Println(result1)
|
||||
fmt.Println(result2)
|
||||
fmt.Println(result3)
|
||||
fmt.Println(result4)
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// true
|
||||
// true
|
||||
// false
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="IsChineseHMPassport">IsChineseHMPassport</span>
|
||||
|
||||
<p>判断港澳台通行证(正则判断)。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func IsChineseHMPassport(hmPassport string) bool
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/validator"
|
||||
)
|
||||
|
||||
func main() {
|
||||
result1 := validator.IsChineseHMPassport("C12345678")
|
||||
result2 := validator.IsChineseHMPassport("C00000000")
|
||||
result3 := validator.IsChineseHMPassport("M12345678")
|
||||
result4 := validator.IsChineseHMPassport("c12345678")
|
||||
result5 := validator.IsChineseHMPassport("C1234567")
|
||||
|
||||
fmt.Println(result1)
|
||||
fmt.Println(result2)
|
||||
fmt.Println(result3)
|
||||
fmt.Println(result4)
|
||||
fmt.Println(result5)
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// true
|
||||
// true
|
||||
// false
|
||||
// false
|
||||
}
|
||||
```
|
||||
@@ -46,6 +46,7 @@ outline: deep
|
||||
<div class="package-cell">cryptor</div>
|
||||
<div class="package-cell">datastructure</div>
|
||||
<div class="package-cell">datetime</div>
|
||||
<div class="package-cell">enum</div>
|
||||
<div class="package-cell">eventbus</div>
|
||||
<div class="package-cell">fileutil</div>
|
||||
<div class="package-cell">formatter</div>
|
||||
|
||||
850
docs/en/api/packages/enum.md
Normal file
850
docs/en/api/packages/enum.md
Normal file
@@ -0,0 +1,850 @@
|
||||
# Enum
|
||||
|
||||
Package enum provides a simple enum implementation.
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## Source:
|
||||
|
||||
- [https://github.com/duke-git/lancet/blob/main/enum/enum.go](https://github.com/duke-git/lancet/blob/main/enum/enum.go)
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## Usage:
|
||||
|
||||
```go
|
||||
import (
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
```
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## Index
|
||||
|
||||
- [NewItem](#NewItem)
|
||||
- [NewItemsFromPairs](#NewItemsFromPairs)
|
||||
- [Value](#Value)
|
||||
- [Name](#Name)
|
||||
- [Valid](#Valid)
|
||||
- [MarshalJSON](#MarshalJSON)
|
||||
- [NewRegistry](#NewRegistry)
|
||||
- [Add](#Add)
|
||||
- [Remove](#Remove)
|
||||
- [Update](#Update)
|
||||
- [GetByValue](#GetByValue)
|
||||
- [GetByName](#GetByName)
|
||||
- [Items](#Items)
|
||||
- [Contains](#Contains)
|
||||
- [Size](#Size)
|
||||
- [Range](#Range)
|
||||
- [SortedItems](#SortedItems)
|
||||
- [Filter](#Filter)
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## Documentation
|
||||
|
||||
### <span id="NewItem">NewItem</span>
|
||||
|
||||
<p>Creates a new enum item.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func NewItem[T comparable](value T, name string) *Item[T]
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
item1 := enum.NewItem(Active, "Active")
|
||||
item2 := enum.NewItem(Inactive, "Inactive")
|
||||
|
||||
fmt.Println(item1.Name(), item1.Value())
|
||||
fmt.Println(item2.Name(), item2.Value())
|
||||
|
||||
// Output:
|
||||
// Active 1
|
||||
// Inactive 2
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="NewItemsFromPairs">NewItemsFromPairs</span>
|
||||
|
||||
<p>Creates enum items from a slice of Pair structs.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func NewItemsFromPairs[T comparable](pairs ...Pair[T]) []*Item[T]
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
items := enum.NewItemsFromPairs(
|
||||
enum.Pair[Status]{Value: Active, Name: "Active"},
|
||||
enum.Pair[Status]{Value: Inactive, Name: "Inactive"},
|
||||
)
|
||||
|
||||
fmt.Println(items[0].Name(), items[0].Value())
|
||||
fmt.Println(items[1].Name(), items[1].Value())
|
||||
|
||||
// Output:
|
||||
// Active 1
|
||||
// Inactive 2
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Value">Value</span>
|
||||
|
||||
<p>Returns the value of the enum item.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func (it *Item[T]) Value() T
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
items := enum.NewItemsFromPairs(
|
||||
enum.Pair[Status]{Value: Active, Name: "Active"},
|
||||
enum.Pair[Status]{Value: Inactive, Name: "Inactive"},
|
||||
)
|
||||
|
||||
fmt.Println(items[0].Name(), items[0].Value())
|
||||
fmt.Println(items[1].Name(), items[1].Value())
|
||||
|
||||
// Output:
|
||||
// Active 1
|
||||
// Inactive 2
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Name">Name</span>
|
||||
|
||||
<p>Returns the name of the enum item.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func (it *Item[T]) Name() string
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
items := enum.NewItemsFromPairs(
|
||||
enum.Pair[Status]{Value: Active, Name: "Active"},
|
||||
enum.Pair[Status]{Value: Inactive, Name: "Inactive"},
|
||||
)
|
||||
|
||||
fmt.Println(items[0].Name(), items[0].Value())
|
||||
fmt.Println(items[1].Name(), items[1].Value())
|
||||
|
||||
// Output:
|
||||
// Active 1
|
||||
// Inactive 2
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Valid">Valid</span>
|
||||
|
||||
<p>Checks if the enum item is valid. If a custom check function is provided, it will be used to validate the value.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func (it *Item[T]) Valid(checker ...func(T) bool) bool
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
item := enum.NewItem(Active, "Active")
|
||||
fmt.Println(item.Valid())
|
||||
|
||||
invalidItem := enum.NewItem(Unknown, "")
|
||||
fmt.Println(invalidItem.Valid())
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="MarshalJSON">MarshalJSON</span>
|
||||
|
||||
<p>Implementation of json.Marshaler interface.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func (it *Item[T]) MarshalJSON() ([]byte, error)
|
||||
func (it *Item[T]) UnmarshalJSON(data []byte) error
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
item := enum.NewItem(Active, "Active")
|
||||
data, _ := item.MarshalJSON()
|
||||
fmt.Println(string(data))
|
||||
|
||||
var unmarshaledItem Item[Status]
|
||||
_ = unmarshaledItem.UnmarshalJSON(data)
|
||||
fmt.Println(unmarshaledItem.Name(), unmarshaledItem.Value())
|
||||
|
||||
// Output:
|
||||
// {"name":"Active","value":1}
|
||||
// Active 1
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="NewRegistry">NewRegistry</span>
|
||||
|
||||
<p>Creates a new enum registry.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func NewRegistry[T comparable](items ...*Item[T]) *Registry[T]
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
registry := enum.NewRegistry[Status]()
|
||||
item1 := enum.NewItem(Active, "Active")
|
||||
item2 := enum.NewItem(Inactive, "Inactive")
|
||||
|
||||
registry.Add(item1, item2)
|
||||
|
||||
if item, found := registry.GetByValue(Active); found {
|
||||
fmt.Println("Found by value:", item.Name())
|
||||
}
|
||||
|
||||
if item, found := registry.GetByName("Inactive"); found {
|
||||
fmt.Println("Found by name:", item.Value())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Found by value: Active
|
||||
// Found by name: 2
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Add">Add</span>
|
||||
|
||||
<p>Adds enum items to the registry.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func (r *Registry[T]) Add(items ...*Item[T])
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
registry := enum.NewRegistry[Status]()
|
||||
item1 := enum.NewItem(Active, "Active")
|
||||
item2 := enum.NewItem(Inactive, "Inactive")
|
||||
|
||||
registry.Add(item1, item2)
|
||||
|
||||
if item, found := registry.GetByValue(Active); found {
|
||||
fmt.Println("Found by value:", item.Name())
|
||||
}
|
||||
|
||||
if item, found := registry.GetByName("Inactive"); found {
|
||||
fmt.Println("Found by name:", item.Value())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Found by value: Active
|
||||
// Found by name: 2
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Remove">Remove</span>
|
||||
|
||||
<p>Removes an enum item from the registry by its value.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func (r *Registry[T]) Remove(value T) bool
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
registry := enum.NewRegistry[Status]()
|
||||
item1 := enum.NewItem(Active, "Active")
|
||||
|
||||
registry.Add(item1)
|
||||
fmt.Println("Size before removal:", registry.Size())
|
||||
|
||||
removed := registry.Remove(Active)
|
||||
fmt.Println("Removed:", removed)
|
||||
fmt.Println("Size after removal:", registry.Size())
|
||||
|
||||
// Output:
|
||||
// Size before removal: 1
|
||||
// Removed: true
|
||||
// Size after removal: 0
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Update">Update</span>
|
||||
|
||||
<p>Updates the name of an enum item in the registry by its value.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func (r *Registry[T]) Update(value T, newName string) bool
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
registry := enum.NewRegistry[Status]()
|
||||
item1 := enum.NewItem(Active, "Active")
|
||||
|
||||
registry.Add(item1)
|
||||
updated := registry.Update(Active, "Activated")
|
||||
fmt.Println("Updated:", updated)
|
||||
|
||||
if item, found := registry.GetByValue(Active); found {
|
||||
fmt.Println("New name:", item.Name())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Updated: true
|
||||
// New name: Activated
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="GetByValue">GetByValue</span>
|
||||
|
||||
<p>Retrieves an enum item by its value.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func (r *Registry[T]) GetByValue(value T) (*Item[T], bool)
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
registry := enum.NewRegistry[Status]()
|
||||
item1 := enum.NewItem(Active, "Active")
|
||||
|
||||
registry.Add(item1, item2)
|
||||
|
||||
if item, found := registry.GetByValue(Active); found {
|
||||
fmt.Println("Found name by value:", item.Name())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Found name by value: Active
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="GetByName">GetByName</span>
|
||||
|
||||
<p>Retrieves an enum item by its name.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func (r *Registry[T]) GetByName(name string) (*Item[T], bool)
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
registry := enum.NewRegistry[Status]()
|
||||
item := enum.NewItem(Active, "Active")
|
||||
|
||||
registry.Add(item)
|
||||
|
||||
if item, found := registry.GetByName("Active"); found {
|
||||
fmt.Println("Found value by name:", item.Value())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Found value by name: 1
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Items">Items</span>
|
||||
|
||||
<p>Returns a slice of all enum items in the registry.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func (r *Registry[T]) Items() []*Item[T]
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
registry := enum.NewRegistry[Status]()
|
||||
item1 := enum.NewItem(Active, "Active")
|
||||
item2 := enum.NewItem(Inactive, "Inactive")
|
||||
|
||||
registry.Add(item1, item2)
|
||||
|
||||
for _, item := range registry.Items() {
|
||||
fmt.Println(item.Name(), item.Value())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Active 1
|
||||
// Inactive 2
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Contains">Contains</span>
|
||||
|
||||
<p>Checks if an enum item with the given value exists in the registry.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func (r *Registry[T]) Contains(value T) bool
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
registry := enum.NewRegistry[Status]()
|
||||
item := enum.NewItem(Active, "Active")
|
||||
registry.Add(item)
|
||||
|
||||
fmt.Println(registry.Contains(Active))
|
||||
fmt.Println(registry.Contains(Inactive))
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Size">Size</span>
|
||||
|
||||
<p>Returns the number of enum items in the registry.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func (r *Registry[T]) Size() int
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
registry := enum.NewRegistry[Status]()
|
||||
fmt.Println("Initial size:", registry.Size())
|
||||
|
||||
item1 := enum.NewItem(Active, "Active")
|
||||
item2 := enum.NewItem(Inactive, "Inactive")
|
||||
registry.Add(item1, item2)
|
||||
|
||||
fmt.Println("Size after adding items:", registry.Size())
|
||||
|
||||
registry.Remove(Active)
|
||||
fmt.Println("Size after removing an item:", registry.Size())
|
||||
|
||||
// Output:
|
||||
// Initial size: 0
|
||||
// Size after adding items: 2
|
||||
// Size after removing an item: 1
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Range">Range</span>
|
||||
|
||||
<p>Iterates over all enum items in the registry and applies the given function.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func (r *Registry[T]) Range(fn func(*Item[T]) bool)
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
registry := enum.NewRegistry[Status]()
|
||||
item1 := enum.NewItem(Active, "Active")
|
||||
item2 := enum.NewItem(Inactive, "Inactive")
|
||||
registry.Add(item1, item2)
|
||||
|
||||
registry.Range(func(item *Item[Status]) bool {
|
||||
fmt.Println(item.Name(), item.Value())
|
||||
return true // continue iteration
|
||||
})
|
||||
|
||||
// Output:
|
||||
// Active 1
|
||||
// Inactive 2
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="SortedItems">SortedItems</span>
|
||||
|
||||
<p>Returns a slice of all enum items sorted by the given less function.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func (r *Registry[T]) SortedItems(less func(*Item[T], *Item[T]) bool) []*Item[T]
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
registry := enum.NewRegistry[Status]()
|
||||
item1 := enum.NewItem(Inactive, "Inactive")
|
||||
item2 := enum.NewItem(Active, "Active")
|
||||
registry.Add(item1, item2)
|
||||
|
||||
for _, item := range registry.SortedItems(func(i1, i2 *Item[Status]) bool {
|
||||
return i1.value < i2.value
|
||||
}) {
|
||||
fmt.Println(item.Name(), item.Value())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Active 1
|
||||
// Inactive 2
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Filter">Filter</span>
|
||||
|
||||
<p>Returns a slice of enum items that satisfy the given predicate function.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func (r *Registry[T]) Filter(predicate func(*Item[T]) bool) []*Item[T]
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/enum"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func main() {
|
||||
registry := enum.NewRegistry[Status]()
|
||||
item1 := enum.NewItem(Active, "Active")
|
||||
item2 := enum.NewItem(Inactive, "Inactive")
|
||||
registry.Add(item1, item2)
|
||||
|
||||
activeItems := registry.Filter(func(item *Item[Status]) bool {
|
||||
return item.Value() == Active
|
||||
})
|
||||
|
||||
for _, item := range activeItems {
|
||||
fmt.Println(item.Name(), item.Value())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Active 1
|
||||
}
|
||||
```
|
||||
@@ -620,7 +620,7 @@ func main() {
|
||||
|
||||
### <span id="Nand">Nand</span>
|
||||
|
||||
<p>Returns a composed predicate that represents the logical NAND of a list of predicates. It evaluates to true only if all predicates evaluate to false for the given value.</p>
|
||||
<p>Returns a composed predicate that represents the logical NAND of a list of predicates. It evaluates to false only if all predicates evaluate to true for the given value.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
@@ -650,7 +650,7 @@ func main() {
|
||||
|
||||
// Output:
|
||||
// false
|
||||
// false
|
||||
// true
|
||||
// true
|
||||
}
|
||||
```
|
||||
|
||||
@@ -79,6 +79,8 @@ import (
|
||||
- [SortByKey](#SortByKey)
|
||||
- [GetOrDefault](#GetOrDefault)
|
||||
- [FindValuesBy](#FindValuesBy)
|
||||
- [ToMarkdownTable](#ToMarkdownTable)
|
||||
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
@@ -2363,3 +2365,69 @@ func main() {
|
||||
// [b d]
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="ToMarkdownTable">ToMarkdownTable</span>
|
||||
|
||||
<p>Convert a map slice data to a Markdown table string. It supports custom header display names and column display order.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
编辑
|
||||
func ToMarkdownTable(data []map[string]interface{}, headerMap map[string]string, columnOrder []string) string
|
||||
```
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/maputil"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// basic usage: automatically extract column names from data as table headers
|
||||
|
||||
data := []map[string]interface{}{
|
||||
{"name": "Alice", "age": 25, "salary": 50000},
|
||||
{"name": "Bob", "age": 30, "salary": 60000},
|
||||
}
|
||||
|
||||
result := maputil.ToMarkdownTable(data, nil, nil)
|
||||
fmt.Println(result)
|
||||
|
||||
// output:
|
||||
// |name|age|salary|
|
||||
// |---|---|---|
|
||||
// |Alice|25|50000|
|
||||
// |Bob|30|60000|
|
||||
|
||||
// custom header name
|
||||
headerMap := map[string]string{
|
||||
"name": "n",
|
||||
"age": "a",
|
||||
"salary": "s",
|
||||
}
|
||||
result = maputil.ToMarkdownTable(data, headerMap, nil)
|
||||
fmt.Println(result)
|
||||
|
||||
// ouput:
|
||||
// |m|a|s|
|
||||
// |---|---|---|
|
||||
// |Alice|25|50000|
|
||||
// |Bob|30|60000|
|
||||
|
||||
// custom column display order
|
||||
columnOrder := []string{"salary", "name"}
|
||||
result = maputil.ToMarkdownTable(data, nil, columnOrder)
|
||||
fmt.Println(result)
|
||||
|
||||
// 输出:
|
||||
// |salary|name|
|
||||
// |---|---|
|
||||
// |50000|Alice|
|
||||
// |60000|Bob|
|
||||
}
|
||||
```
|
||||
@@ -27,6 +27,7 @@ import (
|
||||
- [Contain](#Contain)
|
||||
- [ContainBy](#ContainBy)
|
||||
- [ContainSubSlice](#ContainSubSlice)
|
||||
- [ContainAny](#ContainAny)
|
||||
- [Chunk](#Chunk)
|
||||
- [Compact](#Compact)
|
||||
- [Concat](#Concat)
|
||||
@@ -256,6 +257,43 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="ContainAny">ContainAny</span>
|
||||
|
||||
<p>Check if the slice contains any element from the targets slice.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func ContainAny[T comparable](slice []T, targets []T) bool
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/4xoxhc9XSSw)</span></b>
|
||||
|
||||
```go
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/slice"
|
||||
)
|
||||
|
||||
func main() {
|
||||
result1 := slice.ContainAny([]string{"a", "b", "c"}, []string{"a"})
|
||||
result2 := slice.ContainAny([]string{"a", "b", "c"}, []string{"d", "e"})
|
||||
result3 := slice.ContainAny([]string{"a", "b", "c"}, []string{"d", "a"})
|
||||
result4 := slice.ContainAny([]string{"a", "b", "c"}, []string{})
|
||||
|
||||
fmt.Println(result1)
|
||||
fmt.Println(result2)
|
||||
fmt.Println(result3)
|
||||
fmt.Println(result4)
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
// true
|
||||
// false
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Chunk">Chunk</span>
|
||||
|
||||
<p>Creates an slice of elements split into groups the length of `size`.</p>
|
||||
|
||||
@@ -31,6 +31,7 @@ import (
|
||||
- [IsStruct](#IsStruct)
|
||||
- [Tag](#Tag)
|
||||
- [Name](#Name)
|
||||
- [TypeName](#TypeName)
|
||||
- [Value](#Value)
|
||||
- [Kind](#Kind)
|
||||
- [IsEmbedded](#IsEmbedded)
|
||||
@@ -53,12 +54,13 @@ import (
|
||||
func New(value any, tagName ...string) *Struct
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/O29l8kk-Z17)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/structs"
|
||||
)
|
||||
|
||||
@@ -68,7 +70,11 @@ func main() {
|
||||
}
|
||||
p1 := &People{Name: "11"}
|
||||
s := structs.New(p1)
|
||||
// to do something
|
||||
|
||||
fmt.Println(s.ToMap())
|
||||
|
||||
// Output:
|
||||
// map[name:11] <nil>
|
||||
}
|
||||
```
|
||||
|
||||
@@ -88,7 +94,7 @@ func (s *Struct) ToMap() (map[string]any, error)
|
||||
func ToMap(v any) (map[string]any, error)
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/qQbLySBgerZ)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -130,7 +136,7 @@ func main() {
|
||||
func (s *Struct) Fields() []*Field
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/w3Kk_CyVY7D)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -162,10 +168,10 @@ func main() {
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func (s *Struct) Field(name string) *Field
|
||||
func (s *Struct) Field(name string) (*Field, bool)
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/KocZMSYarza)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -181,12 +187,14 @@ func main() {
|
||||
}
|
||||
p1 := &People{Name: "11"}
|
||||
s := structs.New(p1)
|
||||
f := s.Field("Name")
|
||||
f, found := s.Field("Name")
|
||||
|
||||
fmt.Println(f.Value())
|
||||
fmt.Println(found)
|
||||
|
||||
// Output:
|
||||
// 11
|
||||
// true
|
||||
}
|
||||
```
|
||||
|
||||
@@ -200,7 +208,7 @@ func main() {
|
||||
func (s *Struct) IsStruct() bool
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/bU2FSdkbK1C)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -234,7 +242,7 @@ func main() {
|
||||
func (f *Field) Tag() *Tag
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/DVrx5HvvUJr)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -271,7 +279,7 @@ func main() {
|
||||
func (f *Field) Value() any
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/qufYEU2o4Oi)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -307,7 +315,7 @@ func main() {
|
||||
func (f *Field) IsEmbedded() bool
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/wV2PrbYm3Ec)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -352,7 +360,7 @@ func main() {
|
||||
func (f *Field) IsExported() bool
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/csK4AXYaNbJ)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -391,7 +399,7 @@ func main() {
|
||||
func (f *Field) IsZero() bool
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/RzqpGISf87r)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -430,7 +438,7 @@ func main() {
|
||||
func (f *Field) Name() string
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/zfIGlqsatee)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -469,7 +477,7 @@ func main() {
|
||||
func (f *Field) Kind() reflect.Kind
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/wg4NlcUNG5o)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -498,6 +506,42 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="TypeName">TypeName</span>
|
||||
|
||||
<p>Return struct type name.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func (s *Struct) TypeName() string
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/structs"
|
||||
)
|
||||
|
||||
func main() {
|
||||
type Parent struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
|
||||
p := &Parent{Age: 11}
|
||||
s := structs.New(p1)
|
||||
|
||||
fmt.Println(s.TypeName())
|
||||
|
||||
// Output:
|
||||
// Parent
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="IsSlice">IsSlice</span>
|
||||
|
||||
<p>Check if the field is a slice</p>
|
||||
@@ -508,7 +552,7 @@ func main() {
|
||||
func (f *Field) IsSlice() bool
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/MKz4CgBIUrU)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -545,7 +589,7 @@ func main() {
|
||||
func (f *Field) IsTargetType(targetType reflect.Kind) bool
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/Ig75P-agN39)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
@@ -65,6 +65,8 @@ import (
|
||||
- [IsAmericanExpress](#IsAmericanExpress)
|
||||
- [IsUnionPay](#IsUnionPay)
|
||||
- [IsChinaUnionPay](#IsChinaUnionPay)
|
||||
- [IsPassport](#IsPassport)
|
||||
- [IsChineseHMPassport](#IsChineseHMPassport)
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
@@ -1569,3 +1571,80 @@ func main() {
|
||||
// false
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="IsPassport">IsPassport</span>
|
||||
|
||||
<p>Passport validation(using regex).</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func IsPassport(passport, country string) bool
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/validator"
|
||||
)
|
||||
|
||||
func main() {
|
||||
result1 := validator.IsPassport("P123456789", "CN")
|
||||
result2 := validator.IsPassport("123456789", "US")
|
||||
result3 := validator.IsPassport("AB1234567", "RU")
|
||||
result4 := validator.IsPassport("123456789", "CN")
|
||||
|
||||
fmt.Println(result1)
|
||||
fmt.Println(result2)
|
||||
fmt.Println(result3)
|
||||
fmt.Println(result4)
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// true
|
||||
// true
|
||||
// false
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="IsChineseHMPassport">IsChineseHMPassport</span>
|
||||
|
||||
<p>Mainland travel permit for Hong Kong, Macao validation (using regex). </p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func IsChineseHMPassport(hmPassport string) bool
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
```go
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/validator"
|
||||
)
|
||||
|
||||
func main() {
|
||||
result1 := validator.IsChineseHMPassport("C12345678")
|
||||
result2 := validator.IsChineseHMPassport("C00000000")
|
||||
result3 := validator.IsChineseHMPassport("M12345678")
|
||||
result4 := validator.IsChineseHMPassport("c12345678")
|
||||
result5 := validator.IsChineseHMPassport("C1234567")
|
||||
|
||||
fmt.Println(result1)
|
||||
fmt.Println(result2)
|
||||
fmt.Println(result3)
|
||||
fmt.Println(result4)
|
||||
fmt.Println(result5)
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// true
|
||||
// true
|
||||
// false
|
||||
// false
|
||||
}
|
||||
```
|
||||
526
docs/package-lock.json
generated
526
docs/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
298
enum/enum.go
Normal file
298
enum/enum.go
Normal file
@@ -0,0 +1,298 @@
|
||||
// Copyright 2025 dudaodong@gmail.com. All rights resulterved.
|
||||
// Use of this source code is governed by MIT license
|
||||
|
||||
// Package enum provides a simple enum implementation.
|
||||
package enum
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sort"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Enum defines a common enum interface.
|
||||
type Enum[T comparable] interface {
|
||||
Value() T
|
||||
String() string
|
||||
Name() string
|
||||
Valid(checker ...func(T) bool) bool
|
||||
}
|
||||
|
||||
// Item defines a common enum item struct implement Enum interface.
|
||||
type Item[T comparable] struct {
|
||||
value T
|
||||
name string
|
||||
}
|
||||
|
||||
// NewItem creates a new enum item.
|
||||
func NewItem[T comparable](value T, name string) *Item[T] {
|
||||
return &Item[T]{value: value, name: name}
|
||||
}
|
||||
|
||||
// Pair represents a value-name pair for creating enum items
|
||||
type Pair[T comparable] struct {
|
||||
Value T
|
||||
Name string
|
||||
}
|
||||
|
||||
// NewItemsFromPairs creates enum items from a slice of Pair structs.
|
||||
func NewItemsFromPairs[T comparable](pairs ...Pair[T]) []*Item[T] {
|
||||
if len(pairs) == 0 {
|
||||
return []*Item[T]{}
|
||||
}
|
||||
|
||||
items := make([]*Item[T], 0, len(pairs))
|
||||
for _, pair := range pairs {
|
||||
items = append(items, &Item[T]{value: pair.Value, name: pair.Name})
|
||||
}
|
||||
|
||||
return items
|
||||
}
|
||||
|
||||
// Value returns the value of the enum item.
|
||||
func (it *Item[T]) Value() T {
|
||||
return it.value
|
||||
}
|
||||
|
||||
// Name returns the name of the enum item.
|
||||
func (it *Item[T]) Name() string {
|
||||
return it.name
|
||||
}
|
||||
|
||||
// String returns the string representation of the enum item.
|
||||
func (it *Item[T]) String() string {
|
||||
return it.name
|
||||
}
|
||||
|
||||
// Valid checks if the enum item is valid. If a custom check function is provided, it will be used to validate the value.
|
||||
func (it *Item[T]) Valid(checker ...func(T) bool) bool {
|
||||
if len(checker) > 0 {
|
||||
return checker[0](it.value) && it.name != ""
|
||||
}
|
||||
var zero T
|
||||
return it.value != zero && it.name != ""
|
||||
}
|
||||
|
||||
// MarshalJSON implements the json.Marshaler interface.
|
||||
func (it *Item[T]) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(map[string]any{
|
||||
"value": it.value,
|
||||
"name": it.name,
|
||||
})
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements the json.Unmarshaler interface.
|
||||
func (it *Item[T]) UnmarshalJSON(data []byte) error {
|
||||
type alias struct {
|
||||
Value any `json:"value"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
var temp alias
|
||||
if err := json.Unmarshal(data, &temp); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var v T
|
||||
rv := reflect.TypeOf(v)
|
||||
switch rv.Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
val, ok := temp.Value.(float64)
|
||||
if !ok {
|
||||
return fmt.Errorf("invalid type for value, want int family")
|
||||
}
|
||||
converted := reflect.ValueOf(int64(val)).Convert(rv)
|
||||
it.value = converted.Interface().(T)
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
val, ok := temp.Value.(float64)
|
||||
if !ok {
|
||||
return fmt.Errorf("invalid type for value, want uint family")
|
||||
}
|
||||
converted := reflect.ValueOf(uint64(val)).Convert(rv)
|
||||
it.value = converted.Interface().(T)
|
||||
case reflect.Float32, reflect.Float64:
|
||||
val, ok := temp.Value.(float64)
|
||||
if !ok {
|
||||
return fmt.Errorf("invalid type for value, want float family")
|
||||
}
|
||||
converted := reflect.ValueOf(val).Convert(rv)
|
||||
it.value = converted.Interface().(T)
|
||||
case reflect.String:
|
||||
val, ok := temp.Value.(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("invalid type for value, want string")
|
||||
}
|
||||
it.value = any(val).(T)
|
||||
case reflect.Bool:
|
||||
val, ok := temp.Value.(bool)
|
||||
if !ok {
|
||||
return fmt.Errorf("invalid type for value, want bool")
|
||||
}
|
||||
it.value = any(val).(T)
|
||||
default:
|
||||
val, ok := temp.Value.(float64)
|
||||
if ok {
|
||||
converted := reflect.ValueOf(int64(val)).Convert(rv)
|
||||
it.value = converted.Interface().(T)
|
||||
} else {
|
||||
val2, ok2 := temp.Value.(T)
|
||||
if !ok2 {
|
||||
return fmt.Errorf("invalid type for value")
|
||||
}
|
||||
it.value = val2
|
||||
}
|
||||
}
|
||||
it.name = temp.Name
|
||||
return nil
|
||||
}
|
||||
|
||||
// Registry defines a common enum registry struct.
|
||||
type Registry[T comparable] struct {
|
||||
mu sync.RWMutex
|
||||
values map[T]*Item[T]
|
||||
names map[string]*Item[T]
|
||||
items []*Item[T]
|
||||
}
|
||||
|
||||
// NewRegistry creates a new enum registry.
|
||||
func NewRegistry[T comparable](items ...*Item[T]) *Registry[T] {
|
||||
r := &Registry[T]{
|
||||
values: make(map[T]*Item[T]),
|
||||
names: make(map[string]*Item[T]),
|
||||
items: make([]*Item[T], 0, len(items)),
|
||||
}
|
||||
|
||||
r.Add(items...)
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
// Add adds enum items to the registry.
|
||||
func (r *Registry[T]) Add(items ...*Item[T]) {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
|
||||
for _, item := range items {
|
||||
if _, exists := r.values[item.value]; exists {
|
||||
continue
|
||||
}
|
||||
if _, exists := r.names[item.name]; exists {
|
||||
continue
|
||||
}
|
||||
|
||||
r.values[item.value] = item
|
||||
r.names[item.name] = item
|
||||
r.items = append(r.items, item)
|
||||
}
|
||||
}
|
||||
|
||||
// Remove removes an enum item from the registry by its value.
|
||||
func (r *Registry[T]) Remove(value T) bool {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
item, ok := r.values[value]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
delete(r.values, value)
|
||||
delete(r.names, item.name)
|
||||
for i, it := range r.items {
|
||||
if it.value == value {
|
||||
r.items = append(r.items[:i], r.items[i+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Update updates the name of an enum item in the registry by its value.
|
||||
func (r *Registry[T]) Update(value T, newName string) bool {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
item, ok := r.values[value]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
delete(r.names, item.name)
|
||||
item.name = newName
|
||||
r.names[newName] = item
|
||||
return true
|
||||
}
|
||||
|
||||
// GetByValue retrieves an enum item by its value.
|
||||
func (r *Registry[T]) GetByValue(value T) (*Item[T], bool) {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
|
||||
item, ok := r.values[value]
|
||||
return item, ok
|
||||
}
|
||||
|
||||
// GetByName retrieves an enum item by its name.
|
||||
func (r *Registry[T]) GetByName(name string) (*Item[T], bool) {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
|
||||
item, ok := r.names[name]
|
||||
return item, ok
|
||||
}
|
||||
|
||||
// Items returns a slice of all enum items in the registry.
|
||||
func (r *Registry[T]) Items() []*Item[T] {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
|
||||
result := make([]*Item[T], len(r.items))
|
||||
copy(result, r.items)
|
||||
return result
|
||||
}
|
||||
|
||||
// Contains checks if an enum item with the given value exists in the registry.
|
||||
func (r *Registry[T]) Contains(value T) bool {
|
||||
_, ok := r.GetByValue(value)
|
||||
return ok
|
||||
}
|
||||
|
||||
// Size returns the number of enum items in the registry.
|
||||
func (r *Registry[T]) Size() int {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
|
||||
return len(r.items)
|
||||
}
|
||||
|
||||
// Range iterates over all enum items in the registry and applies the given function.
|
||||
func (r *Registry[T]) Range(fn func(*Item[T]) bool) {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
|
||||
for _, item := range r.items {
|
||||
if !fn(item) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SortedItems returns a slice of all enum items sorted by the given less function.
|
||||
func (r *Registry[T]) SortedItems(less func(*Item[T], *Item[T]) bool) []*Item[T] {
|
||||
items := r.Items()
|
||||
sort.Slice(items, func(i, j int) bool {
|
||||
return less(items[i], items[j])
|
||||
})
|
||||
return items
|
||||
}
|
||||
|
||||
// Filter returns a slice of enum items that satisfy the given predicate function.
|
||||
func (r *Registry[T]) Filter(predicate func(*Item[T]) bool) []*Item[T] {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
|
||||
var result []*Item[T]
|
||||
for _, item := range r.items {
|
||||
if predicate(item) {
|
||||
result = append(result, item)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
208
enum/enum_example_test.go
Normal file
208
enum/enum_example_test.go
Normal file
@@ -0,0 +1,208 @@
|
||||
package enum
|
||||
|
||||
import "fmt"
|
||||
|
||||
func ExampleNewItem() {
|
||||
item1 := NewItem(Active, "Active")
|
||||
item2 := NewItem(Inactive, "Inactive")
|
||||
|
||||
fmt.Println(item1.Name(), item1.Value())
|
||||
fmt.Println(item2.Name(), item2.Value())
|
||||
|
||||
// Output:
|
||||
// Active 1
|
||||
// Inactive 2
|
||||
}
|
||||
|
||||
func ExampleNewItemsFromPairs() {
|
||||
items := NewItemsFromPairs(
|
||||
Pair[Status]{Value: Active, Name: "Active"},
|
||||
Pair[Status]{Value: Inactive, Name: "Inactive"},
|
||||
)
|
||||
|
||||
fmt.Println(items[0].Name(), items[0].Value())
|
||||
fmt.Println(items[1].Name(), items[1].Value())
|
||||
|
||||
// Output:
|
||||
// Active 1
|
||||
// Inactive 2
|
||||
}
|
||||
|
||||
func ExampleItem_Valid() {
|
||||
item := NewItem(Active, "Active")
|
||||
fmt.Println(item.Valid())
|
||||
|
||||
invalidItem := NewItem(Unknown, "")
|
||||
fmt.Println(invalidItem.Valid())
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
}
|
||||
|
||||
func ExampleItem_MarshalJSON() {
|
||||
item := NewItem(Active, "Active")
|
||||
data, _ := item.MarshalJSON()
|
||||
fmt.Println(string(data))
|
||||
|
||||
var unmarshaledItem Item[Status]
|
||||
_ = unmarshaledItem.UnmarshalJSON(data)
|
||||
fmt.Println(unmarshaledItem.Name(), unmarshaledItem.Value())
|
||||
|
||||
// Output:
|
||||
// {"name":"Active","value":1}
|
||||
// Active 1
|
||||
}
|
||||
|
||||
func ExampleRegistry_Add() {
|
||||
registry := NewRegistry[Status]()
|
||||
item1 := NewItem(Active, "Active")
|
||||
item2 := NewItem(Inactive, "Inactive")
|
||||
|
||||
registry.Add(item1, item2)
|
||||
|
||||
if item, found := registry.GetByValue(Active); found {
|
||||
fmt.Println("Found by value:", item.Name())
|
||||
}
|
||||
|
||||
if item, found := registry.GetByName("Inactive"); found {
|
||||
fmt.Println("Found by name:", item.Value())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Found by value: Active
|
||||
// Found by name: 2
|
||||
}
|
||||
|
||||
func ExampleRegistry_Remove() {
|
||||
registry := NewRegistry[Status]()
|
||||
item1 := NewItem(Active, "Active")
|
||||
|
||||
registry.Add(item1)
|
||||
fmt.Println("Size before removal:", registry.Size())
|
||||
|
||||
removed := registry.Remove(Active)
|
||||
fmt.Println("Removed:", removed)
|
||||
fmt.Println("Size after removal:", registry.Size())
|
||||
|
||||
// Output:
|
||||
// Size before removal: 1
|
||||
// Removed: true
|
||||
// Size after removal: 0
|
||||
}
|
||||
|
||||
func ExampleRegistry_Update() {
|
||||
registry := NewRegistry[Status]()
|
||||
item1 := NewItem(Active, "Active")
|
||||
|
||||
registry.Add(item1)
|
||||
updated := registry.Update(Active, "Activated")
|
||||
fmt.Println("Updated:", updated)
|
||||
|
||||
if item, found := registry.GetByValue(Active); found {
|
||||
fmt.Println("New name:", item.Name())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Updated: true
|
||||
// New name: Activated
|
||||
}
|
||||
|
||||
func ExampleRegistry_Items() {
|
||||
registry := NewRegistry[Status]()
|
||||
item1 := NewItem(Active, "Active")
|
||||
item2 := NewItem(Inactive, "Inactive")
|
||||
|
||||
registry.Add(item1, item2)
|
||||
|
||||
for _, item := range registry.Items() {
|
||||
fmt.Println(item.Name(), item.Value())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Active 1
|
||||
// Inactive 2
|
||||
}
|
||||
|
||||
func ExampleRegistry_Contains() {
|
||||
registry := NewRegistry[Status]()
|
||||
item := NewItem(Active, "Active")
|
||||
registry.Add(item)
|
||||
|
||||
fmt.Println(registry.Contains(Active))
|
||||
fmt.Println(registry.Contains(Inactive))
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
}
|
||||
|
||||
func ExampleRegistry_Size() {
|
||||
registry := NewRegistry[Status]()
|
||||
fmt.Println("Initial size:", registry.Size())
|
||||
|
||||
item1 := NewItem(Active, "Active")
|
||||
item2 := NewItem(Inactive, "Inactive")
|
||||
registry.Add(item1, item2)
|
||||
|
||||
fmt.Println("Size after adding items:", registry.Size())
|
||||
|
||||
registry.Remove(Active)
|
||||
fmt.Println("Size after removing an item:", registry.Size())
|
||||
|
||||
// Output:
|
||||
// Initial size: 0
|
||||
// Size after adding items: 2
|
||||
// Size after removing an item: 1
|
||||
}
|
||||
|
||||
func ExampleRegistry_Range() {
|
||||
registry := NewRegistry[Status]()
|
||||
item1 := NewItem(Active, "Active")
|
||||
item2 := NewItem(Inactive, "Inactive")
|
||||
registry.Add(item1, item2)
|
||||
|
||||
registry.Range(func(item *Item[Status]) bool {
|
||||
fmt.Println(item.Name(), item.Value())
|
||||
return true // continue iteration
|
||||
})
|
||||
|
||||
// Output:
|
||||
// Active 1
|
||||
// Inactive 2
|
||||
}
|
||||
|
||||
func ExampleRegistry_SortedItems() {
|
||||
registry := NewRegistry[Status]()
|
||||
item1 := NewItem(Inactive, "Inactive")
|
||||
item2 := NewItem(Active, "Active")
|
||||
registry.Add(item1, item2)
|
||||
|
||||
for _, item := range registry.SortedItems(func(i1, i2 *Item[Status]) bool {
|
||||
return i1.value < i2.value
|
||||
}) {
|
||||
fmt.Println(item.Name(), item.Value())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Active 1
|
||||
// Inactive 2
|
||||
}
|
||||
|
||||
func ExampleRegistry_Filter() {
|
||||
registry := NewRegistry[Status]()
|
||||
item1 := NewItem(Active, "Active")
|
||||
item2 := NewItem(Inactive, "Inactive")
|
||||
registry.Add(item1, item2)
|
||||
|
||||
activeItems := registry.Filter(func(item *Item[Status]) bool {
|
||||
return item.Value() == Active
|
||||
})
|
||||
|
||||
for _, item := range activeItems {
|
||||
fmt.Println(item.Name(), item.Value())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Active 1
|
||||
}
|
||||
184
enum/enum_test.go
Normal file
184
enum/enum_test.go
Normal file
@@ -0,0 +1,184 @@
|
||||
// Copyright 2025 dudaodong@gmail.com. All rights resulterved.
|
||||
// Use of this source code is governed by MIT license
|
||||
|
||||
package enum
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/duke-git/lancet/v2/internal"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Unknown Status = iota
|
||||
Active
|
||||
Inactive
|
||||
)
|
||||
|
||||
func TestNewItemsFromPairs(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := internal.NewAssert(t, "TestNewItemsFromPairs")
|
||||
|
||||
items := NewItemsFromPairs(
|
||||
Pair[Status]{Value: Active, Name: "Active"},
|
||||
Pair[Status]{Value: Inactive, Name: "Inactive"},
|
||||
)
|
||||
|
||||
assert.Equal(2, len(items))
|
||||
assert.Equal(Active, items[0].Value())
|
||||
assert.Equal("Active", items[0].Name())
|
||||
assert.Equal(Inactive, items[1].Value())
|
||||
assert.Equal("Inactive", items[1].Name())
|
||||
}
|
||||
|
||||
func TestItem_Valid(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := internal.NewAssert(t, "TestItem_Valid")
|
||||
|
||||
item := NewItem(Active, "Active")
|
||||
assert.Equal(true, item.Valid())
|
||||
|
||||
invalidItem := NewItem(Unknown, "")
|
||||
assert.Equal(false, invalidItem.Valid())
|
||||
}
|
||||
|
||||
func TestItem_MarshalJSON(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := internal.NewAssert(t, "TestItem_MarshalJSON")
|
||||
|
||||
item := NewItem(Active, "Active")
|
||||
data, err := item.MarshalJSON()
|
||||
assert.IsNil(err)
|
||||
assert.Equal("{\"name\":\"Active\",\"value\":1}", string(data))
|
||||
|
||||
var unmarshaledItem Item[Status]
|
||||
err = unmarshaledItem.UnmarshalJSON(data)
|
||||
assert.IsNil(err)
|
||||
assert.Equal(item.Value(), unmarshaledItem.Value())
|
||||
assert.Equal(item.Name(), unmarshaledItem.Name())
|
||||
}
|
||||
|
||||
func TestRegistry_AddAndGet(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := internal.NewAssert(t, "TestRegistry_AddAndGet")
|
||||
|
||||
registry := NewRegistry[Status]()
|
||||
item1 := NewItem(Active, "Active")
|
||||
item2 := NewItem(Inactive, "Inactive")
|
||||
|
||||
registry.Add(item1, item2)
|
||||
|
||||
assert.Equal(2, registry.Size())
|
||||
|
||||
item, ok := registry.GetByValue(Active)
|
||||
assert.Equal(true, ok)
|
||||
assert.Equal("Active", item.Name())
|
||||
|
||||
item, ok = registry.GetByName("Inactive")
|
||||
assert.Equal(true, ok)
|
||||
assert.Equal(Inactive, item.Value())
|
||||
}
|
||||
|
||||
func TestRegistry_Remove(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := internal.NewAssert(t, "TestRegistry_Remove")
|
||||
|
||||
registry := NewRegistry[Status]()
|
||||
item1 := NewItem(Active, "Active")
|
||||
item2 := NewItem(Inactive, "Inactive")
|
||||
|
||||
registry.Add(item1, item2)
|
||||
assert.Equal(2, registry.Size())
|
||||
|
||||
removed := registry.Remove(Active)
|
||||
assert.Equal(true, removed)
|
||||
assert.Equal(1, registry.Size())
|
||||
|
||||
_, ok := registry.GetByValue(Active)
|
||||
assert.Equal(false, ok)
|
||||
}
|
||||
|
||||
func TestRegistry_Update(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := internal.NewAssert(t, "TestRegistry_Update")
|
||||
|
||||
registry := NewRegistry[Status]()
|
||||
item1 := NewItem(Active, "Active")
|
||||
registry.Add(item1)
|
||||
|
||||
updated := registry.Update(Active, "Activated")
|
||||
assert.Equal(true, updated)
|
||||
|
||||
item, ok := registry.GetByValue(Active)
|
||||
assert.Equal(true, ok)
|
||||
assert.Equal("Activated", item.Name())
|
||||
}
|
||||
|
||||
func TestRegistry_Contains(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := internal.NewAssert(t, "TestRegistry_Contains")
|
||||
|
||||
registry := NewRegistry[Status]()
|
||||
item1 := NewItem(Active, "Active")
|
||||
registry.Add(item1)
|
||||
|
||||
assert.Equal(true, registry.Contains(Active))
|
||||
assert.Equal(false, registry.Contains(Inactive))
|
||||
}
|
||||
|
||||
func TestRegistry_Range(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := internal.NewAssert(t, "TestRegistry_Range")
|
||||
|
||||
registry := NewRegistry[Status]()
|
||||
item1 := NewItem(Active, "Active")
|
||||
item2 := NewItem(Inactive, "Inactive")
|
||||
registry.Add(item1, item2)
|
||||
|
||||
var values []Status
|
||||
registry.Range(func(item *Item[Status]) bool {
|
||||
values = append(values, item.Value())
|
||||
return true
|
||||
})
|
||||
|
||||
assert.Equal(2, len(values))
|
||||
assert.Equal(Active, values[0])
|
||||
assert.Equal(Inactive, values[1])
|
||||
}
|
||||
|
||||
func TestRegistry_SortedItems(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := internal.NewAssert(t, "TestRegistry_SortedItems")
|
||||
|
||||
registry := NewRegistry[Status]()
|
||||
item1 := NewItem(Inactive, "Inactive")
|
||||
item2 := NewItem(Active, "Active")
|
||||
registry.Add(item1, item2)
|
||||
|
||||
sortedItems := registry.SortedItems(func(i1, i2 *Item[Status]) bool {
|
||||
return i1.value < i2.value
|
||||
})
|
||||
|
||||
assert.Equal(2, len(sortedItems))
|
||||
assert.Equal(Active, sortedItems[0].Value())
|
||||
assert.Equal(Inactive, sortedItems[1].Value())
|
||||
}
|
||||
|
||||
func TestRegistry_Filter(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := internal.NewAssert(t, "TestRegistry_Filter")
|
||||
|
||||
registry := NewRegistry[Status]()
|
||||
item1 := NewItem(Active, "Active")
|
||||
item2 := NewItem(Inactive, "Inactive")
|
||||
registry.Add(item1, item2)
|
||||
|
||||
filteredItems := registry.Filter(func(item *Item[Status]) bool {
|
||||
return item.Value() == Active
|
||||
})
|
||||
|
||||
assert.Equal(1, len(filteredItems))
|
||||
assert.Equal(Active, filteredItems[0].Value())
|
||||
}
|
||||
@@ -21,7 +21,7 @@ type EventBus[T any] struct {
|
||||
// listeners map[string][]*EventListener[T]
|
||||
listeners sync.Map
|
||||
mu sync.RWMutex
|
||||
errorHandler func(err error)
|
||||
errorHandler func(topic string, err error)
|
||||
}
|
||||
|
||||
// EventListener is the struct that holds the listener function and its priority.
|
||||
@@ -117,7 +117,7 @@ func (eb *EventBus[T]) Publish(event Event[T]) {
|
||||
func (eb *EventBus[T]) publishToListener(listener *EventListener[T], event Event[T]) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil && eb.errorHandler != nil {
|
||||
eb.errorHandler(fmt.Errorf("%v", r))
|
||||
eb.errorHandler(event.Topic, fmt.Errorf("%v", r))
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -126,7 +126,7 @@ func (eb *EventBus[T]) publishToListener(listener *EventListener[T], event Event
|
||||
|
||||
// SetErrorHandler sets the error handler function.
|
||||
// Play: https://go.dev/play/p/gmB0gnFe5mc
|
||||
func (eb *EventBus[T]) SetErrorHandler(handler func(err error)) {
|
||||
func (eb *EventBus[T]) SetErrorHandler(handler func(topic string, err error)) {
|
||||
eb.errorHandler = handler
|
||||
}
|
||||
|
||||
|
||||
@@ -189,8 +189,8 @@ func ExampleEventBus_GetListenersCount() {
|
||||
func ExampleEventBus_SetErrorHandler() {
|
||||
eb := NewEventBus[int]()
|
||||
|
||||
eb.SetErrorHandler(func(err error) {
|
||||
fmt.Println(err)
|
||||
eb.SetErrorHandler(func(topic string, err error) {
|
||||
fmt.Println(topic, err)
|
||||
})
|
||||
|
||||
eb.Subscribe("event1", func(eventData int) {
|
||||
@@ -200,7 +200,7 @@ func ExampleEventBus_SetErrorHandler() {
|
||||
eb.Publish(Event[int]{Topic: "event1", Payload: 1})
|
||||
|
||||
// Output:
|
||||
// error
|
||||
// event1 error
|
||||
}
|
||||
|
||||
func ExampleEventBus_GetAllListenersCount() {
|
||||
|
||||
@@ -114,7 +114,8 @@ func TestEventBus_ErrorHandler(t *testing.T) {
|
||||
|
||||
eb := NewEventBus[string]()
|
||||
|
||||
eb.SetErrorHandler(func(err error) {
|
||||
eb.SetErrorHandler(func(topic string, err error) {
|
||||
assert.Equal("event1", topic)
|
||||
assert.Equal("error", err.Error())
|
||||
})
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ func And[T any](predicates ...func(T) bool) func(T) bool {
|
||||
}
|
||||
|
||||
// Nand returns a composed predicate that represents the logical NAND of a list of predicates.
|
||||
// It evaluates to true only if all predicates evaluate to false for the given value.
|
||||
// It evaluates to false only if all predicates evaluate to true for the given value.
|
||||
// Play: https://go.dev/play/p/Rb-FdNGpgSO
|
||||
func Nand[T any](predicates ...func(T) bool) func(T) bool {
|
||||
if len(predicates) < 2 {
|
||||
@@ -26,11 +26,11 @@ func Nand[T any](predicates ...func(T) bool) func(T) bool {
|
||||
}
|
||||
return func(value T) bool {
|
||||
for _, predicate := range predicates {
|
||||
if predicate(value) {
|
||||
return false // Short-circuit on the first true predicate
|
||||
if !predicate(value) {
|
||||
return true // Short-circuit on the first false predicate
|
||||
}
|
||||
}
|
||||
return true // True if all predicates are false
|
||||
return false // False if all predicates are true
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -65,7 +65,7 @@ func ExampleNand() {
|
||||
|
||||
// Output:
|
||||
// false
|
||||
// false
|
||||
// true
|
||||
// true
|
||||
}
|
||||
|
||||
|
||||
@@ -65,7 +65,7 @@ func TestPredicatesNandPure(t *testing.T) {
|
||||
)
|
||||
|
||||
assert.ShouldBeFalse(isNumericAndLength5("12345"))
|
||||
assert.ShouldBeFalse(isNumericAndLength5("1234"))
|
||||
assert.ShouldBeTrue(isNumericAndLength5("1234"))
|
||||
assert.ShouldBeTrue(isNumericAndLength5("abcdef"))
|
||||
}
|
||||
|
||||
|
||||
148
maputil/map.go
148
maputil/map.go
@@ -8,6 +8,7 @@ import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/exp/constraints"
|
||||
@@ -680,3 +681,150 @@ func FindValuesBy[K comparable, V any](m map[K]V, predicate func(key K, value V)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// ToMarkdownTable converts a slice of maps to a Markdown table.
|
||||
// Play: ttps://go.dev/play/p/todo
|
||||
func ToMarkdownTable(data []map[string]interface{}, headerMap map[string]string, columnOrder []string) string {
|
||||
if len(data) == 0 {
|
||||
return "| |\n|---|\n"
|
||||
}
|
||||
|
||||
var headers []string
|
||||
|
||||
// 如果提供了columnOrder,则按指定顺序排列
|
||||
if len(columnOrder) > 0 {
|
||||
headers = make([]string, len(columnOrder))
|
||||
copy(headers, columnOrder)
|
||||
} else {
|
||||
// 否则按自然顺序提取headers
|
||||
seen := make(map[string]bool)
|
||||
for _, row := range data {
|
||||
for k := range row {
|
||||
if !seen[k] {
|
||||
seen[k] = true
|
||||
headers = append(headers, k)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var builder strings.Builder
|
||||
|
||||
// Header row - 使用映射的中文标题
|
||||
builder.WriteString("| ")
|
||||
for i, h := range headers {
|
||||
// 如果有映射则使用中文标题,否则使用原字段名
|
||||
displayHeader := h
|
||||
if headerMap != nil && headerMap[h] != "" {
|
||||
displayHeader = headerMap[h]
|
||||
}
|
||||
builder.WriteString(displayHeader)
|
||||
if i < len(headers)-1 {
|
||||
builder.WriteString(" | ")
|
||||
}
|
||||
}
|
||||
builder.WriteString(" |\n")
|
||||
|
||||
// Separator
|
||||
builder.WriteString("|")
|
||||
for i := range headers {
|
||||
if i > 0 {
|
||||
builder.WriteString("|")
|
||||
}
|
||||
builder.WriteString("---")
|
||||
}
|
||||
builder.WriteString("|\n")
|
||||
|
||||
// Data rows
|
||||
for _, row := range data {
|
||||
builder.WriteString("| ")
|
||||
for i, h := range headers {
|
||||
val, exists := row[h]
|
||||
var cell string
|
||||
if !exists {
|
||||
cell = ""
|
||||
} else {
|
||||
cell = formatValue(val)
|
||||
}
|
||||
builder.WriteString(cell)
|
||||
if i < len(headers)-1 {
|
||||
builder.WriteString(" | ")
|
||||
}
|
||||
}
|
||||
builder.WriteString(" |\n")
|
||||
}
|
||||
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
// formatValue formats any value for display in Markdown table
|
||||
func formatValue(v interface{}) string {
|
||||
switch val := v.(type) {
|
||||
case int:
|
||||
return commaInt64(int64(val))
|
||||
case int8:
|
||||
return commaInt64(int64(val))
|
||||
case int16:
|
||||
return commaInt64(int64(val))
|
||||
case int32:
|
||||
return commaInt64(int64(val))
|
||||
case int64:
|
||||
return commaInt64(val)
|
||||
case uint:
|
||||
return commaUint64(uint64(val))
|
||||
case uint8:
|
||||
return commaUint64(uint64(val))
|
||||
case uint16:
|
||||
return commaUint64(uint64(val))
|
||||
case uint32:
|
||||
return commaUint64(uint64(val))
|
||||
case uint64:
|
||||
return commaUint64(val)
|
||||
case float32:
|
||||
return fmt.Sprintf("%.2f", val)
|
||||
case float64:
|
||||
return fmt.Sprintf("%.2f", val)
|
||||
case string:
|
||||
return val
|
||||
case bool:
|
||||
return fmt.Sprintf("%t", val)
|
||||
case nil:
|
||||
return ""
|
||||
default:
|
||||
return fmt.Sprintf("%v", val)
|
||||
}
|
||||
}
|
||||
|
||||
// commaInt64 adds comma separators to int64
|
||||
func commaInt64(n int64) string {
|
||||
if n == 0 {
|
||||
return "0"
|
||||
}
|
||||
neg := n < 0
|
||||
if neg {
|
||||
n = -n
|
||||
}
|
||||
s := strconv.FormatInt(n, 10)
|
||||
return addCommas(s)
|
||||
}
|
||||
|
||||
// commaUint64 adds comma separators to uint64
|
||||
func commaUint64(n uint64) string {
|
||||
if n == 0 {
|
||||
return "0"
|
||||
}
|
||||
s := strconv.FormatUint(n, 10)
|
||||
return addCommas(s)
|
||||
}
|
||||
|
||||
// addCommas inserts commas every 3 digits
|
||||
func addCommas(s string) string {
|
||||
var result strings.Builder
|
||||
for i, c := range s {
|
||||
if i > 0 && (len(s)-i)%3 == 0 {
|
||||
result.WriteRune(',')
|
||||
}
|
||||
result.WriteRune(c)
|
||||
}
|
||||
return result.String()
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package maputil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/cmplx"
|
||||
"sort"
|
||||
"strconv"
|
||||
@@ -926,3 +927,46 @@ func TestFindValuesBy(t *testing.T) {
|
||||
assert.Equal(tt.expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToMarkdownTable(t *testing.T) {
|
||||
// 测试空数据
|
||||
emptyResult := ToMarkdownTable([]map[string]interface{}{}, nil, nil)
|
||||
expectedEmpty := "| |\n|---|\n"
|
||||
if emptyResult != expectedEmpty {
|
||||
t.Errorf("Expected empty table, got: %s", emptyResult)
|
||||
}
|
||||
|
||||
// 测试基本数据
|
||||
data := []map[string]interface{}{
|
||||
{"name": "Alice", "age": 25, "salary": 50000},
|
||||
{"name": "Bob", "age": 30, "salary": 60000},
|
||||
}
|
||||
result := ToMarkdownTable(data, nil, nil)
|
||||
fmt.Printf("%s", result)
|
||||
// 验证结果包含预期的表头和数据行
|
||||
}
|
||||
|
||||
// 测试自定义列顺序
|
||||
func TestToMarkdownTableWithColumnOrder(t *testing.T) {
|
||||
data := []map[string]interface{}{
|
||||
{"name": "Alice", "age": 25, "salary": 50000},
|
||||
}
|
||||
columnOrder := []string{"salary", "name", "age"}
|
||||
result := ToMarkdownTable(data, nil, columnOrder)
|
||||
fmt.Printf("%s", result)
|
||||
// 验证列顺序是否正确
|
||||
}
|
||||
|
||||
// 测试自定义表头
|
||||
func TestToMarkdownTableWithHeaderMap(t *testing.T) {
|
||||
data := []map[string]interface{}{
|
||||
{"name": "Alice", "age": 25},
|
||||
}
|
||||
headerMap := map[string]string{
|
||||
"name": "姓名",
|
||||
"age": "年龄",
|
||||
}
|
||||
result := ToMarkdownTable(data, headerMap, nil)
|
||||
fmt.Printf("%s", result)
|
||||
// 验证中文表头是否正确显示
|
||||
}
|
||||
|
||||
@@ -238,6 +238,9 @@ func Sum[T constraints.Integer | constraints.Float](numbers ...T) T {
|
||||
// Average return average value of numbers.
|
||||
// Play: https://go.dev/play/p/Vv7LBwER-pz
|
||||
func Average[T constraints.Integer | constraints.Float](numbers ...T) float64 {
|
||||
if len(numbers) == 0 {
|
||||
return 0
|
||||
}
|
||||
var sum float64
|
||||
for _, num := range numbers {
|
||||
sum += float64(num)
|
||||
|
||||
111
slice/slice.go
111
slice/slice.go
@@ -74,6 +74,27 @@ func ContainSubSlice[T comparable](slice, subSlice []T) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// ContainAny check if the slice contains any element from the targets slice.
|
||||
// Play: https://go.dev/play/p/4xoxhc9XSSw
|
||||
func ContainAny[T comparable](slice []T, targets []T) bool {
|
||||
if len(targets) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
sliceMap := make(map[T]struct{}, len(slice))
|
||||
for _, item := range slice {
|
||||
sliceMap[item] = struct{}{}
|
||||
}
|
||||
|
||||
for _, target := range targets {
|
||||
if _, exists := sliceMap[target]; exists {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// Chunk creates a slice of elements split into groups the length of size.
|
||||
// Play: https://go.dev/play/p/b4Pou5j2L_C
|
||||
func Chunk[T any](slice []T, size int) [][]T {
|
||||
@@ -507,19 +528,17 @@ func flattenRecursive(value reflect.Value, result reflect.Value) reflect.Value {
|
||||
}
|
||||
|
||||
// ForEach iterates over elements of slice and invokes function for each element.
|
||||
// Play: https://go.dev/play/p/DrPaa4YsHRF
|
||||
func ForEach[T any](slice []T, iteratee func(index int, item T)) {
|
||||
for i := 0; i < len(slice); i++ {
|
||||
iteratee(i, slice[i])
|
||||
for idx, elem := range slice {
|
||||
iteratee(idx, elem)
|
||||
}
|
||||
}
|
||||
|
||||
// ForEachWithBreak iterates over elements of slice and invokes function for each element,
|
||||
// when iteratee return false, will break the for each loop.
|
||||
// Play: https://go.dev/play/p/qScs39f3D9W
|
||||
// when function return false, will break the for each loop.
|
||||
func ForEachWithBreak[T any](slice []T, iteratee func(index int, item T) bool) {
|
||||
for i := 0; i < len(slice); i++ {
|
||||
if !iteratee(i, slice[i]) {
|
||||
for idx, elem := range slice {
|
||||
if !iteratee(idx, elem) {
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -692,11 +711,12 @@ func IntSlice(slice any) []int {
|
||||
// DeleteAt delete the element of slice at index.
|
||||
// Play: https://go.dev/play/p/800B1dPBYyd
|
||||
func DeleteAt[T any](slice []T, index int) []T {
|
||||
result := append([]T(nil), slice...)
|
||||
|
||||
if index < 0 || index >= len(slice) {
|
||||
return slice[:len(slice)-1]
|
||||
return result[:len(slice)-1]
|
||||
}
|
||||
|
||||
result := append([]T(nil), slice...)
|
||||
copy(result[index:], result[index+1:])
|
||||
|
||||
// Set the last element to zero value, clean up the memory.
|
||||
@@ -736,7 +756,8 @@ func Drop[T any](slice []T, n int) []T {
|
||||
}
|
||||
|
||||
if n <= 0 {
|
||||
return slice
|
||||
result := make([]T, 0, size)
|
||||
return append(result, slice...)
|
||||
}
|
||||
|
||||
result := make([]T, 0, size-n)
|
||||
@@ -754,7 +775,8 @@ func DropRight[T any](slice []T, n int) []T {
|
||||
}
|
||||
|
||||
if n <= 0 {
|
||||
return slice
|
||||
result := make([]T, 0, size)
|
||||
return append(result, slice...)
|
||||
}
|
||||
|
||||
result := make([]T, 0, size-n)
|
||||
@@ -800,7 +822,9 @@ func InsertAt[T any](slice []T, index int, value any) []T {
|
||||
size := len(slice)
|
||||
|
||||
if index < 0 || index > size {
|
||||
return slice
|
||||
result := make([]T, size)
|
||||
copy(result, slice)
|
||||
return result
|
||||
}
|
||||
|
||||
switch v := value.(type) {
|
||||
@@ -817,21 +841,21 @@ func InsertAt[T any](slice []T, index int, value any) []T {
|
||||
copy(result[index+len(v):], slice[index:])
|
||||
return result
|
||||
default:
|
||||
return slice
|
||||
result := make([]T, size)
|
||||
copy(result, slice)
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateAt update the slice element at index.
|
||||
// Play: https://go.dev/play/p/f3mh2KloWVm
|
||||
func UpdateAt[T any](slice []T, index int, value T) []T {
|
||||
if index < 0 || index >= len(slice) {
|
||||
return slice
|
||||
}
|
||||
|
||||
result := make([]T, len(slice))
|
||||
copy(result, slice)
|
||||
|
||||
result[index] = value
|
||||
if index >= 0 && index < len(slice) {
|
||||
result[index] = value
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
@@ -1021,7 +1045,9 @@ func SymmetricDifference[T comparable](slices ...[]T) []T {
|
||||
return []T{}
|
||||
}
|
||||
if len(slices) == 1 {
|
||||
return Unique(slices[0])
|
||||
result := make([]T, len(slices[0]))
|
||||
copy(result, slices[0])
|
||||
return Unique(result)
|
||||
}
|
||||
|
||||
result := make([]T, 0)
|
||||
@@ -1042,6 +1068,7 @@ func SymmetricDifference[T comparable](slices ...[]T) []T {
|
||||
}
|
||||
|
||||
// Reverse return slice of element order is reversed to the given slice.
|
||||
// Reverse modifies the slice in place.
|
||||
// Play: https://go.dev/play/p/8uI8f1lwNrQ
|
||||
func Reverse[T any](slice []T) {
|
||||
for i, j := 0, len(slice)-1; i < j; i, j = i+1, j-1 {
|
||||
@@ -1049,7 +1076,8 @@ func Reverse[T any](slice []T) {
|
||||
}
|
||||
}
|
||||
|
||||
// ReverseCopy return a new slice of element order is reversed to the given slice.
|
||||
// ReverseCopy return a new slice of element where the order is reversed to the given
|
||||
// slice.
|
||||
// Play: https://go.dev/play/p/c9arEaP7Cg-
|
||||
func ReverseCopy[T any](slice []T) []T {
|
||||
result := make([]T, len(slice))
|
||||
@@ -1067,7 +1095,7 @@ func Shuffle[T any](slice []T) []T {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
rand.Shuffle(len(slice), func(i, j int) {
|
||||
slice[i], slice[j] = slice[j], slice[i]
|
||||
swap(slice, i, j)
|
||||
})
|
||||
|
||||
return slice
|
||||
@@ -1240,11 +1268,12 @@ func SortByField[T any](slice []T, field string, sortType ...string) error {
|
||||
// Without creates a slice excluding all given items.
|
||||
// Play: https://go.dev/play/p/bwhEXEypThg
|
||||
func Without[T comparable](slice []T, items ...T) []T {
|
||||
result := make([]T, 0, len(slice))
|
||||
|
||||
if len(items) == 0 || len(slice) == 0 {
|
||||
return slice
|
||||
return append(result, slice...)
|
||||
}
|
||||
|
||||
result := make([]T, 0, len(slice))
|
||||
for _, v := range slice {
|
||||
if !Contain(items, v) {
|
||||
result = append(result, v)
|
||||
@@ -1465,36 +1494,28 @@ func Random[T any](slice []T) (val T, idx int) {
|
||||
return slice[idx], idx
|
||||
}
|
||||
|
||||
// RightPadding adds padding to the right end of a slice.
|
||||
// RightPadding returns a copy of the slice padding the given value to the right end of a slice.
|
||||
// If paddingLength is zero or less, the function returns a copy of the slice.
|
||||
// Play: https://go.dev/play/p/0_2rlLEMBXL
|
||||
func RightPadding[T any](slice []T, paddingValue T, paddingLength int) []T {
|
||||
if paddingLength == 0 {
|
||||
return slice
|
||||
suffix := []T{}
|
||||
if paddingLength > 0 {
|
||||
suffix = repeat([]T{paddingValue}, paddingLength)
|
||||
}
|
||||
for i := 0; i < paddingLength; i++ {
|
||||
slice = append(slice, paddingValue)
|
||||
}
|
||||
return slice
|
||||
padded := concat(slice, suffix)
|
||||
return padded
|
||||
}
|
||||
|
||||
// LeftPadding adds padding to the left begin of a slice.
|
||||
// LeftPadding returns a copy of the slice padding the given value to the left begin of a slice.
|
||||
// If paddingLength is zero or less, the function returns a copy of the slice.
|
||||
// Play: https://go.dev/play/p/jlQVoelLl2k
|
||||
func LeftPadding[T any](slice []T, paddingValue T, paddingLength int) []T {
|
||||
if paddingLength == 0 {
|
||||
return slice
|
||||
prefix := []T{}
|
||||
if paddingLength > 0 {
|
||||
prefix = repeat([]T{paddingValue}, paddingLength)
|
||||
}
|
||||
|
||||
paddedSlice := make([]T, len(slice)+paddingLength)
|
||||
i := 0
|
||||
for ; i < paddingLength; i++ {
|
||||
paddedSlice[i] = paddingValue
|
||||
}
|
||||
for j := 0; j < len(slice); j++ {
|
||||
paddedSlice[i] = slice[j]
|
||||
i++
|
||||
}
|
||||
|
||||
return paddedSlice
|
||||
padded := concat(prefix, slice)
|
||||
return padded
|
||||
}
|
||||
|
||||
// Frequency counts the frequency of each element in the slice.
|
||||
|
||||
@@ -58,6 +58,24 @@ func ExampleContainSubSlice() {
|
||||
// false
|
||||
}
|
||||
|
||||
func ExampleContainAny() {
|
||||
result1 := ContainAny([]string{"a", "b", "c"}, []string{"a"})
|
||||
result2 := ContainAny([]string{"a", "b", "c"}, []string{"d", "e"})
|
||||
result3 := ContainAny([]string{"a", "b", "c"}, []string{"d", "a"})
|
||||
result4 := ContainAny([]string{"a", "b", "c"}, []string{})
|
||||
|
||||
fmt.Println(result1)
|
||||
fmt.Println(result2)
|
||||
fmt.Println(result3)
|
||||
fmt.Println(result4)
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
// true
|
||||
// false
|
||||
}
|
||||
|
||||
func ExampleChunk() {
|
||||
arr := []string{"a", "b", "c", "d", "e"}
|
||||
|
||||
@@ -831,7 +849,7 @@ func ExampleUniqueByComparator() {
|
||||
})
|
||||
|
||||
caseInsensitiveStrings := UniqueByComparator([]string{"apple", "banana", "Apple", "cherry", "Banana", "date"}, func(item string, other string) bool {
|
||||
return strings.ToLower(item) == strings.ToLower(other)
|
||||
return strings.EqualFold(item, other)
|
||||
})
|
||||
|
||||
fmt.Println(uniqueNums)
|
||||
|
||||
@@ -2,6 +2,7 @@ package slice
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/bits"
|
||||
"reflect"
|
||||
|
||||
"golang.org/x/exp/constraints"
|
||||
@@ -96,3 +97,71 @@ func partitionAnySlice[T any](slice []T, lowIndex, highIndex int, less func(a, b
|
||||
func swap[T any](slice []T, i, j int) {
|
||||
slice[i], slice[j] = slice[j], slice[i]
|
||||
}
|
||||
|
||||
// `repeat` returns a new slice that repeats the provided slice the given number of
|
||||
// times. The result has length and capacity (len(x) * count). The result is never nil.
|
||||
// Repeat panics if count is negative or if the result of (len(x) * count) overflows.
|
||||
//
|
||||
// repeat has been provided in the standard lib within the package `slices` under the
|
||||
// name Repeat since GO version 1.21 onwards. As lancet commits to compatibility with GO
|
||||
// 1.18 onwards, we implement the functionality as an internal function.
|
||||
func repeat[S ~[]E, E any](x S, count int) S {
|
||||
if count < 0 {
|
||||
panic("count cannot be negative")
|
||||
}
|
||||
|
||||
const maxInt = ^uint(0) >> 1
|
||||
hi, lo := bits.Mul(uint(len(x)), uint(count))
|
||||
if hi > 0 || lo > maxInt {
|
||||
panic("the result of (len(x) * count) overflows")
|
||||
}
|
||||
|
||||
newslice := make(S, int(lo)) // lo = len(x) * count
|
||||
n := copy(newslice, x)
|
||||
for n < len(newslice) {
|
||||
n += copy(newslice[n:], newslice[:n])
|
||||
}
|
||||
return newslice
|
||||
}
|
||||
|
||||
// concat returns a new slice concatenating the passed in slices.
|
||||
//
|
||||
// concat has been provided in the standard lib within the package `slices` under the
|
||||
// name Concat since GO version 1.21 onwards. As lancet commits to compatibility with GO
|
||||
// 1.18 onwards, we implement the functionality as an internal function.
|
||||
func concat[S ~[]E, E any](slices ...S) S {
|
||||
size := 0
|
||||
for _, s := range slices {
|
||||
size += len(s)
|
||||
if size < 0 {
|
||||
panic("len out of range")
|
||||
}
|
||||
}
|
||||
// Use Grow, not make, to round up to the size class:
|
||||
// the extra space is otherwise unused and helps
|
||||
// callers that append a few elements to the result.
|
||||
newslice := grow[S](nil, size)
|
||||
for _, s := range slices {
|
||||
newslice = append(newslice, s...)
|
||||
}
|
||||
return newslice
|
||||
}
|
||||
|
||||
// grow increases the slice's capacity, if necessary, to guarantee space for
|
||||
// another n elements. After grow(n), at least n elements can be appended
|
||||
// to the slice without another allocation. If n is negative or too large to
|
||||
// allocate the memory, grow panics.
|
||||
//
|
||||
// grow has been provided in the standard lib within the package `slices` under the
|
||||
// name Grow since GO version 1.21 onwards. As lancet commits to compatibility with GO
|
||||
// 1.18 onwards, we implement the functionality as an internal function.
|
||||
func grow[S ~[]E, E any](s S, n int) S {
|
||||
if n < 0 {
|
||||
panic("cannot be negative")
|
||||
}
|
||||
if n -= cap(s) - len(s); n > 0 {
|
||||
// This expression allocates only once.
|
||||
s = append(s[:cap(s)], make([]E, n)...)[:len(s)]
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
@@ -89,6 +89,46 @@ func TestContainSubSlice(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainAny(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert := internal.NewAssert(t, "TestContainAny")
|
||||
|
||||
tests := []struct {
|
||||
slice []string
|
||||
targets []string
|
||||
want bool
|
||||
}{
|
||||
{[]string{"a", "b", "c"}, []string{"a"}, true},
|
||||
{[]string{"a", "b", "c"}, []string{"a", "b"}, true},
|
||||
{[]string{"a", "b", "c"}, []string{"d", "e"}, false},
|
||||
{[]string{"a", "b", "c"}, []string{"d", "a"}, true},
|
||||
{[]string{"a", "b", "c"}, []string{}, false},
|
||||
{[]string{}, []string{"a"}, false},
|
||||
{[]string{}, []string{}, false},
|
||||
{[]string{"a", "b", "c"}, []string{"c", "d", "e"}, true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.want, ContainAny(tt.slice, tt.targets))
|
||||
}
|
||||
|
||||
intTests := []struct {
|
||||
slice []int
|
||||
targets []int
|
||||
want bool
|
||||
}{
|
||||
{[]int{1, 2, 3, 4, 5}, []int{3}, true},
|
||||
{[]int{1, 2, 3, 4, 5}, []int{6, 7}, false},
|
||||
{[]int{1, 2, 3, 4, 5}, []int{5, 6, 7}, true},
|
||||
{[]int{1, 2, 3, 4, 5}, []int{}, false},
|
||||
}
|
||||
|
||||
for _, tt := range intTests {
|
||||
assert.Equal(tt.want, ContainAny(tt.slice, tt.targets))
|
||||
}
|
||||
}
|
||||
|
||||
func TestChunk(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -1008,7 +1048,7 @@ func TestUniqueByComparator(t *testing.T) {
|
||||
t.Run("case-insensitive string comparison", func(t *testing.T) {
|
||||
stringSlice := []string{"apple", "banana", "Apple", "cherry", "Banana", "date"}
|
||||
caseInsensitiveComparator := func(item, other string) bool {
|
||||
return strings.ToLower(item) == strings.ToLower(other)
|
||||
return strings.EqualFold(item, other)
|
||||
}
|
||||
|
||||
result := UniqueByComparator(stringSlice, caseInsensitiveComparator)
|
||||
@@ -1756,6 +1796,20 @@ func TestRightPaddingAndLeftPadding(t *testing.T) {
|
||||
|
||||
padded := LeftPadding(RightPadding(nums, 0, 3), 0, 3)
|
||||
assert.Equal([]int{0, 0, 0, 1, 2, 3, 4, 5, 0, 0, 0}, padded)
|
||||
|
||||
// Test with negative padding length
|
||||
paddedNegative := LeftPadding(RightPadding(nums, 0, -3), 0, -3)
|
||||
assert.Equal([]int{1, 2, 3, 4, 5}, paddedNegative)
|
||||
|
||||
// Test with empty slice
|
||||
empty := []int{}
|
||||
paddedEmpty := LeftPadding(RightPadding(empty, 0, 3), 0, 3)
|
||||
assert.Equal([]int{0, 0, 0, 0, 0, 0}, paddedEmpty)
|
||||
|
||||
// Test with nil
|
||||
nilSlice := []int(nil)
|
||||
paddedNil := LeftPadding(RightPadding(nilSlice, 0, 3), 0, 3)
|
||||
assert.Equal([]int{0, 0, 0, 0, 0, 0}, paddedNil)
|
||||
}
|
||||
|
||||
func TestUniqueByConcurrent(t *testing.T) {
|
||||
|
||||
@@ -26,26 +26,31 @@ func newField(v reflect.Value, f reflect.StructField, tagName string) *Field {
|
||||
}
|
||||
|
||||
// Tag returns the value that the key in the tag string.
|
||||
// Play: https://go.dev/play/p/DVrx5HvvUJr
|
||||
func (f *Field) Tag() *Tag {
|
||||
return f.tag
|
||||
}
|
||||
|
||||
// Value returns the underlying value of the field.
|
||||
// Play: https://go.dev/play/p/qufYEU2o4Oi
|
||||
func (f *Field) Value() any {
|
||||
return f.rvalue.Interface()
|
||||
}
|
||||
|
||||
// IsEmbedded returns true if the given field is an embedded field.
|
||||
// Play: https://go.dev/play/p/wV2PrbYm3Ec
|
||||
func (f *Field) IsEmbedded() bool {
|
||||
return len(f.field.Index) > 1
|
||||
}
|
||||
|
||||
// IsExported returns true if the given field is exported.
|
||||
// Play: https://go.dev/play/p/csK4AXYaNbJ
|
||||
func (f *Field) IsExported() bool {
|
||||
return f.field.IsExported()
|
||||
}
|
||||
|
||||
// IsZero returns true if the given field is zero value.
|
||||
// Play: https://go.dev/play/p/RzqpGISf87r
|
||||
func (f *Field) IsZero() bool {
|
||||
z := reflect.Zero(f.rvalue.Type()).Interface()
|
||||
v := f.Value()
|
||||
@@ -63,22 +68,26 @@ func (f *Field) IsNil() bool {
|
||||
}
|
||||
|
||||
// Name returns the name of the given field
|
||||
// Play: https://go.dev/play/p/zfIGlqsatee
|
||||
func (f *Field) Name() string {
|
||||
return f.field.Name
|
||||
}
|
||||
|
||||
// Kind returns the field's kind
|
||||
// Play: https://go.dev/play/p/wg4NlcUNG5o
|
||||
func (f *Field) Kind() reflect.Kind {
|
||||
return f.rvalue.Kind()
|
||||
}
|
||||
|
||||
// IsSlice check if a struct field type is slice or not
|
||||
// Play: https://go.dev/play/p/MKz4CgBIUrU
|
||||
func (f *Field) IsSlice() bool {
|
||||
k := f.rvalue.Kind()
|
||||
return k == reflect.Slice
|
||||
}
|
||||
|
||||
// IsTargetType check if a struct field type is target type or not
|
||||
// Play: https://go.dev/play/p/Ig75P-agN39
|
||||
func (f *Field) IsTargetType(targetType reflect.Kind) bool {
|
||||
return f.rvalue.Kind() == targetType
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ type Struct struct {
|
||||
}
|
||||
|
||||
// New returns a new *Struct
|
||||
// Play: https://go.dev/play/p/O29l8kk-Z17
|
||||
func New(value any, tagName ...string) *Struct {
|
||||
value = pointer.ExtractPointer(value)
|
||||
v := reflect.ValueOf(value)
|
||||
@@ -60,6 +61,7 @@ func New(value any, tagName ...string) *Struct {
|
||||
// Name string `json:"myName"`
|
||||
//
|
||||
// ToMap convert the exported fields of a struct to map.
|
||||
// Play: https://go.dev/play/p/qQbLySBgerZ
|
||||
func (s *Struct) ToMap() (map[string]any, error) {
|
||||
if !s.IsStruct() {
|
||||
return nil, fmt.Errorf("invalid struct %v", s)
|
||||
@@ -87,6 +89,7 @@ func (s *Struct) ToMap() (map[string]any, error) {
|
||||
}
|
||||
|
||||
// Fields returns all the struct fields within a slice
|
||||
// Play: https://go.dev/play/p/w3Kk_CyVY7D
|
||||
func (s *Struct) Fields() []*Field {
|
||||
fieldNum := s.rvalue.NumField()
|
||||
fields := make([]*Field, 0, fieldNum)
|
||||
@@ -100,6 +103,7 @@ func (s *Struct) Fields() []*Field {
|
||||
}
|
||||
|
||||
// Field returns a Field if the given field name was found
|
||||
// Play: https://go.dev/play/p/KocZMSYarza
|
||||
func (s *Struct) Field(name string) (*Field, bool) {
|
||||
f, ok := s.rtype.FieldByName(name)
|
||||
if !ok {
|
||||
@@ -109,6 +113,7 @@ func (s *Struct) Field(name string) (*Field, bool) {
|
||||
}
|
||||
|
||||
// IsStruct returns true if the given rvalue is a struct
|
||||
// Play: https://go.dev/play/p/bU2FSdkbK1C
|
||||
func (s *Struct) IsStruct() bool {
|
||||
k := s.rvalue.Kind()
|
||||
if k == reflect.Invalid {
|
||||
@@ -119,6 +124,13 @@ func (s *Struct) IsStruct() bool {
|
||||
|
||||
// ToMap convert struct to map, only convert exported struct field
|
||||
// map key is specified same as struct field tag `json` value.
|
||||
// Play: https://go.dev/play/p/qQbLySBgerZ
|
||||
func ToMap(v any) (map[string]any, error) {
|
||||
return New(v).ToMap()
|
||||
}
|
||||
|
||||
// TypeName return struct type name
|
||||
// Play: https://go.dev/play/p/todo
|
||||
func (s *Struct) TypeName() string {
|
||||
return s.rtype.Name()
|
||||
}
|
||||
|
||||
@@ -177,3 +177,21 @@ func TestStruct_IsStruct(t *testing.T) {
|
||||
assert.Equal(true, s1.IsStruct())
|
||||
assert.Equal(false, s2.IsStruct())
|
||||
}
|
||||
|
||||
func TestStruct_TypeName(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert := internal.NewAssert(t, "TestStruct_TypeName")
|
||||
|
||||
type Test1 struct{}
|
||||
t1 := &Test1{}
|
||||
|
||||
s1 := New(t1)
|
||||
assert.Equal("Test1", s1.TypeName())
|
||||
|
||||
type Test2 struct{}
|
||||
t2 := Test2{}
|
||||
|
||||
s2 := New(t2)
|
||||
assert.Equal("Test2", s2.TypeName())
|
||||
}
|
||||
|
||||
@@ -19,30 +19,45 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
alphaMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]+$`)
|
||||
letterRegexMatcher *regexp.Regexp = regexp.MustCompile(`[a-zA-Z]`)
|
||||
alphaNumericMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z0-9-]+$`)
|
||||
numberRegexMatcher *regexp.Regexp = regexp.MustCompile(`\d`)
|
||||
intStrMatcher *regexp.Regexp = regexp.MustCompile(`^[\+-]?\d+$`)
|
||||
urlMatcher *regexp.Regexp = regexp.MustCompile(`^((ftp|http|https?):\/\/)?(\S+(:\S*)?@)?((([1-9]\d?|1\d\d|2[01]\d|22[0-3])(\.(1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.([0-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(([a-zA-Z0-9]+([-\.][a-zA-Z0-9]+)*)|((www\.)?))?(([a-z\x{00a1}-\x{ffff}0-9]+-?-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.([a-z\x{00a1}-\x{ffff}]{2,}))?))(:(\d{1,5}))?((\/|\?|#)[^\s]*)?$`)
|
||||
dnsMatcher *regexp.Regexp = regexp.MustCompile(`^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$`)
|
||||
emailMatcher *regexp.Regexp = regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,4}$`)
|
||||
chineseMobileMatcher *regexp.Regexp = regexp.MustCompile(`^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$`)
|
||||
chineseIdMatcher *regexp.Regexp = regexp.MustCompile(`^(\d{17})([0-9]|X|x)$`)
|
||||
chineseMatcher *regexp.Regexp = regexp.MustCompile("[\u4e00-\u9fa5]")
|
||||
chinesePhoneMatcher *regexp.Regexp = regexp.MustCompile(`\d{3}-\d{8}|\d{4}-\d{7}|\d{4}-\d{8}`)
|
||||
creditCardMatcher *regexp.Regexp = regexp.MustCompile(`^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\\d{3})\\d{11}|6[27][0-9]{14})$`)
|
||||
base64Matcher *regexp.Regexp = regexp.MustCompile(`^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$`)
|
||||
base64URLMatcher *regexp.Regexp = regexp.MustCompile(`^([A-Za-z0-9_-]{4})*([A-Za-z0-9_-]{2}(==)?|[A-Za-z0-9_-]{3}=?)?$`)
|
||||
binMatcher *regexp.Regexp = regexp.MustCompile(`^(0b)?[01]+$`)
|
||||
hexMatcher *regexp.Regexp = regexp.MustCompile(`^(#|0x|0X)?[0-9a-fA-F]+$`)
|
||||
visaMatcher *regexp.Regexp = regexp.MustCompile(`^4[0-9]{12}(?:[0-9]{3})?$`)
|
||||
masterCardMatcher *regexp.Regexp = regexp.MustCompile(`^5[1-5][0-9]{14}$`)
|
||||
americanExpressMatcher *regexp.Regexp = regexp.MustCompile(`^3[47][0-9]{13}$`)
|
||||
unionPay *regexp.Regexp = regexp.MustCompile("^62[0-5]\\d{13,16}$")
|
||||
chinaUnionPay *regexp.Regexp = regexp.MustCompile(`^62[0-9]{14,17}$`)
|
||||
alphaMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]+$`)
|
||||
letterRegexMatcher *regexp.Regexp = regexp.MustCompile(`[a-zA-Z]`)
|
||||
alphaNumericMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z0-9-]+$`)
|
||||
numberRegexMatcher *regexp.Regexp = regexp.MustCompile(`\d`)
|
||||
intStrMatcher *regexp.Regexp = regexp.MustCompile(`^[\+-]?\d+$`)
|
||||
// dnsMatcher *regexp.Regexp = regexp.MustCompile(`^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$`)
|
||||
dnsMatcher *regexp.Regexp = regexp.MustCompile(`^(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)*(?:xn--[a-zA-Z0-9\-]{1,59}|[a-zA-Z0-9](?:[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?)$`)
|
||||
chineseMobileMatcher *regexp.Regexp = regexp.MustCompile(`^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$`)
|
||||
chineseIdMatcher *regexp.Regexp = regexp.MustCompile(`([1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx])|([1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}[0-9Xx])`)
|
||||
chineseMatcher *regexp.Regexp = regexp.MustCompile("[\u4e00-\u9fa5]")
|
||||
chinesePhoneMatcher *regexp.Regexp = regexp.MustCompile(`\d{3}-\d{8}|\d{4}-\d{7}|\d{4}-\d{8}`)
|
||||
creditCardMatcher *regexp.Regexp = regexp.MustCompile(`^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\\d{3})\\d{11}|6[27][0-9]{14})$`)
|
||||
base64Matcher *regexp.Regexp = regexp.MustCompile(`^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$`)
|
||||
base64URLMatcher *regexp.Regexp = regexp.MustCompile(`^([A-Za-z0-9_-]{4})*([A-Za-z0-9_-]{2}(==)?|[A-Za-z0-9_-]{3}=?)?$`)
|
||||
binMatcher *regexp.Regexp = regexp.MustCompile(`^(0b)?[01]+$`)
|
||||
hexMatcher *regexp.Regexp = regexp.MustCompile(`^(#|0x|0X)?[0-9a-fA-F]+$`)
|
||||
visaMatcher *regexp.Regexp = regexp.MustCompile(`^4[0-9]{12}(?:[0-9]{3})?$`)
|
||||
masterCardMatcher *regexp.Regexp = regexp.MustCompile(`^5[1-5][0-9]{14}$`)
|
||||
americanExpressMatcher *regexp.Regexp = regexp.MustCompile(`^3[47][0-9]{13}$`)
|
||||
chinaUnionPayMatcher *regexp.Regexp = regexp.MustCompile(`^62[0-9]{14,17}$`)
|
||||
chineseHMPassportMatcher *regexp.Regexp = regexp.MustCompile(`^[CM]\d{8}$`)
|
||||
)
|
||||
|
||||
var passportMatcher = map[string]*regexp.Regexp{
|
||||
"CN": regexp.MustCompile(`^P\d{9}$`),
|
||||
"US": regexp.MustCompile(`^\d{9}$`),
|
||||
"GB": regexp.MustCompile(`^[A-Z0-9]{9}$`),
|
||||
"RU": regexp.MustCompile(`^[A-Z]{2}\d{7}$`),
|
||||
"DE": regexp.MustCompile(`^\d{9}$`),
|
||||
"FR": regexp.MustCompile(`^[A-Z]{2}\d{7}$`),
|
||||
"JP": regexp.MustCompile(`^\d{8}$`),
|
||||
"IT": regexp.MustCompile(`^\d{8}$`),
|
||||
"AU": regexp.MustCompile(`^[A-Z]{1}\d{8}$`),
|
||||
"BR": regexp.MustCompile(`^\d{9}$`),
|
||||
"IN": regexp.MustCompile(`^[A-Z]{1,2}\d{7}$`),
|
||||
"HK": regexp.MustCompile(`^M\d{8}$`),
|
||||
"MO": regexp.MustCompile(`^[A-Z]\d{8}$`),
|
||||
}
|
||||
|
||||
var (
|
||||
// Identity card formula
|
||||
factor = [17]int{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}
|
||||
@@ -258,21 +273,45 @@ func IsPort(str string) bool {
|
||||
// IsUrl check if the string is url.
|
||||
// Play: https://go.dev/play/p/pbJGa7F98Ka
|
||||
func IsUrl(str string) bool {
|
||||
if str == "" || len(str) >= 2083 || len(str) <= 3 || strings.HasPrefix(str, ".") {
|
||||
return false
|
||||
}
|
||||
u, err := url.Parse(str)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if strings.HasPrefix(u.Host, ".") {
|
||||
return false
|
||||
}
|
||||
if u.Host == "" && (u.Path != "" && !strings.Contains(u.Path, ".")) {
|
||||
if str == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
return urlMatcher.MatchString(str)
|
||||
u, err := url.Parse(str)
|
||||
if err != nil || u.Scheme == "" || u.Host == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
allowedSchemes := map[string]struct{}{
|
||||
"http": {},
|
||||
"https": {},
|
||||
"ftp": {},
|
||||
"ws": {},
|
||||
"wss": {},
|
||||
"file": {},
|
||||
"mailto": {},
|
||||
"data": {},
|
||||
}
|
||||
|
||||
if _, ok := allowedSchemes[u.Scheme]; !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
if u.Scheme == "file" || u.Scheme == "mailto" || u.Scheme == "data" {
|
||||
return true
|
||||
}
|
||||
|
||||
host := u.Hostname()
|
||||
if !strings.Contains(host, ".") || strings.HasSuffix(host, ".") {
|
||||
return false
|
||||
}
|
||||
|
||||
// domainRegexp := regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9\-\.]*[a-zA-Z0-9]$`)
|
||||
if !dnsMatcher.MatchString(host) {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// IsDns check if the string is dns.
|
||||
@@ -286,8 +325,6 @@ func IsDns(dns string) bool {
|
||||
func IsEmail(email string) bool {
|
||||
_, err := mail.ParseAddress(email)
|
||||
return err == nil
|
||||
|
||||
// return emailMatcher.MatchString(email)
|
||||
}
|
||||
|
||||
// IsChineseMobile check if the string is chinese mobile number.
|
||||
@@ -460,12 +497,13 @@ func IsZeroValue(value any) bool {
|
||||
func IsGBK(data []byte) bool {
|
||||
i := 0
|
||||
for i < len(data) {
|
||||
if data[i] <= 0xff {
|
||||
if data[i] < 0x81 {
|
||||
i++
|
||||
continue
|
||||
} else {
|
||||
if data[i] >= 0x81 &&
|
||||
data[i] <= 0xfe &&
|
||||
i+1 < len(data) &&
|
||||
data[i+1] >= 0x40 &&
|
||||
data[i+1] <= 0xfe &&
|
||||
data[i+1] != 0xf7 {
|
||||
@@ -561,12 +599,53 @@ func IsAmericanExpress(v string) bool {
|
||||
|
||||
// IsUnionPay check if a give string is a valid union pay nubmer or not.
|
||||
// Play: https://go.dev/play/p/CUHPEwEITDf
|
||||
func IsUnionPay(v string) bool {
|
||||
return unionPay.MatchString(v)
|
||||
func IsUnionPay(cardNo string) bool {
|
||||
if len(cardNo) < 16 || len(cardNo) > 19 {
|
||||
return false
|
||||
}
|
||||
|
||||
matched, _ := regexp.MatchString(`^\d+$`, cardNo)
|
||||
if !matched {
|
||||
return false
|
||||
}
|
||||
|
||||
if len(cardNo) < 3 {
|
||||
return false
|
||||
}
|
||||
|
||||
prefix := cardNo[:3]
|
||||
prefixNum, err := strconv.Atoi(prefix)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if prefixNum < 620 || prefixNum > 625 {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// IsChinaUnionPay check if a give string is a valid china union pay nubmer or not.
|
||||
// Play: https://go.dev/play/p/yafpdxLiymu
|
||||
func IsChinaUnionPay(v string) bool {
|
||||
return chinaUnionPay.MatchString(v)
|
||||
func IsChinaUnionPay(cardNo string) bool {
|
||||
return chinaUnionPayMatcher.MatchString(cardNo)
|
||||
}
|
||||
|
||||
// IsPassport checks if the passport number is valid for a given country.
|
||||
// country is a two-letter country code (ISO 3166-1 alpha-2).
|
||||
// Play: todo
|
||||
func IsPassport(passport, country string) bool {
|
||||
if matcher, ok := passportMatcher[country]; ok {
|
||||
return matcher.MatchString(passport)
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// IsChineseHMPassport checks if the string is a valid Chinese Hong Kong and Macau Travel Permit number.
|
||||
// Chinese Hong Kong and Macau Travel Permit format: C or M + 8 digits (e.g., C12345678, M12345678).
|
||||
// Play: https://go.dev/play/p/TODO
|
||||
func IsChineseHMPassport(hmPassport string) bool {
|
||||
return chineseHMPassportMatcher.MatchString(hmPassport)
|
||||
}
|
||||
|
||||
@@ -214,7 +214,7 @@ func ExampleIsUrl() {
|
||||
fmt.Println(result3)
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
// true
|
||||
// false
|
||||
}
|
||||
@@ -683,3 +683,42 @@ func ExampleIsAlphaNumeric() {
|
||||
// true
|
||||
// false
|
||||
}
|
||||
|
||||
func ExampleIsPassport() {
|
||||
result1 := IsPassport("P123456789", "CN")
|
||||
result2 := IsPassport("123456789", "US")
|
||||
result3 := IsPassport("AB1234567", "RU")
|
||||
result4 := IsPassport("123456789", "CN")
|
||||
|
||||
fmt.Println(result1)
|
||||
fmt.Println(result2)
|
||||
fmt.Println(result3)
|
||||
fmt.Println(result4)
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// true
|
||||
// true
|
||||
// false
|
||||
}
|
||||
|
||||
func ExampleIsChineseHMPassport() {
|
||||
result1 := IsChineseHMPassport("C12345678")
|
||||
result2 := IsChineseHMPassport("C00000000")
|
||||
result3 := IsChineseHMPassport("M12345678")
|
||||
result4 := IsChineseHMPassport("c12345678")
|
||||
result5 := IsChineseHMPassport("C1234567")
|
||||
|
||||
fmt.Println(result1)
|
||||
fmt.Println(result2)
|
||||
fmt.Println(result3)
|
||||
fmt.Println(result4)
|
||||
fmt.Println(result5)
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// true
|
||||
// true
|
||||
// false
|
||||
// false
|
||||
}
|
||||
|
||||
@@ -437,10 +437,32 @@ func TestIsUrl(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestIsUrl")
|
||||
|
||||
assert.Equal(true, IsUrl("http://abc.com"))
|
||||
assert.Equal(true, IsUrl("abc.com"))
|
||||
assert.Equal(true, IsUrl("a.b.com"))
|
||||
assert.Equal(false, IsUrl("abc"))
|
||||
tests := []struct {
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"http://abc.com", true},
|
||||
{"https://abc.com", true},
|
||||
{"ftp://abc.com", true},
|
||||
{"http://abc.com/path?query=123", true},
|
||||
{"https://abc.com/path/to/resource", true},
|
||||
{"ws://abc.com", true},
|
||||
{"wss://abc.com", true},
|
||||
{"mailto://abc.com", true},
|
||||
{"file://path/to/file", true},
|
||||
{"data://text/plain;base64,SGVsbG8sIFdvcmxkIQ==", true},
|
||||
{"http://abc.com/path/to/resource?query=123#fragment", true},
|
||||
|
||||
{"abc", false},
|
||||
{"http://", false},
|
||||
{"http://abc", false},
|
||||
{"http://abc:8080", false},
|
||||
{"http://abc:99999999", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, IsUrl(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsDns(t *testing.T) {
|
||||
@@ -455,7 +477,8 @@ func TestIsDns(t *testing.T) {
|
||||
{"abc.com", true},
|
||||
{"123.cn", true},
|
||||
{"a.b.com", true},
|
||||
{"a.b.c", false},
|
||||
{"a.b.c", true},
|
||||
{"www.xn--6qq986b3xl.xn--fiqs8s.com", true},
|
||||
{"a@b.com", false},
|
||||
{"http://abc.com", false},
|
||||
}
|
||||
@@ -477,12 +500,24 @@ func TestIsEmail(t *testing.T) {
|
||||
|
||||
func TestContainChinese(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert := internal.NewAssert(t, "TestContainChinese")
|
||||
|
||||
assert.Equal(true, ContainChinese("你好"))
|
||||
assert.Equal(true, ContainChinese("你好hello"))
|
||||
assert.Equal(false, ContainChinese("hello"))
|
||||
tests := []struct {
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"你好", true},
|
||||
{"hello", false},
|
||||
{"你好hello", true},
|
||||
{"hello你好", true},
|
||||
{"", false},
|
||||
{"123", false},
|
||||
{"!@#$%^&*()", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, ContainChinese(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsChineseMobile(t *testing.T) {
|
||||
@@ -490,8 +525,20 @@ func TestIsChineseMobile(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestIsChineseMobile")
|
||||
|
||||
assert.Equal(true, IsChineseMobile("13263527980"))
|
||||
assert.Equal(false, IsChineseMobile("434324324"))
|
||||
tests := []struct {
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"13263527980", true},
|
||||
{"1326352798", false},
|
||||
{"132635279801", false},
|
||||
{"1326352798a", false},
|
||||
{"1326352798@", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, IsChineseMobile(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsChinesePhone(t *testing.T) {
|
||||
@@ -887,7 +934,7 @@ func TestIsUnionPay(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := internal.NewAssert(t, "TestIsUnionPay")
|
||||
|
||||
assert.Equal(true, IsUnionPay("6221263430109903"))
|
||||
assert.Equal(true, IsUnionPay("6228480402564890"))
|
||||
assert.Equal(false, IsUnionPay("3782822463100007"))
|
||||
}
|
||||
|
||||
@@ -895,8 +942,25 @@ func TestIsChinaUnionPay(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := internal.NewAssert(t, "TestIsChinaUnionPay")
|
||||
|
||||
assert.Equal(true, IsChinaUnionPay("6250941006528599"))
|
||||
assert.Equal(false, IsChinaUnionPay("3782822463100007"))
|
||||
tests := []struct {
|
||||
cardNumber string
|
||||
expected bool
|
||||
}{
|
||||
{"6228480420000000000", true},
|
||||
{"6214830000000000", true},
|
||||
{"6230580000000000000", true},
|
||||
{"6259640000000000000", true},
|
||||
{"6260000000000000000", true},
|
||||
{"6288888888888888", true},
|
||||
|
||||
// 非银联前缀
|
||||
{"4123456789012345", false},
|
||||
{"3528000000000000", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, IsChinaUnionPay(tt.cardNumber))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsAlphaNumeric(t *testing.T) {
|
||||
@@ -924,3 +988,72 @@ func TestIsAlphaNumeric(t *testing.T) {
|
||||
assert.Equal(tt.expected, IsAlphaNumeric(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsPassport(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert := internal.NewAssert(t, "TestIsPassport")
|
||||
|
||||
tests := []struct {
|
||||
passport string
|
||||
countryCode string
|
||||
expected bool
|
||||
}{
|
||||
{"P123456789", "CN", true},
|
||||
{"123456789", "US", true},
|
||||
{"A12345678", "GB", true},
|
||||
{"AB1234567", "FR", true},
|
||||
{"12345678", "JP", true},
|
||||
{"M12345678", "HK", true},
|
||||
{"A12345678", "MO", true},
|
||||
{"A1234567", "IN", true},
|
||||
{"12345678", "IT", true},
|
||||
{"A12345678", "AU", true},
|
||||
{"123456789", "BR", true},
|
||||
{"AB1234567", "RU", true},
|
||||
{"123456789", "CN", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, IsPassport(tt.passport, tt.countryCode))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsChineseHMPassport(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert := internal.NewAssert(t, "TestIsChineseHMPassport")
|
||||
|
||||
tests := []struct {
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"C12345678", true},
|
||||
{"C00000000", true},
|
||||
{"C99999999", true},
|
||||
{"M12345678", true}, // M prefix
|
||||
{"M00000000", true}, // M prefix
|
||||
{"M99999999", true}, // M prefix
|
||||
{"c12345678", false}, // lowercase c
|
||||
{"m12345678", false}, // lowercase m
|
||||
{"C1234567", false}, // 7 digits
|
||||
{"M1234567", false}, // 7 digits with M
|
||||
{"C123456789", false}, // 9 digits
|
||||
{"M123456789", false}, // 9 digits with M
|
||||
{"C1234567a", false}, // contains letter
|
||||
{"M1234567a", false}, // contains letter with M
|
||||
{"D12345678", false}, // starts with D
|
||||
{"12345678", false}, // no prefix
|
||||
{"CC12345678", false}, // double C
|
||||
{"MM12345678", false}, // double M
|
||||
{"C 12345678", false}, // contains space
|
||||
{"M 12345678", false}, // contains space with M
|
||||
{"C12345-678", false}, // contains dash
|
||||
{"M12345-678", false}, // contains dash with M
|
||||
{"", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, IsChineseHMPassport(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user