mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-02-03 13:21:56 +02:00
72 lines
1.4 KiB
Go
72 lines
1.4 KiB
Go
package lo
|
|
|
|
// Keys creates an array of the map keys.
|
|
func Keys[K comparable, V any](in map[K]V) []K {
|
|
result := make([]K, 0, len(in))
|
|
|
|
for k, _ := range in {
|
|
result = append(result, k)
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
// Values creates an array of the map values.
|
|
func Values[K comparable, V any](in map[K]V) []V {
|
|
result := make([]V, 0, len(in))
|
|
|
|
for _, v := range in {
|
|
result = append(result, v)
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
// Entries transforms a map into array of key/value pairs.
|
|
func Entries[K comparable, V any](in map[K]V) []Entry[K, V] {
|
|
entries := make([]Entry[K, V], 0, len(in))
|
|
|
|
for k, v := range in {
|
|
entries = append(entries, Entry[K, V]{
|
|
Key: k,
|
|
Value: v,
|
|
})
|
|
}
|
|
|
|
return entries
|
|
}
|
|
|
|
// FromEntries transforms an array of key/value pairs into a map.
|
|
func FromEntries[K comparable, V any](entries []Entry[K, V]) map[K]V {
|
|
out := map[K]V{}
|
|
|
|
for _, v := range entries {
|
|
out[v.Key] = v.Value
|
|
}
|
|
|
|
return out
|
|
}
|
|
|
|
// Assign merges multiple maps from left to right.
|
|
func Assign[K comparable, V any](maps ...map[K]V) map[K]V {
|
|
out := map[K]V{}
|
|
|
|
for _, m := range maps {
|
|
for k, v := range m {
|
|
out[k] = v
|
|
}
|
|
}
|
|
|
|
return out
|
|
}
|
|
|
|
// MapValues manipulates a map values and transforms it to a map of another type.
|
|
func MapValues[K comparable, V any, R any](in map[K]V, iteratee func(V, K) R) map[K]R {
|
|
result := map[K]R{}
|
|
|
|
for k, v := range in {
|
|
result[k] = iteratee(v, k)
|
|
}
|
|
|
|
return result
|
|
} |