mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
Compare commits
5 Commits
d5b9e67330
...
385e64cc52
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
385e64cc52 | ||
|
|
be45a259db | ||
|
|
6f703fe577 | ||
|
|
cb8d93c499 | ||
|
|
ae1014c572 |
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -1465,36 +1463,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.
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -39,10 +39,26 @@ var (
|
||||
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}$`)
|
||||
unionPayMatcher *regexp.Regexp = regexp.MustCompile(`^62[0-5]\\d{13,16}$`)
|
||||
chinaUnionPayMatcher *regexp.Regexp = regexp.MustCompile(`^62[0-9]{14,17}$`)
|
||||
)
|
||||
|
||||
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}
|
||||
@@ -460,12 +476,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 {
|
||||
@@ -562,11 +579,22 @@ 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)
|
||||
return unionPayMatcher.MatchString(v)
|
||||
}
|
||||
|
||||
// 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)
|
||||
return chinaUnionPayMatcher.MatchString(v)
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
@@ -683,3 +683,21 @@ 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
|
||||
}
|
||||
|
||||
@@ -924,3 +924,33 @@ 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))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user