From 8c2f92f202aa40392ef14c26791a050c422d7b57 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Wed, 8 Jun 2022 09:14:03 +0200 Subject: [PATCH] ternary op function (#29) Co-authored-by: Ruidy --- Makefile | 7 ++++--- ternary.go | 8 ++++++++ ternary_test.go | 23 +++++++++++++++++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 ternary.go create mode 100644 ternary_test.go diff --git a/Makefile b/Makefile index 3de24bd..074cbde 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,12 @@ -TEST = "go test ./... -coverpkg=./... -coverprofile coverage.out -covermode=count; go tool cover -func coverage.out; rm coverage.out" +TEST=go test ./... +COVER=-coverpkg=./... -coverprofile cov.out -covermode=count; go tool cover -func cov.out; rm cov.out IMAGE=underscore build: docker build -t $(IMAGE):latest . test: build - docker run --name $(IMAGE) --rm -i -t $(IMAGE) sh -c $(TEST) + docker run --name $(IMAGE) --rm -i -t $(IMAGE) sh -c "$(TEST) $(COVER)" scan: trivy --cache-dir .trivycache/ image --exit-code 0 --no-progress --severity CRITICAL $(IMAGE) @@ -17,4 +18,4 @@ docs: cd docs && hugo server -D build-docs: - cd docs && hugo --gc --minify \ No newline at end of file + cd docs && hugo --gc --minify diff --git a/ternary.go b/ternary.go new file mode 100644 index 0000000..01d256d --- /dev/null +++ b/ternary.go @@ -0,0 +1,8 @@ +package underscore + +func Ternary[T any](condition bool, pos, neg T) T { + if condition { + return pos + } + return neg +} diff --git a/ternary_test.go b/ternary_test.go new file mode 100644 index 0000000..54b259e --- /dev/null +++ b/ternary_test.go @@ -0,0 +1,23 @@ +package underscore_test + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + u "github.com/rjNemo/underscore" +) + +func TestTernary(t *testing.T) { + tests := []struct { + condition bool + want string + }{ + {true, "foo"}, + {false, "bar"}, + } + + for _, tc := range tests { + assert.Equal(t, u.Ternary(tc.condition, "foo", "bar"), tc.want) + } +}