mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
feat: add Ternary
This commit is contained in:
@@ -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
|
||||||
|
|||||||
@@ -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() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user