mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-13 09:12:28 +08:00
feat: add ToSlice and ToSlicePointer and AppendIfAbsent
This commit is contained in:
@@ -641,7 +641,6 @@ func InsertByIndex(slice interface{}, index int, value interface{}) (interface{}
|
|||||||
return slice, errors.New("InvalidSliceIndex")
|
return slice, errors.New("InvalidSliceIndex")
|
||||||
}
|
}
|
||||||
|
|
||||||
// value is slice
|
|
||||||
vv := reflect.ValueOf(value)
|
vv := reflect.ValueOf(value)
|
||||||
if vv.Kind() == reflect.Slice {
|
if vv.Kind() == reflect.Slice {
|
||||||
if reflect.TypeOf(slice).Elem() != reflect.TypeOf(value).Elem() {
|
if reflect.TypeOf(slice).Elem() != reflect.TypeOf(value).Elem() {
|
||||||
@@ -651,7 +650,6 @@ func InsertByIndex(slice interface{}, index int, value interface{}) (interface{}
|
|||||||
return v.Interface(), nil
|
return v.Interface(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// value is not slice
|
|
||||||
if reflect.TypeOf(slice).Elem() != reflect.TypeOf(value) {
|
if reflect.TypeOf(slice).Elem() != reflect.TypeOf(value) {
|
||||||
return slice, errors.New("InvalidValueType")
|
return slice, errors.New("InvalidValueType")
|
||||||
}
|
}
|
||||||
@@ -709,21 +707,8 @@ func Unique(slice interface{}) interface{} {
|
|||||||
for i := 0; i < len(temp); i++ {
|
for i := 0; i < len(temp); i++ {
|
||||||
res.Index(i).Set(reflect.ValueOf(temp[i]))
|
res.Index(i).Set(reflect.ValueOf(temp[i]))
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.Interface()
|
return res.Interface()
|
||||||
|
|
||||||
// if use map filter, the result slice element order is random, not same as origin slice
|
|
||||||
//mp := make(map[interface{}]bool)
|
|
||||||
//for i := 0; i < sv.Len(); i++ {
|
|
||||||
// v := sv.Index(i).Interface()
|
|
||||||
// mp[v] = true
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//var res []interface{}
|
|
||||||
//for k := range mp {
|
|
||||||
// res = append(res, mp[k])
|
|
||||||
//}
|
|
||||||
//return res
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// UniqueBy call iteratee func with every item of slice, then remove duplicated.
|
// UniqueBy call iteratee func with every item of slice, then remove duplicated.
|
||||||
@@ -732,11 +717,6 @@ func UniqueBy(slice, iteratee interface{}) interface{} {
|
|||||||
sv := sliceValue(slice)
|
sv := sliceValue(slice)
|
||||||
fn := functionValue(iteratee)
|
fn := functionValue(iteratee)
|
||||||
|
|
||||||
// elemType := sv.Type().Elem()
|
|
||||||
// if !checkCallbackFuncSignature2(fn, elemType, elemType) {
|
|
||||||
// panic("iteratee function signature should be of type func(" + elemType.String() + ")" + elemType.String())
|
|
||||||
// }
|
|
||||||
|
|
||||||
if sv.Len() == 0 {
|
if sv.Len() == 0 {
|
||||||
return slice
|
return slice
|
||||||
}
|
}
|
||||||
@@ -955,3 +935,36 @@ func LastIndexOf(slice, value interface{}) int {
|
|||||||
|
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToSlicePointer returns a pointer to the slices of a variable parameter transformation
|
||||||
|
func ToSlicePointer(value ...interface{}) []*interface{} {
|
||||||
|
out := make([]*interface{}, len(value))
|
||||||
|
for i := range value {
|
||||||
|
out[i] = &value[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToSlice returns a slices of a variable parameter transformation
|
||||||
|
func ToSlice(value ...interface{}) interface{} {
|
||||||
|
rv := reflect.ValueOf(value)
|
||||||
|
out := reflect.MakeSlice(rv.Type(), len(value), len(value))
|
||||||
|
for i := range value {
|
||||||
|
out.Index(i).Set(reflect.ValueOf(value[i]))
|
||||||
|
}
|
||||||
|
return out.Interface()
|
||||||
|
}
|
||||||
|
|
||||||
|
// AppendIfAbsent only absent append the value
|
||||||
|
func AppendIfAbsent(slice interface{}, value interface{}) interface{} {
|
||||||
|
sv := sliceValue(slice)
|
||||||
|
out := reflect.MakeSlice(sv.Type(), sv.Len(), sv.Len())
|
||||||
|
for i := 0; i < sv.Len(); i++ {
|
||||||
|
out.Index(i).Set(sv.Index(i))
|
||||||
|
}
|
||||||
|
if !Contain(slice, value) {
|
||||||
|
out = reflect.Append(out, reflect.ValueOf(value))
|
||||||
|
}
|
||||||
|
return out.Interface()
|
||||||
|
}
|
||||||
|
|||||||
@@ -592,3 +592,35 @@ func TestLastIndexOf(t *testing.T) {
|
|||||||
assert.Equal(1, LastIndexOf(arr, "a"))
|
assert.Equal(1, LastIndexOf(arr, "a"))
|
||||||
assert.Equal(-1, LastIndexOf(arr, "d"))
|
assert.Equal(-1, LastIndexOf(arr, "d"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestToSlice(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestToSlice")
|
||||||
|
|
||||||
|
str1 := "a"
|
||||||
|
str2 := "b"
|
||||||
|
out1 := ToSlice(str1)
|
||||||
|
out2 := ToSlice(str1, str2)
|
||||||
|
|
||||||
|
assert.Equal([]string{"a"}, StringSlice(out1))
|
||||||
|
assert.Equal([]string{"a", "b"}, StringSlice(out2))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToSlicePointer(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestToSlicePointer")
|
||||||
|
|
||||||
|
var str1 interface{}
|
||||||
|
str1 = "a"
|
||||||
|
var str2 interface{}
|
||||||
|
str2 = "b"
|
||||||
|
|
||||||
|
assert.Equal([]*interface{}{&str1}, ToSlicePointer(str1))
|
||||||
|
assert.Equal([]*interface{}{&str1, &str2}, ToSlicePointer(str1, str2))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToAppendIfAbsent(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestToAppendIfAbsent")
|
||||||
|
|
||||||
|
str1 := []string{"a", "b"}
|
||||||
|
assert.Equal([]string{"a", "b"}, AppendIfAbsent(str1, "a"))
|
||||||
|
assert.Equal([]string{"a", "b", "c"}, AppendIfAbsent(str1, "c"))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user