mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
TernaryOperator Reduce if else operations (#56)
* feat:(slice add AppendIfAbsent function) * feat:(slice add TernaryOperator function) Co-authored-by: george.zheng <george.zheng@ambergroup.io>
This commit is contained in:
@@ -0,0 +1,10 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
// TernaryOperator if true return trueValue else return falseValue
|
||||||
|
func TernaryOperator[T any](isTrue bool, trueValue T, falseValue T) T {
|
||||||
|
if isTrue {
|
||||||
|
return trueValue
|
||||||
|
} else {
|
||||||
|
return falseValue
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/duke-git/lancet/v2/internal"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTernaryOperator(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TernaryOperator")
|
||||||
|
trueValue := "1"
|
||||||
|
falseValue := "0"
|
||||||
|
|
||||||
|
assert.Equal(trueValue, TernaryOperator(true, trueValue, falseValue))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user