Skip to content

Commit

Permalink
feat: add and, or operators
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Sep 30, 2024
1 parent 9371a73 commit 4068e09
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
11 changes: 11 additions & 0 deletions utilx/utilx.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ func NotEmpty[T comparable](value T) bool {
return !Empty(value)
}

// And works similar to "&&" in other languages.
func And[T comparable](a, b T) T {
var c T

if a == c {
return a
}

return b
}

// Or works similar to "||" in other languages.
func Or[T comparable](a, b T) T {
var c T
Expand Down
62 changes: 62 additions & 0 deletions utilx/utilx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,65 @@ func TestIfElse(t *testing.T) {
require.Equal(t, test.expected, got)
}
}

func TestOr(t *testing.T) {
tests := []struct {
a int
b int
expected int
}{
{1, 2, 1},
{0, 2, 2},
}

for _, test := range tests {
got := utilx.Or(test.a, test.b)
require.Equal(t, test.expected, got)
}
}

func TestAnd(t *testing.T) {
tests := []struct {
a int
b int
expected int
}{
{1, 2, 2},
{0, 2, 0},
}

for _, test := range tests {
got := utilx.And(test.a, test.b)
require.Equal(t, test.expected, got)
}
}

func TestNotEmpty(t *testing.T) {
tests := []struct {
value int
expected bool
}{
{0, false},
{1, true},
}

for _, test := range tests {
got := utilx.NotEmpty(test.value)
require.Equal(t, test.expected, got)
}
}

func TestEmpty(t *testing.T) {
tests := []struct {
value int
expected bool
}{
{0, true},
{1, false},
}

for _, test := range tests {
got := utilx.Empty(test.value)
require.Equal(t, test.expected, got)
}
}

0 comments on commit 4068e09

Please sign in to comment.