mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-17 03:02:28 +08:00
feat: add IsNotBlank func (#132)
Co-authored-by: yuanyou <yuanyou@kezaihui.com>
This commit is contained in:
@@ -1543,6 +1543,8 @@ import "github.com/duke-git/lancet/v2/strutil"
|
|||||||
- **<big>IsBlank</big>** : checks if a string is whitespace or empty.
|
- **<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)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/strutil.md#IsBlank)]
|
||||||
[[play](https://go.dev/play/p/6zXRH_c0Qd3)]
|
[[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.
|
- **<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)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/strutil.md#HasPrefixAny)]
|
||||||
[[play](https://go.dev/play/p/8UUTl2C5slo)]
|
[[play](https://go.dev/play/p/8UUTl2C5slo)]
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ import (
|
|||||||
- [StringToBytes](#StringToBytes)
|
- [StringToBytes](#StringToBytes)
|
||||||
- [BytesToString](#BytesToString)
|
- [BytesToString](#BytesToString)
|
||||||
- [IsBlank](#IsBlank)
|
- [IsBlank](#IsBlank)
|
||||||
|
- [IsNotBlank](#IsNotBlank)
|
||||||
- [HasPrefixAny](#HasPrefixAny)
|
- [HasPrefixAny](#HasPrefixAny)
|
||||||
- [HasSuffixAny](#HasSuffixAny)
|
- [HasSuffixAny](#HasSuffixAny)
|
||||||
- [IndexOffset](#IndexOffset)
|
- [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>
|
### <span id="HasPrefixAny">HasPrefixAny</span>
|
||||||
|
|
||||||
<p>检查字符串是否以指定字符串数组中的任何一个开头。</p>
|
<p>检查字符串是否以指定字符串数组中的任何一个开头。</p>
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ import (
|
|||||||
- [StringToBytes](#StringToBytes)
|
- [StringToBytes](#StringToBytes)
|
||||||
- [BytesToString](#BytesToString)
|
- [BytesToString](#BytesToString)
|
||||||
- [IsBlank](#IsBlank)
|
- [IsBlank](#IsBlank)
|
||||||
|
- [IsNotBlank](#IsNotBlank)
|
||||||
- [HasPrefixAny](#HasPrefixAny)
|
- [HasPrefixAny](#HasPrefixAny)
|
||||||
- [HasSuffixAny](#HasSuffixAny)
|
- [HasSuffixAny](#HasSuffixAny)
|
||||||
- [IndexOffset](#IndexOffset)
|
- [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>
|
### <span id="HasPrefixAny">HasPrefixAny</span>
|
||||||
|
|
||||||
<p>Checks if a string starts with any of an array of specified strings.</p>
|
<p>Checks if a string starts with any of an array of specified strings.</p>
|
||||||
|
|||||||
@@ -408,6 +408,11 @@ func IsBlank(str string) bool {
|
|||||||
return true
|
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.
|
// HasPrefixAny check if a string starts with any of a slice of specified strings.
|
||||||
// Play: https://go.dev/play/p/8UUTl2C5slo
|
// Play: https://go.dev/play/p/8UUTl2C5slo
|
||||||
func HasPrefixAny(str string, prefixes []string) bool {
|
func HasPrefixAny(str string, prefixes []string) bool {
|
||||||
|
|||||||
@@ -479,6 +479,26 @@ func ExampleIsBlank() {
|
|||||||
// false
|
// 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() {
|
func ExampleHasPrefixAny() {
|
||||||
result1 := HasPrefixAny("foo bar", []string{"fo", "xyz", "hello"})
|
result1 := HasPrefixAny("foo bar", []string{"fo", "xyz", "hello"})
|
||||||
result2 := HasPrefixAny("foo bar", []string{"oom", "world"})
|
result2 := HasPrefixAny("foo bar", []string{"oom", "world"})
|
||||||
|
|||||||
@@ -427,6 +427,18 @@ func TestIsBlank(t *testing.T) {
|
|||||||
assert.Equal(IsBlank(" 中文"), false)
|
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) {
|
func TestHasPrefixAny(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user