Skip to content

Commit

Permalink
feat: sher 包新增 map 相关映射操作
Browse files Browse the repository at this point in the history
  • Loading branch information
kercylan98 committed Dec 29, 2023
1 parent 515cbc6 commit 7086281
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions utils/sher/map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package sher

// MappingFromSlice 将切片中的元素进行转换
func MappingFromSlice[S ~[]V, NS ~[]N, V, N any](slice S, handler func(value V) N) NS {
if slice == nil {
return nil
}
result := make(NS, len(slice))
for i, v := range slice {
result[i] = handler(v)
}
return result
}

// MappingFromMap 将 map 中的元素进行转换
func MappingFromMap[M ~map[K]V, NM ~map[K]N, K comparable, V, N any](m M, handler func(value V) N) NM {
if m == nil {
return nil
}
result := make(NM, len(m))
for k, v := range m {
result[k] = handler(v)
}
return result
}

0 comments on commit 7086281

Please sign in to comment.