Skip to content

Commit

Permalink
fix: Zip should panic if args are not slice/array (fixes #123)
Browse files Browse the repository at this point in the history
  • Loading branch information
thoas committed Jul 6, 2021
1 parent 74c18f1 commit c274694
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
24 changes: 11 additions & 13 deletions zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,19 @@ type Tuple struct {
// from each of the input iterables. The returned list is truncated in length
// to the length of the shortest input iterable.
func Zip(slice1 interface{}, slice2 interface{}) []Tuple {
inValue1 := reflect.ValueOf(slice1)
inValue2 := reflect.ValueOf(slice2)
kind1 := inValue1.Type().Kind()
kind2 := inValue2.Type().Kind()

result := []Tuple{}
for _, kind := range []reflect.Kind{kind1, kind2} {
if kind != reflect.Slice && kind != reflect.Array {
return result
}
if !IsCollection(slice1) || !IsCollection(slice2) {
panic("First parameter must be a collection")
}

var minLength int
length1 := inValue1.Len()
length2 := inValue2.Len()
var (
minLength int
inValue1 = reflect.ValueOf(slice1)
inValue2 = reflect.ValueOf(slice2)
result = []Tuple{}
length1 = inValue1.Len()
length2 = inValue2.Len()
)

if length1 <= length2 {
minLength = length1
} else {
Expand Down
7 changes: 3 additions & 4 deletions zip_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package funk

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestZipEmptyResult(t *testing.T) {
Expand All @@ -11,9 +12,7 @@ func TestZipEmptyResult(t *testing.T) {
emptySlice := []int{}

t.Run("NonSliceOrArray", func(t *testing.T) {
expected := []Tuple{}
result := Zip(map1, array1)
assert.Equal(t, result, expected)
assert.Panics(t, func() { Zip(map1, array1) }, "It should panic")
})

t.Run("ZerosSized", func(t *testing.T) {
Expand Down

0 comments on commit c274694

Please sign in to comment.