diff --git a/common/common.go b/common/common.go new file mode 100644 index 0000000..74f9975 --- /dev/null +++ b/common/common.go @@ -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 + } +} diff --git a/common/common_test.go b/common/common_test.go new file mode 100644 index 0000000..71a02bf --- /dev/null +++ b/common/common_test.go @@ -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)) +}