From 72e1d92fa19c0668e3abc4ddd38e5ee9ffc1da01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E4=B8=80=E8=AF=BA=E5=A5=B9=E7=88=B8?= <649654639@qq.com> Date: Sat, 27 Aug 2022 19:12:03 +0800 Subject: [PATCH 1/2] TernaryOperator Reduce if else operations (#56) * feat:(slice add AppendIfAbsent function) * feat:(slice add TernaryOperator function) Co-authored-by: george.zheng --- common/common.go | 10 ++++++++++ common/common_test.go | 14 ++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 common/common.go create mode 100644 common/common_test.go 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)) +} From 04058dd7da7d7dc468c794367721ffb8773f3bc1 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Sat, 27 Aug 2022 19:19:34 +0800 Subject: [PATCH 2/2] refactor: change common package to condition package --- common/common.go | 10 ---------- condition/condition.go | 14 ++++++++++++++ .../common_test.go => condition/condition_test.go | 5 +++-- 3 files changed, 17 insertions(+), 12 deletions(-) delete mode 100644 common/common.go create mode 100644 condition/condition.go rename common/common_test.go => condition/condition_test.go (93%) diff --git a/common/common.go b/common/common.go deleted file mode 100644 index 74f9975..0000000 --- a/common/common.go +++ /dev/null @@ -1,10 +0,0 @@ -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/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/common/common_test.go b/condition/condition_test.go similarity index 93% rename from common/common_test.go rename to condition/condition_test.go index 71a02bf..50b454b 100644 --- a/common/common_test.go +++ b/condition/condition_test.go @@ -1,8 +1,9 @@ -package common +package condition import ( - "github.com/duke-git/lancet/v2/internal" "testing" + + "github.com/duke-git/lancet/v2/internal" ) func TestTernaryOperator(t *testing.T) {