From c2746942ef205307235f5c542c6c86e0b568375e Mon Sep 17 00:00:00 2001 From: Florent Messa Date: Tue, 6 Jul 2021 08:54:54 +0200 Subject: [PATCH] fix: Zip should panic if args are not slice/array (fixes #123) --- zip.go | 24 +++++++++++------------- zip_test.go | 7 +++---- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/zip.go b/zip.go index f9056bc..dcd3828 100644 --- a/zip.go +++ b/zip.go @@ -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 { diff --git a/zip_test.go b/zip_test.go index 6dd9be5..3cccf68 100644 --- a/zip_test.go +++ b/zip_test.go @@ -1,8 +1,9 @@ package funk import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestZipEmptyResult(t *testing.T) { @@ -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) {