mirror of
https://github.com/rjNemo/underscore
synced 2026-06-06 02:26:42 +00:00
ternary op function (#29)
Co-authored-by: Ruidy <rnemausat@newstore.com>
This commit is contained in:
parent
d1da6fbab0
commit
8c2f92f202
3 changed files with 35 additions and 3 deletions
5
Makefile
5
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
|
IMAGE=underscore
|
||||||
|
|
||||||
build:
|
build:
|
||||||
docker build -t $(IMAGE):latest .
|
docker build -t $(IMAGE):latest .
|
||||||
|
|
||||||
test: build
|
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:
|
scan:
|
||||||
trivy --cache-dir .trivycache/ image --exit-code 0 --no-progress --severity CRITICAL $(IMAGE)
|
trivy --cache-dir .trivycache/ image --exit-code 0 --no-progress --severity CRITICAL $(IMAGE)
|
||||||
|
|
|
||||||
8
ternary.go
Normal file
8
ternary.go
Normal 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
23
ternary_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue