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

feat: add IsNotBlank func (#132)

Co-authored-by: yuanyou <yuanyou@kezaihui.com>
This commit is contained in:
elza
2023-09-13 15:29:26 +08:00
committed by GitHub
parent b698fec50f
commit f445ecbaf8
6 changed files with 119 additions and 0 deletions

View File

@@ -1543,6 +1543,8 @@ import "github.com/duke-git/lancet/v2/strutil"
- **<big>IsBlank</big>** : checks if a string is whitespace or empty.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/strutil.md#IsBlank)]
[[play](https://go.dev/play/p/6zXRH_c0Qd3)]
- **<big>IsNotBlank</big>** : checks if a string is not whitespace or not empty.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/strutil.md#IsNotBlank)]
- **<big>HasPrefixAny</big>** : checks if a string starts with any of an array of specified strings.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/strutil.md#HasPrefixAny)]
[[play](https://go.dev/play/p/8UUTl2C5slo)]

View File

@@ -49,6 +49,7 @@ import (
- [StringToBytes](#StringToBytes)
- [BytesToString](#BytesToString)
- [IsBlank](#IsBlank)
- [IsNotBlank](#IsNotBlank)
- [HasPrefixAny](#HasPrefixAny)
- [HasSuffixAny](#HasSuffixAny)
- [IndexOffset](#IndexOffset)
@@ -1075,6 +1076,45 @@ func main() {
}
```
### <span id="IsNotBlank">IsNotBlank</span>
<p>Checks if a string is not whitespace or not empty.</p>
<b>Signature:</b>
```go
func IsNotBlank(str string) bool
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
result1 := IsNotBlank("")
result2 := IsNotBlank(" ")
result3 := IsNotBlank("\t\v\f\n")
result4 := IsNotBlank(" 中文")
result5 := IsNotBlank(" world ")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
// Output:
// false
// false
// false
// true
// true
}
```
### <span id="HasPrefixAny">HasPrefixAny</span>
<p>检查字符串是否以指定字符串数组中的任何一个开头。</p>

View File

@@ -49,6 +49,7 @@ import (
- [StringToBytes](#StringToBytes)
- [BytesToString](#BytesToString)
- [IsBlank](#IsBlank)
- [IsNotBlank](#IsNotBlank)
- [HasPrefixAny](#HasPrefixAny)
- [HasSuffixAny](#HasSuffixAny)
- [IndexOffset](#IndexOffset)
@@ -1076,6 +1077,45 @@ func main() {
}
```
### <span id="IsNotBlank">IsNotBlank</span>
<p>Checks if a string is not whitespace or not empty.</p>
<b>Signature:</b>
```go
func IsNotBlank(str string) bool
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
result1 := IsNotBlank("")
result2 := IsNotBlank(" ")
result3 := IsNotBlank("\t\v\f\n")
result4 := IsNotBlank(" 中文")
result5 := IsNotBlank(" world ")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
// Output:
// false
// false
// false
// true
// true
}
```
### <span id="HasPrefixAny">HasPrefixAny</span>
<p>Checks if a string starts with any of an array of specified strings.</p>

View File

@@ -408,6 +408,11 @@ func IsBlank(str string) bool {
return true
}
// IsNotBlank checks if a string is not whitespace, not empty.
func IsNotBlank(str string) bool {
return !IsBlank(str)
}
// HasPrefixAny check if a string starts with any of a slice of specified strings.
// Play: https://go.dev/play/p/8UUTl2C5slo
func HasPrefixAny(str string, prefixes []string) bool {

View File

@@ -479,6 +479,26 @@ func ExampleIsBlank() {
// false
}
func ExampleIsNotBlank() {
result1 := IsNotBlank("")
result2 := IsNotBlank(" ")
result3 := IsNotBlank("\t\v\f\n")
result4 := IsNotBlank(" 中文")
result5 := IsNotBlank(" world ")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
// Output:
// false
// false
// false
// true
// true
}
func ExampleHasPrefixAny() {
result1 := HasPrefixAny("foo bar", []string{"fo", "xyz", "hello"})
result2 := HasPrefixAny("foo bar", []string{"oom", "world"})

View File

@@ -427,6 +427,18 @@ func TestIsBlank(t *testing.T) {
assert.Equal(IsBlank(" 中文"), false)
}
func TestIsNotBlank(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestIsBlank")
assert.Equal(IsNotBlank(""), false)
assert.Equal(IsNotBlank(" "), false)
assert.Equal(IsNotBlank("\t\v\f\n"), false)
assert.Equal(IsNotBlank(" 中文"), true)
assert.Equal(IsNotBlank(" world "), true)
}
func TestHasPrefixAny(t *testing.T) {
t.Parallel()