feat: 工具实现整合 lancet 库
This commit is contained in:
+20
-68
@@ -9,48 +9,37 @@ package op_array
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"github.com/duke-git/lancet/v2/convertor"
|
||||
"github.com/duke-git/lancet/v2/slice"
|
||||
)
|
||||
|
||||
// ToMap 数组转map
|
||||
func ToMap[Key comparable, Value any](dataList []Value, keyFormat func(item Value) Key) map[Key]Value {
|
||||
res := make(map[Key]Value)
|
||||
for _, item := range dataList {
|
||||
key := keyFormat(item)
|
||||
res[key] = item
|
||||
}
|
||||
return res
|
||||
return convertor.ToMap(dataList, func(item Value) (Key, Value) {
|
||||
return keyFormat(item), item
|
||||
})
|
||||
}
|
||||
|
||||
// ToCustomMap 数组转map
|
||||
func ToCustomMap[Key comparable, Value any, CustomValue any](dataList []Value, keyFormat func(item Value) Key, formatCustomValue func(item Value) CustomValue) map[Key]CustomValue {
|
||||
res := make(map[Key]CustomValue)
|
||||
for _, item := range dataList {
|
||||
key := keyFormat(item)
|
||||
res[key] = formatCustomValue(item)
|
||||
}
|
||||
return res
|
||||
return convertor.ToMap(dataList, func(item Value) (Key, CustomValue) {
|
||||
return keyFormat(item), formatCustomValue(item)
|
||||
})
|
||||
}
|
||||
|
||||
// ExtractField 提取数组指定字段, 并构建成一个新的数组
|
||||
func ExtractField[FieldValue any, Value any](dataList []Value, fieldValue func(item Value) FieldValue) []FieldValue {
|
||||
res := make([]FieldValue, 0)
|
||||
for _, item := range dataList {
|
||||
v := fieldValue(item)
|
||||
res = append(res, v)
|
||||
}
|
||||
return res
|
||||
return slice.Map(dataList, func(_ int, item Value) FieldValue {
|
||||
return fieldValue(item)
|
||||
})
|
||||
}
|
||||
|
||||
// Filter 过滤列表数据
|
||||
func Filter[Value any](dataList []Value, filterValue func(item Value) bool) []Value {
|
||||
res := make([]Value, 0)
|
||||
for _, item := range dataList {
|
||||
if filterValue(item) {
|
||||
res = append(res, item)
|
||||
}
|
||||
}
|
||||
return res
|
||||
return slice.Filter(dataList, func(_ int, item Value) bool {
|
||||
return filterValue(item)
|
||||
})
|
||||
}
|
||||
|
||||
// Group 按照指定字段进行分组
|
||||
@@ -74,12 +63,9 @@ func Group[Key comparable, Value any](dataList []Value, keyFormat func(item Valu
|
||||
|
||||
// GroupToMap 数组数据按照指定 key 进行分组
|
||||
func GroupToMap[Key comparable, Value any](dataList []Value, keyFormat func(item Value) Key) map[Key][]Value {
|
||||
dataTable := make(map[Key][]Value)
|
||||
for _, item := range dataList {
|
||||
key := keyFormat(item)
|
||||
dataTable[key] = append(dataTable[key], item)
|
||||
}
|
||||
return dataTable
|
||||
return slice.GroupWith(dataList, func(item Value) Key {
|
||||
return keyFormat(item)
|
||||
})
|
||||
}
|
||||
|
||||
// TreeItem 数据结构
|
||||
@@ -126,44 +112,10 @@ func Tree[ID comparable, Value any](dataList []Value, formatParentID func(v Valu
|
||||
|
||||
// DeepClone 深度克隆一个数组, 每一个元素的地址均重新分配
|
||||
func DeepClone[Value any](dataList []Value) []Value {
|
||||
v := *new(Value)
|
||||
vType := reflect.TypeOf(v)
|
||||
res := make([]Value, len(dataList))
|
||||
for i := 0; i < len(dataList); i++ {
|
||||
newV := new(Value)
|
||||
switch vType.Kind() {
|
||||
case reflect.Ptr:
|
||||
// 解引用, 并单独读取值后重新分配地址
|
||||
copyV := reflect.New(vType.Elem()).Elem()
|
||||
copyV.Set(reflect.ValueOf(dataList[i]).Elem())
|
||||
*newV = copyV.Addr().Interface().(Value)
|
||||
case reflect.Map:
|
||||
mapVal := reflect.ValueOf(dataList[i])
|
||||
mapKeyList := mapVal.MapKeys()
|
||||
newMap := reflect.MakeMap(vType)
|
||||
for _, key := range mapKeyList {
|
||||
// 通过键获取对应的值
|
||||
value := mapVal.MapIndex(key)
|
||||
newMap.SetMapIndex(key, value)
|
||||
}
|
||||
*newV = newMap.Interface().(Value)
|
||||
default:
|
||||
*newV = dataList[i]
|
||||
}
|
||||
res[i] = *newV
|
||||
}
|
||||
return res
|
||||
return convertor.DeepClone(dataList)
|
||||
}
|
||||
|
||||
// Shard 将数组按照指定数量进行分片
|
||||
func Shard[Value any](dataList []Value, itemShardCount int) [][]Value {
|
||||
res := make([][]Value, 0)
|
||||
for i := 0; i < len(dataList); i += itemShardCount {
|
||||
tmpList := make([]Value, 0)
|
||||
for j := 0; j < itemShardCount; j++ {
|
||||
tmpList = append(tmpList, dataList[i+j])
|
||||
}
|
||||
res = append(res, tmpList)
|
||||
}
|
||||
return res
|
||||
return slice.Chunk(dataList, itemShardCount)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user