diff --git a/condition/condition.go b/condition/condition.go new file mode 100644 index 0000000..f1b3b7e --- /dev/null +++ b/condition/condition.go @@ -0,0 +1,14 @@ +// Copyright 2021 dudaodong@gmail.com. All rights reserved. +// Use of this source code is governed by MIT license + +// Package condition contains some functions for conditional judgment. eg. And, Or, TernaryOperator ... +package condition + +// 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/condition/condition_test.go b/condition/condition_test.go new file mode 100644 index 0000000..50b454b --- /dev/null +++ b/condition/condition_test.go @@ -0,0 +1,15 @@ +package condition + +import ( + "testing" + + "github.com/duke-git/lancet/v2/internal" +) + +func TestTernaryOperator(t *testing.T) { + assert := internal.NewAssert(t, "TernaryOperator") + trueValue := "1" + falseValue := "0" + + assert.Equal(trueValue, TernaryOperator(true, trueValue, falseValue)) +}