-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconcat.go
47 lines (42 loc) · 1.26 KB
/
concat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package slices
import (
"reflect"
"github.com/golodash/godash/internal"
)
// Creates a new slice concatenating slice with other one.
//
// Complexity: O(n)
//
// n = number of 'values' length
func Concat(slice, values interface{}) interface{} {
if !internal.SliceCheck(slice) {
panic("passed 'slice' variable is not slice type")
}
valuesValue := reflect.ValueOf(values)
sliceValue := reflect.ValueOf(slice)
sliceType := reflect.TypeOf(slice)
for i := 0; i < valuesValue.Len(); i++ {
item := reflect.ValueOf(valuesValue.Index(i).Interface())
if !item.IsValid() {
continue
}
if item.Kind() == reflect.Slice {
if sliceType.Kind() == item.Kind() || sliceType.Elem().Kind() == reflect.Interface || item.Elem().Kind() == reflect.Interface {
for j := 0; j < item.Len(); j++ {
innerItem := reflect.ValueOf(item.Index(j).Interface())
if !innerItem.IsValid() {
continue
}
if innerItem.Kind() == sliceType.Elem().Kind() || sliceType.Elem().Kind() == reflect.Interface {
sliceValue = reflect.Append(sliceValue, innerItem)
}
}
}
} else {
if item.Kind() == sliceType.Elem().Kind() || sliceType.Elem().Kind() == reflect.Interface {
sliceValue = reflect.Append(sliceValue, item)
}
}
}
return sliceValue.Interface()
}