diff --git a/condition/condition.go b/condition/condition.go index df5ae43..82db112 100644 --- a/condition/condition.go +++ b/condition/condition.go @@ -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. // 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) { 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 +// Deprecated: Use Ternary instead. +func TernaryOperator[T, U any](isTrue T, ifValue U, elseValue U) U { + return Ternary(isTrue, ifValue, elseValue) +} diff --git a/condition/condition_example_test.go b/condition/condition_example_test.go index f645d8c..80f3042 100644 --- a/condition/condition_example_test.go +++ b/condition/condition_example_test.go @@ -148,13 +148,13 @@ func ExampleNand() { // false } -func ExampleTernaryOperator() { +func ExampleTernary() { conditionTrue := 2 > 1 - result1 := TernaryOperator(conditionTrue, 0, 1) + result1 := Ternary(conditionTrue, 0, 1) fmt.Println(result1) conditionFalse := 2 > 3 - result2 := TernaryOperator(conditionFalse, 0, 1) + result2 := Ternary(conditionFalse, 0, 1) fmt.Println(result2) // Output: diff --git a/condition/condition_test.go b/condition/condition_test.go index b8446bc..45103ab 100644 --- a/condition/condition_test.go +++ b/condition/condition_test.go @@ -124,13 +124,13 @@ func TestNand(t *testing.T) { assert.Equal(false, Nand(1, 1)) } -func TestTernaryOperator(t *testing.T) { +func TestTernary(t *testing.T) { t.Parallel() - assert := internal.NewAssert(t, "TernaryOperator") + assert := internal.NewAssert(t, "TestTernary") trueValue := "1" falseValue := "0" - assert.Equal(trueValue, TernaryOperator(true, trueValue, falseValue)) + assert.Equal(trueValue, Ternary(true, trueValue, falseValue)) } diff --git a/docs/api/packages/condition.md b/docs/api/packages/condition.md index 5a370ca..9dfe5b6 100644 --- a/docs/api/packages/condition.md +++ b/docs/api/packages/condition.md @@ -27,7 +27,8 @@ import ( - [Nor](#Nor) - [Xnor](#Xnor) - [Nand](#Nand) -- [TernaryOperator](#TernaryOperator) +- [Ternary](#Ternary) +- [TernaryOperatordeprecated](#TernaryOperator)
@@ -257,9 +258,45 @@ func main() { } ``` +### Ternary +

三元运算符。

+ +函数签名: + +```go +func Ternary[T, U any](isTrue T, ifValue U, elseValue U) U +``` +示例:[运行](https://go.dev/play/p/ElllPZY0guT) + +```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 +} +``` + ### TernaryOperator

三元运算符

+> ⚠️ 本函数已弃用,使用`Ternary`代替。 + 函数签名: ```go diff --git a/docs/en/api/packages/condition.md b/docs/en/api/packages/condition.md index 9421248..1c3af32 100644 --- a/docs/en/api/packages/condition.md +++ b/docs/en/api/packages/condition.md @@ -27,7 +27,8 @@ import ( - [Nor](#Nor) - [Xnor](#Xnor) - [Nand](#Nand) -- [TernaryOperator](#TernaryOperator) +- [Ternary](#Ternary) +- [TernaryOperatordeprecated](#TernaryOperator)
@@ -269,9 +270,45 @@ func main() { +### Ternary +

Checks the value of param `isTrue`, if true return ifValue else return elseValue

+ +Signature: + +```go +func Ternary[T, U any](isTrue T, ifValue U, elseValue U) U +``` +Example:[运行](https://go.dev/play/p/ElllPZY0guT) + +```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 +} +``` + ### TernaryOperator

Checks the value of param `isTrue`, if true return ifValue else return elseValue

+> ⚠️ This function is deprecated. use `Ternary` instead. + Signature: ```go @@ -307,4 +344,3 @@ func main() { -