mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
Compare commits
2 Commits
0ed2b11ba1
...
de877e5278
| Author | SHA1 | Date | |
|---|---|---|---|
| de877e5278 | |||
| 08f14d2b08 |
@@ -72,10 +72,17 @@ func Nand[T, U any](a T, b U) bool {
|
|||||||
|
|
||||||
// TernaryOperator checks the value of param `isTrue`, if true return ifValue else return elseValue.
|
// TernaryOperator checks the value of param `isTrue`, if true return ifValue else return elseValue.
|
||||||
// Play: https://go.dev/play/p/ElllPZY0guT
|
// Play: https://go.dev/play/p/ElllPZY0guT
|
||||||
func TernaryOperator[T, U any](isTrue T, ifValue U, elseValue U) U {
|
func Ternary[T, U any](isTrue T, ifValue U, elseValue U) U {
|
||||||
if Bool(isTrue) {
|
if Bool(isTrue) {
|
||||||
return ifValue
|
return ifValue
|
||||||
} else {
|
} else {
|
||||||
return elseValue
|
return elseValue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TernaryOperator checks the value of param `isTrue`, if true return ifValue else return elseValue.
|
||||||
|
// Play: https://go.dev/play/p/ElllPZY0guT
|
||||||
|
// Deprecated: Use Ternary instead.
|
||||||
|
func TernaryOperator[T, U any](isTrue T, ifValue U, elseValue U) U {
|
||||||
|
return Ternary(isTrue, ifValue, elseValue)
|
||||||
|
}
|
||||||
|
|||||||
@@ -148,13 +148,13 @@ func ExampleNand() {
|
|||||||
// false
|
// false
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleTernaryOperator() {
|
func ExampleTernary() {
|
||||||
conditionTrue := 2 > 1
|
conditionTrue := 2 > 1
|
||||||
result1 := TernaryOperator(conditionTrue, 0, 1)
|
result1 := Ternary(conditionTrue, 0, 1)
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
|
|
||||||
conditionFalse := 2 > 3
|
conditionFalse := 2 > 3
|
||||||
result2 := TernaryOperator(conditionFalse, 0, 1)
|
result2 := Ternary(conditionFalse, 0, 1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
|
|||||||
@@ -124,13 +124,13 @@ func TestNand(t *testing.T) {
|
|||||||
assert.Equal(false, Nand(1, 1))
|
assert.Equal(false, Nand(1, 1))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTernaryOperator(t *testing.T) {
|
func TestTernary(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
assert := internal.NewAssert(t, "TernaryOperator")
|
assert := internal.NewAssert(t, "TestTernary")
|
||||||
|
|
||||||
trueValue := "1"
|
trueValue := "1"
|
||||||
falseValue := "0"
|
falseValue := "0"
|
||||||
|
|
||||||
assert.Equal(trueValue, TernaryOperator(true, trueValue, falseValue))
|
assert.Equal(trueValue, Ternary(true, trueValue, falseValue))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,8 @@ import (
|
|||||||
- [Nor](#Nor)
|
- [Nor](#Nor)
|
||||||
- [Xnor](#Xnor)
|
- [Xnor](#Xnor)
|
||||||
- [Nand](#Nand)
|
- [Nand](#Nand)
|
||||||
- [TernaryOperator](#TernaryOperator)
|
- [Ternary](#Ternary)
|
||||||
|
- [TernaryOperator<sup>deprecated</sup>](#TernaryOperator)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -257,9 +258,45 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="Ternary">Ternary</span>
|
||||||
|
<p>三元运算符。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Ternary[T, U any](isTrue T, ifValue U, elseValue U) U
|
||||||
|
```
|
||||||
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/ElllPZY0guT)</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/condition"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
conditionTrue := 2 > 1
|
||||||
|
result1 := condition.Ternary(conditionTrue, 0, 1)
|
||||||
|
|
||||||
|
conditionFalse := 2 > 3
|
||||||
|
result2 := condition.Ternary(conditionFalse, 0, 1)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 0
|
||||||
|
// 1
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="TernaryOperator">TernaryOperator</span>
|
### <span id="TernaryOperator">TernaryOperator</span>
|
||||||
<p>三元运算符</p>
|
<p>三元运算符</p>
|
||||||
|
|
||||||
|
> ⚠️ 本函数已弃用,使用`Ternary`代替。
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|||||||
@@ -68,6 +68,7 @@ import (
|
|||||||
- [Rotate](#Rotate)
|
- [Rotate](#Rotate)
|
||||||
- [TemplateReplace](#TemplateReplace)
|
- [TemplateReplace](#TemplateReplace)
|
||||||
- [RegexMatchAllGroups](#RegexMatchAllGroups)
|
- [RegexMatchAllGroups](#RegexMatchAllGroups)
|
||||||
|
- [ExtractContent](#ExtractContent)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -1729,3 +1730,33 @@ func main() {
|
|||||||
// [jane.doe@example.com jane.doe example com]
|
// [jane.doe@example.com jane.doe example com]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="ExtractContent">ExtractContent</span>
|
||||||
|
|
||||||
|
<p>提取两个标记之间的内容。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ExtractContent(s, start, end string) []string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:<span style="float:right;display:inline-block;">[Run](todo)</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
html := `<span>content1</span>aa<span>content2</span>bb<span>content1</span>`
|
||||||
|
|
||||||
|
result := strutil.ExtractContent(html, "<span>", "</span>")
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [content1 content2 content1]
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -27,7 +27,8 @@ import (
|
|||||||
- [Nor](#Nor)
|
- [Nor](#Nor)
|
||||||
- [Xnor](#Xnor)
|
- [Xnor](#Xnor)
|
||||||
- [Nand](#Nand)
|
- [Nand](#Nand)
|
||||||
- [TernaryOperator](#TernaryOperator)
|
- [Ternary](#Ternary)
|
||||||
|
- [TernaryOperator<sup>deprecated</sup>](#TernaryOperator)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -269,9 +270,45 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Ternary">Ternary</span>
|
||||||
|
<p>Checks the value of param `isTrue`, if true return ifValue else return elseValue</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Ternary[T, U any](isTrue T, ifValue U, elseValue U) U
|
||||||
|
```
|
||||||
|
<b>Example:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/ElllPZY0guT)</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/condition"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
conditionTrue := 2 > 1
|
||||||
|
result1 := condition.Ternary(conditionTrue, 0, 1)
|
||||||
|
|
||||||
|
conditionFalse := 2 > 3
|
||||||
|
result2 := condition.Ternary(conditionFalse, 0, 1)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 0
|
||||||
|
// 1
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="TernaryOperator">TernaryOperator</span>
|
### <span id="TernaryOperator">TernaryOperator</span>
|
||||||
<p>Checks the value of param `isTrue`, if true return ifValue else return elseValue</p>
|
<p>Checks the value of param `isTrue`, if true return ifValue else return elseValue</p>
|
||||||
|
|
||||||
|
> ⚠️ This function is deprecated. use `Ternary` instead.
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -307,4 +344,3 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -68,6 +68,8 @@ import (
|
|||||||
- [Rotate](#Rotate)
|
- [Rotate](#Rotate)
|
||||||
- [TemplateReplace](#TemplateReplace)
|
- [TemplateReplace](#TemplateReplace)
|
||||||
- [RegexMatchAllGroups](#RegexMatchAllGroups)
|
- [RegexMatchAllGroups](#RegexMatchAllGroups)
|
||||||
|
- [ExtractContent](#RegexMatchAllGroups)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -1708,7 +1710,7 @@ func main() {
|
|||||||
func RegexMatchAllGroups(pattern, str string) [][]string
|
func RegexMatchAllGroups(pattern, str string) [][]string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/JZiu0RXpgN-)</span></b>
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/JZiu0RXpgN-)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
@@ -1730,3 +1732,33 @@ func main() {
|
|||||||
// [jane.doe@example.com jane.doe example com]
|
// [jane.doe@example.com jane.doe example com]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="ExtractContent">ExtractContent</span>
|
||||||
|
|
||||||
|
<p>Extracts the content between the start and end strings in the source string.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ExtractContent(s, start, end string) []string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:<span style="float:right;display:inline-block;">[Run](todo)</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
html := `<span>content1</span>aa<span>content2</span>bb<span>content1</span>`
|
||||||
|
|
||||||
|
result := strutil.ExtractContent(html, "<span>", "</span>")
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [content1 content2 content1]
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -735,3 +735,24 @@ func RegexMatchAllGroups(pattern, str string) [][]string {
|
|||||||
matches := re.FindAllStringSubmatch(str, -1)
|
matches := re.FindAllStringSubmatch(str, -1)
|
||||||
return matches
|
return matches
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ExtractContent extracts the content between the start and end strings in the source string.
|
||||||
|
// Play: todo
|
||||||
|
func ExtractContent(s, start, end string) []string {
|
||||||
|
result := []string{}
|
||||||
|
|
||||||
|
for {
|
||||||
|
if _, after, ok := strings.Cut(s, start); ok {
|
||||||
|
if before, _, ok := strings.Cut(after, end); ok {
|
||||||
|
result = append(result, before)
|
||||||
|
s = after
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|||||||
@@ -753,3 +753,15 @@ func ExampleRegexMatchAllGroups() {
|
|||||||
// [john.doe@example.com john.doe example com]
|
// [john.doe@example.com john.doe example com]
|
||||||
// [jane.doe@example.com jane.doe example com]
|
// [jane.doe@example.com jane.doe example com]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleExtractContent() {
|
||||||
|
html := `<span>content1</span>aa<span>content2</span>bb<span>content1</span>`
|
||||||
|
|
||||||
|
result := ExtractContent(html, "<span>", "</span>")
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [content1 content2 content1]
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -853,3 +853,87 @@ func TestRegexMatchAllGroups(t *testing.T) {
|
|||||||
assert.Equal(tt.expected, result)
|
assert.Equal(tt.expected, result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestExtractContent(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
assert := internal.NewAssert(t, "TestExtractContent")
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
input string
|
||||||
|
start string
|
||||||
|
end string
|
||||||
|
expected []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Extract content between <tag> and </tag>",
|
||||||
|
input: "This is <tag>content1</tag> and <tag>content2</tag> and <tag>content3</tag>",
|
||||||
|
start: "<tag>",
|
||||||
|
end: "</tag>",
|
||||||
|
expected: []string{"content1", "content2", "content3"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "No tags in the string",
|
||||||
|
input: "This string has no tags",
|
||||||
|
start: "<tag>",
|
||||||
|
end: "</tag>",
|
||||||
|
expected: []string{},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Single tag pair",
|
||||||
|
input: "<tag>onlyContent</tag>",
|
||||||
|
start: "<tag>",
|
||||||
|
end: "</tag>",
|
||||||
|
expected: []string{"onlyContent"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Tags without end tag",
|
||||||
|
input: "This <tag>content without end tag",
|
||||||
|
start: "<tag>",
|
||||||
|
end: "</tag>",
|
||||||
|
expected: []string{},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Tags with nested content",
|
||||||
|
input: "<tag>content <nested>inner</nested> end</tag>",
|
||||||
|
start: "<tag>",
|
||||||
|
end: "</tag>",
|
||||||
|
expected: []string{"content <nested>inner</nested> end"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Edge case with empty string",
|
||||||
|
input: "",
|
||||||
|
start: "<tag>",
|
||||||
|
end: "</tag>",
|
||||||
|
expected: []string{},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Edge case with no start tag",
|
||||||
|
input: "content without start tag",
|
||||||
|
start: "<tag>",
|
||||||
|
end: "</tag>",
|
||||||
|
expected: []string{},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Edge case with no end tag",
|
||||||
|
input: "<tag>content without end tag",
|
||||||
|
start: "<tag>",
|
||||||
|
end: "</tag>",
|
||||||
|
expected: []string{},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Multiple consecutive tags",
|
||||||
|
input: "<tag>content1</tag><tag>content2</tag><tag>content3</tag>",
|
||||||
|
start: "<tag>",
|
||||||
|
end: "</tag>",
|
||||||
|
expected: []string{"content1", "content2", "content3"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
result := ExtractContent(tt.input, tt.start, tt.end)
|
||||||
|
assert.Equal(tt.expected, result)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user