1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

feat: add IsBin, IsHex, IsBase64URL, IsJWT

This commit is contained in:
dudaodong
2023-09-06 17:42:31 +08:00
parent 20786c360b
commit 71c7733eb0
3 changed files with 152 additions and 0 deletions

View File

@@ -29,6 +29,10 @@ var (
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]+$`)
)
// IsAlpha checks if the string contains only letters (a-zA-Z).
@@ -401,3 +405,38 @@ func IsInt(v any) bool {
}
return false
}
// IsBin check if a give string is a valid binary value or not.
// Play: todo
func IsBin(v string) bool {
return binMatcher.MatchString(v)
}
// IsHex check if a give string is a valid hexadecimal value or not.
// Play: todo
func IsHex(v string) bool {
return hexMatcher.MatchString(v)
}
// IsBase64URL check if a give string is a valid URL-safe Base64 encoded string.
// Play: todo
func IsBase64URL(v string) bool {
return base64URLMatcher.MatchString(v)
}
// IsJWT check if a give string is is a valid JSON Web Token (JWT).
// Play: todo
func IsJWT(v string) bool {
strings := strings.Split(v, ".")
if len(strings) != 3 {
return false
}
for _, s := range strings {
if !IsBase64URL(s) {
return false
}
}
return true
}

View File

@@ -521,3 +521,69 @@ func ExampleIsNumber() {
// true
// true
}
func ExampleIsBin() {
result1 := IsBin("0101")
result2 := IsBin("0b1101")
result3 := IsBin("b1101")
result4 := IsBin("1201")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
// Output:
// true
// true
// false
// false
}
func ExampleIsHex() {
result1 := IsHex("0xabcde")
result2 := IsHex("0XABCDE")
result3 := IsHex("cdfeg")
result4 := IsHex("0xcdfeg")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
// Output:
// true
// true
// false
// false
}
func ExampleIsBase64URL() {
result1 := IsBase64URL("SAGsbG8sIHdvcmxkIQ")
result2 := IsBase64URL("SAGsbG8sIHdvcmxkIQ==")
result3 := IsBase64URL("SAGsbG8sIHdvcmxkIQ=")
result4 := IsBase64URL("SAGsbG8sIHdvcmxkIQ===")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
// Output:
// true
// true
// false
// false
}
func ExampleIsJWT() {
result1 := IsJWT("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibWVzc2FnZSI6IlB1dGluIGlzIGFic29sdXRlIHNoaXQiLCJpYXQiOjE1MTYyMzkwMjJ9.wkLWA5GtCpWdxNOrRse8yHZgORDgf8TpJp73WUQb910")
result2 := IsJWT("abc")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// true
// false
}

View File

@@ -547,3 +547,50 @@ func TestIsPrintable(t *testing.T) {
assert.Equal(true, IsPrintable("😄"))
assert.Equal(false, IsPrintable("\u0000"))
}
func TestIsBin(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestIsBin")
assert.Equal(true, IsBin("0101"))
assert.Equal(true, IsBin("0b1101"))
assert.Equal(false, IsBin("b1101"))
assert.Equal(false, IsBin("1201"))
assert.Equal(false, IsBin(""))
}
func TestIsHex(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestIsHex")
assert.Equal(true, IsHex("ABCDE"))
assert.Equal(true, IsHex("abcde"))
assert.Equal(true, IsHex("0xabcde"))
assert.Equal(true, IsHex("0Xabcde"))
assert.Equal(true, IsHex("#abcde"))
assert.Equal(false, IsHex("cdfeg"))
assert.Equal(false, IsHex("0xcdfeg"))
assert.Equal(false, IsHex(""))
}
func TestIsBase64URL(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestIsBase64URL")
assert.Equal(true, IsBase64URL("SAGsbG8sIHdvcmxkIQ"))
assert.Equal(true, IsBase64URL("SAGsbG8sIHdvcmxkIQ=="))
assert.Equal(false, IsBase64URL("SAGsbG8sIHdvcmxkIQ="))
assert.Equal(false, IsBase64URL("SAGsbG8sIHdvcmxkIQ==="))
// assert.Equal(false, IsBase64URL(""))
}
func TestIsJWT(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestIsJWT")
assert.Equal(true, IsJWT("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibWVzc2FnZSI6IlB1dGluIGlzIGFic29sdXRlIHNoaXQiLCJpYXQiOjE1MTYyMzkwMjJ9.wkLWA5GtCpWdxNOrRse8yHZgORDgf8TpJp73WUQb910"))
assert.Equal(false, IsJWT("abc"))
}