commit c0d838e5400977c20b35acb8607f0dcc44eb4eb1
parent 1cd349beccd0cdb41fc333f5ee0ee4e1b4553bfd
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Tue, 15 Nov 2022 22:54:48 -0500
Add generic ternary utils functions
Diffstat:
1 file changed, 12 insertions(+), 0 deletions(-)
diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go
@@ -873,3 +873,15 @@ func Must[T any](v T, err error) T {
func ValidateRuneLength(str string, min, max int) bool {
return govalidator.RuneLength(str, strconv.Itoa(min), strconv.Itoa(max))
}
+
+func Ternary[T any](predicate bool, a, b T) T {
+ if predicate {
+ return a
+ }
+ return b
+}
+
+func TernaryOrZero[T any](predicate bool, a T) T {
+ var zero T
+ return Ternary(predicate, a, zero)
+}