mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-06 13:42:28 +08:00
Compare commits
11 Commits
v2.3.7
...
7d4b9510a2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7d4b9510a2 | ||
|
|
9f0ad2354a | ||
|
|
55b66dee99 | ||
|
|
4c64a16204 | ||
|
|
385e64cc52 | ||
|
|
be45a259db | ||
|
|
6f703fe577 | ||
|
|
cb8d93c499 | ||
|
|
ae1014c572 | ||
|
|
d5b9e67330 | ||
|
|
a81403766f |
@@ -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-----
|
||||
MIIJKAIBAAKCAgEAw0anfgtraA2uaZwoLpLBvo1EkfYvDBgeXoMQ4WMKbcw6jU8k
|
||||
18E+f3WM52I2RssEk6g0X1vIiarHZU5qyxbv2iNoT7EfgizzlXYvx06pM69GNBQr
|
||||
V46+lNhiOv4eeLZcOHBnxAyizlIKyLuwO+C1cX/6BxuXjX3ogw+6IaBFZN/EOMmT
|
||||
Wc6sPKrnNCEqgCNiTbPAXb+N8j5Iv8QPs0AwVTB3jC9LP0mRt2GGD0cu+QIZkoGE
|
||||
hyW9Dd+0tgeIK32uXCF8uDjNpUV1TxCdn5H+lvgddxfiaIJAdY5UaTx48XRIdULh
|
||||
QrVKXJNyTYWTXbezViYI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6VHH2hP/+jmEre/9E1D9c3EImqgT0wNR/gMTNtQ0t0Js2Xk=
|
||||
-----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==
|
||||
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAxBXtNRZ18uz+ujnKC/CB
|
||||
6bYjOHn0++xX1tbqRoRfP4VP01R/k9d28sI9cqQa9Oqo3TPehKAyR4m3p9ZJ+pOg
|
||||
NLpe7QEPWSGqIvPILRqS/4NnnjBuKvRHRTqiQwiuzTo/BBTxJCtF028Q7yRKmloE
|
||||
dtrdFryqsZmTrtMIvaNneTa/Bnx33s4vfAm76wNKF5li0AN7NsmKfWjOG7/f29qp
|
||||
9rTJEBnCIKImrwTFY9BWrx83LPDAUq8tpn8zevEe0c/42yZNlBprea4FIdZs8A/3
|
||||
gDZE7kltYjdybox0XWjR9znWPLmH3De+1xJ0woJ53f3jYDJ3REwmm8+sX26FIT3W
|
||||
PN/ksVgHsoaj2IZ3MaEkxf4E4GEp9sMSP8b+ans/B5gmcZi2pO8nx/ASVlxCthUS
|
||||
aK92CsoKHzqTotaZ+/dt689qT4zuqUWZpnlLIUKd0t1G485S8km15GgPcMY5vXe1
|
||||
3i8dmsxVRIjgYclSClKCnDBTTAV5GUSPnanPU4N0dlKpO0UxzILixaw227yw7zDy
|
||||
+rQ8ixchQ+ROQ6I4bdced7FuVXfEAa2gVuzm3uZ1J/xDnV6xwLYye2SitlzAL0wA
|
||||
/mFAF6koSa6+MYV+tzdPta2Xkw9xmO3a5PKwhCvOUbEMBJfLI2GPAfTuPh1LH4MZ
|
||||
qFUgzYHUiBCI21N3kMVPGAUCAwEAAQ==
|
||||
-----END rsa public key-----
|
||||
|
||||
@@ -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
|
||||
}
|
||||
```
|
||||
|
||||
@@ -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"))
|
||||
}
|
||||
|
||||
|
||||
@@ -507,19 +507,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 +690,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 +735,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 +754,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 +801,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 +820,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 +1024,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 +1047,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 +1055,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 +1074,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 +1247,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 +1473,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.
|
||||
|
||||
@@ -831,7 +831,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
|
||||
}
|
||||
|
||||
@@ -1008,7 +1008,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 +1756,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) {
|
||||
|
||||
@@ -19,30 +19,48 @@ 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+$`)
|
||||
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}$`)
|
||||
emailMatcher *regexp.Regexp = regexp.MustCompile(`\w+(-+.\w+)*@\w+(-.\w+)*.\w+(-.\w+)*`)
|
||||
chineseMobileMatcher *regexp.Regexp = regexp.MustCompile(`^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$`)
|
||||
chineseIdMatcher *regexp.Regexp = regexp.MustCompile(`([1-9]\d{5}(18|19|([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}$`)
|
||||
unionPayMatcher *regexp.Regexp = regexp.MustCompile(`^62[0-5]\\d{13,16}$`)
|
||||
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 +276,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 +328,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 +500,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 +602,71 @@ 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)
|
||||
}
|
||||
|
||||
// luhnCheck checks if the credit card number is valid using the Luhn algorithm.
|
||||
func luhnCheck(card string) bool {
|
||||
var sum int
|
||||
alt := false
|
||||
for i := len(card) - 1; i >= 0; i-- {
|
||||
n := int(card[i] - '0')
|
||||
if alt {
|
||||
n *= 2
|
||||
if n > 9 {
|
||||
n -= 9
|
||||
}
|
||||
}
|
||||
sum += n
|
||||
alt = !alt
|
||||
}
|
||||
return sum%10 == 0
|
||||
}
|
||||
|
||||
// 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) {
|
||||
@@ -477,12 +499,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 +524,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 +933,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 +941,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 +987,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