Skip to content

Commit

Permalink
feat: adding WithoutEmpty
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed Jul 4, 2022
1 parent 07fddc1 commit 198c67f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

@samber: I sometimes forget to update this file. Ping me on [Twitter](https://twitter.com/samuelberthe) or open an issue in case of error. We need to keep a clear changelog for easier lib upgrade.

## 1.24.0 (2022-07-04)

Adding:

- lo.Without
- lo.WithoutEmpty

## 1.23.0 (2022-07-04)

Adding:
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ Supported intersection helpers:
- Difference
- Union
- Without
- WithoutEmpty

Supported search helpers:

Expand Down Expand Up @@ -1101,6 +1102,15 @@ subset := lo.Without[int]([]int{0, 2, 10}, 0, 1, 2, 3, 4, 5)
// []int{10}
```

### WithoutEmpty

Returns slice excluding empty values.

```go
subset := lo.WithoutEmpty[int]([]int{0, 2, 10})
// []int{2, 10}
```

### IndexOf

Returns the index at which the first occurrence of a value is found in an array or return -1 if the value cannot be found.
Expand Down
14 changes: 14 additions & 0 deletions intersect.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,17 @@ func Without[T comparable](collection []T, exclude ...T) []T {
}
return result
}

// WithoutEmpty returns slice excluding empty values.
func WithoutEmpty[T comparable](collection []T) []T {
var empty T

result := make([]T, 0, len(collection))
for _, e := range collection {
if e != empty {
result = append(result, e)
}
}

return result
}
10 changes: 10 additions & 0 deletions intersect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,13 @@ func TestWithout(t *testing.T) {
is.Equal(result4, []int{})
is.Equal(result5, []int{})
}

func TestWithoutEmpty(t *testing.T) {
is := assert.New(t)
result1 := WithoutEmpty([]int{0, 1, 2})
result2 := WithoutEmpty([]int{1, 2})
result3 := WithoutEmpty([]int{})
is.Equal(result1, []int{1, 2})
is.Equal(result2, []int{1, 2})
is.Equal(result3, []int{})
}

0 comments on commit 198c67f

Please sign in to comment.