|
|
|
@ -19,7 +19,6 @@ import (
|
|
|
|
|
// @param fn: 业务方编写转换逻辑,内部对原始切片的元素逐个回调给业务方,并通过回调返回值组成转换后的字符串切片
|
|
|
|
|
//
|
|
|
|
|
// @return ret: 转换后的字符串切片
|
|
|
|
|
//
|
|
|
|
|
func Slice2Strings(a interface{}, fn func(originItem interface{}) string) (ret []string) {
|
|
|
|
|
IterateInterfaceAsSlice(a, func(iterItem interface{}) bool {
|
|
|
|
|
ret = append(ret, fn(iterItem))
|
|
|
|
@ -29,7 +28,6 @@ func Slice2Strings(a interface{}, fn func(originItem interface{}) string) (ret [
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Slice2Times 将任意类型切片转换为时间切片
|
|
|
|
|
//
|
|
|
|
|
func Slice2Times(a interface{}, fn func(originItem interface{}) time.Time) (ret []time.Time) {
|
|
|
|
|
IterateInterfaceAsSlice(a, func(iterItem interface{}) bool {
|
|
|
|
|
ret = append(ret, fn(iterItem))
|
|
|
|
@ -41,7 +39,6 @@ func Slice2Times(a interface{}, fn func(originItem interface{}) time.Time) (ret
|
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
// SliceUniqueCount 遍历切片`a`,逐个调用`fn`转换为string, 并将所有元素归类计数
|
|
|
|
|
//
|
|
|
|
|
func SliceUniqueCount(a interface{}, fn func(originItem interface{}) string) (ret map[string]int) {
|
|
|
|
|
ret = make(map[string]int)
|
|
|
|
|
IterateInterfaceAsSlice(a, func(iterItem interface{}) bool {
|
|
|
|
@ -55,9 +52,9 @@ func SliceUniqueCount(a interface{}, fn func(originItem interface{}) string) (re
|
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
// SliceLimit 取切片前`PrefixNumLimit`个元素和后`SuffixNumLimit`个元素,通过`cb`回调给业务方
|
|
|
|
|
// 注意,内部会处理`PrefixNumLimit`或`SuffixNumLimit`过大的情况
|
|
|
|
|
// `PrefixNumLimit`如果为-1,则没有限制,`SuffixNumLimit`同理
|
|
|
|
|
//
|
|
|
|
|
// 注意,内部会处理`PrefixNumLimit`或`SuffixNumLimit`过大的情况
|
|
|
|
|
// `PrefixNumLimit`如果为-1,则没有限制,`SuffixNumLimit`同理
|
|
|
|
|
func SliceLimit(a interface{}, PrefixNumLimit int, SuffixNumLimit int, cb func(index int)) {
|
|
|
|
|
v := reflect.ValueOf(a)
|
|
|
|
|
if PrefixNumLimit == -1 && SuffixNumLimit == -1 {
|
|
|
|
@ -108,8 +105,8 @@ func Map2Strings(a interface{}, fn func(k, v interface{}) string) (ret []string)
|
|
|
|
|
// 遍历切片`a`,通过`onIterate`逐个回调元素
|
|
|
|
|
//
|
|
|
|
|
// @param onIterate:
|
|
|
|
|
// @return keepIterate: 如果返回false,则停止变化
|
|
|
|
|
//
|
|
|
|
|
// @return keepIterate: 如果返回false,则停止变化
|
|
|
|
|
func IterateInterfaceAsSlice(a interface{}, onIterate func(iterItem interface{}) (keepIterate bool)) {
|
|
|
|
|
// TODO(chef): fix haven't use keepIterate
|
|
|
|
|
v := reflect.ValueOf(a)
|
|
|
|
|