mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
feat: add IsVisa, IsMasterCard, IsAmericanExpress, IsUnionPay, IsChinaUnionPay
This commit is contained in:
@@ -30,9 +30,13 @@ var (
|
||||
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}$`)
|
||||
)
|
||||
|
||||
// IsAlpha checks if the string contains only letters (a-zA-Z).
|
||||
@@ -440,3 +444,33 @@ func IsJWT(v string) bool {
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// IsVisa check if a give string is a valid visa card nubmer or not.
|
||||
// Play: todo
|
||||
func IsVisa(v string) bool {
|
||||
return visaMatcher.MatchString(v)
|
||||
}
|
||||
|
||||
// IsMasterCard check if a give string is a valid master card nubmer or not.
|
||||
// Play: todo
|
||||
func IsMasterCard(v string) bool {
|
||||
return masterCardMatcher.MatchString(v)
|
||||
}
|
||||
|
||||
// IsAmericanExpress check if a give string is a valid american expression card nubmer or not.
|
||||
// Play: todo
|
||||
func IsAmericanExpress(v string) bool {
|
||||
return americanExpressMatcher.MatchString(v)
|
||||
}
|
||||
|
||||
// IsUnionPay check if a give string is a valid union pay nubmer or not.
|
||||
// Play: todo
|
||||
func IsUnionPay(v string) bool {
|
||||
return unionPay.MatchString(v)
|
||||
}
|
||||
|
||||
// IsChinaUnionPay check if a give string is a valid china union pay nubmer or not.
|
||||
// Play: todo
|
||||
func IsChinaUnionPay(v string) bool {
|
||||
return chinaUnionPay.MatchString(v)
|
||||
}
|
||||
|
||||
@@ -594,3 +594,43 @@ func TestIsJWT(t *testing.T) {
|
||||
assert.Equal(true, IsJWT("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibWVzc2FnZSI6IlB1dGluIGlzIGFic29sdXRlIHNoaXQiLCJpYXQiOjE1MTYyMzkwMjJ9.wkLWA5GtCpWdxNOrRse8yHZgORDgf8TpJp73WUQb910"))
|
||||
assert.Equal(false, IsJWT("abc"))
|
||||
}
|
||||
|
||||
func TestIsVisa(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := internal.NewAssert(t, "TestIsVisa")
|
||||
|
||||
assert.Equal(true, IsVisa("4111111111111111"))
|
||||
assert.Equal(false, IsVisa("123"))
|
||||
}
|
||||
|
||||
func TestIsMasterCard(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := internal.NewAssert(t, "TestIsMasterCard")
|
||||
|
||||
assert.Equal(true, IsMasterCard("5425233430109903"))
|
||||
assert.Equal(false, IsMasterCard("4111111111111111"))
|
||||
}
|
||||
|
||||
func TestIsAmericanExpress(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := internal.NewAssert(t, "TestIsAmericanExpress")
|
||||
|
||||
assert.Equal(true, IsAmericanExpress("342883359122187"))
|
||||
assert.Equal(false, IsAmericanExpress("3782822463100007"))
|
||||
}
|
||||
|
||||
func TestIsUnionPay(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := internal.NewAssert(t, "TestIsUnionPay")
|
||||
|
||||
assert.Equal(true, IsUnionPay("6221263430109903"))
|
||||
assert.Equal(false, IsUnionPay("3782822463100007"))
|
||||
}
|
||||
|
||||
func TestIsChinaUnionPay(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := internal.NewAssert(t, "TestIsChinaUnionPay")
|
||||
|
||||
assert.Equal(true, IsChinaUnionPay("6250941006528599"))
|
||||
assert.Equal(false, IsChinaUnionPay("3782822463100007"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user