Added FilterMap
This commit is contained in:
parent
f7abe7c01f
commit
e33476ea5c
2 changed files with 42 additions and 1 deletions
|
|
@ -1,10 +1,12 @@
|
|||
package moslice_test
|
||||
|
||||
import (
|
||||
"lmika.dev/pkg/modash/moslice"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"lmika.dev/pkg/modash/moslice"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
|
|
@ -27,3 +29,23 @@ func TestFilter(t *testing.T) {
|
|||
assert.Equal(t, []int{}, moslice.Filter([]int{}, func(x int) bool { return x%2 == 0 }))
|
||||
})
|
||||
}
|
||||
|
||||
func TestFilterMap(t *testing.T) {
|
||||
var (
|
||||
ints = []int{1, 2, 3, 4, 5}
|
||||
strs = []string{"foo", "bar", "baz"}
|
||||
)
|
||||
|
||||
t.Run("should filter items matching the predicate", func(t *testing.T) {
|
||||
assert.Equal(t, []string{"2", "4"}, moslice.FilterMap(ints, func(x int) (string, bool) { return fmt.Sprint(x), x%2 == 0 }))
|
||||
assert.Equal(t, []int{3, 3}, moslice.FilterMap(strs, func(x string) (int, bool) { return len(x), strings.Contains(x, "b") }))
|
||||
})
|
||||
|
||||
t.Run("should moslice nil if the passed in slice is nil", func(t *testing.T) {
|
||||
assert.Nil(t, moslice.FilterMap(nil, func(x int) (string, bool) { return fmt.Sprint(x), x%2 == 0 }))
|
||||
})
|
||||
|
||||
t.Run("should return empty slice if the passed in slice is empty slice", func(t *testing.T) {
|
||||
assert.Equal(t, []string{}, moslice.FilterMap([]int{}, func(x int) (string, bool) { return fmt.Sprint(x), x%2 == 0 }))
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue