ternary op function (#29)

Co-authored-by: Ruidy <rnemausat@newstore.com>
This commit is contained in:
Ruidy 2022-06-08 09:14:03 +02:00 committed by GitHub
parent d1da6fbab0
commit 8c2f92f202
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 3 deletions

View file

@ -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
cd docs && hugo --gc --minify

8
ternary.go Normal file
View file

@ -0,0 +1,8 @@
package underscore
func Ternary[T any](condition bool, pos, neg T) T {
if condition {
return pos
}
return neg
}

23
ternary_test.go Normal file
View file

@ -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)
}
}