Skip to content

Commit

Permalink
feat: adding lo.IsNotEmpty
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed Jul 4, 2022
1 parent 92938ec commit d61070e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ Type manipulation helpers:
- [FromAnySlice](#fromanyslice)
- [Empty](#empty)
- [IsEmpty](#isempty)
- [IsNotEmpty](#isnotempty)
- [Coalesce](#coalesce)

Function helpers:
Expand Down Expand Up @@ -1558,6 +1559,31 @@ lo.IsEmpty[test](test{foobar: "foobar"})
// false
```

### IsNotEmpty

Returns true if argument is a zero value.

```go
lo.IsNotEmpty[int](0)
// false
lo.IsNotEmpty[int](42)
// true

lo.IsNotEmpty[string]("")
// false
lo.IsNotEmpty[bool]("foobar")
// true

type test struct {
foobar string
}

lo.IsNotEmpty[test](test{foobar: ""})
// false
lo.IsNotEmpty[test](test{foobar: "foobar"})
// true
```

### Coalesce

Returns the first non-empty arguments. Arguments must be comparable.
Expand Down
6 changes: 6 additions & 0 deletions type_manipulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ func IsEmpty[T comparable](v T) bool {
return zero == v
}

// IsNotEmpty returns true if argument is not a zero value.
func IsNotEmpty[T comparable](v T) bool {
var zero T
return zero != v
}

// Coalesce returns the first non-empty arguments. Arguments must be comparable.
func Coalesce[T comparable](v ...T) (result T, ok bool) {
for _, e := range v {
Expand Down
16 changes: 16 additions & 0 deletions type_manipulation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,22 @@ func TestIsEmpty(t *testing.T) {
is.False(IsEmpty[test](test{foobar: "foo"}))
}

func TestIsNotEmpty(t *testing.T) {
is := assert.New(t)

//nolint:unused
type test struct {
foobar string
}

is.False(IsNotEmpty[string](""))
is.True(IsNotEmpty[string]("foo"))
is.False(IsNotEmpty[int64](0))
is.True(IsNotEmpty[int64](42))
is.False(IsNotEmpty[test](test{foobar: ""}))
is.True(IsNotEmpty[test](test{foobar: "foo"}))
}

func TestCoalesce(t *testing.T) {
is := assert.New(t)

Expand Down

0 comments on commit d61070e

Please sign in to comment.