Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat<map> add ValuesByKeys for get values array in order [issue - 467] #494

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ Supported helpers for maps:
- [PickBy](#pickby)
- [PickByKeys](#pickbykeys)
- [PickByValues](#pickbyvalues)
- [ValuesByKeys](#ValuesByKeys)
- [OmitBy](#omitby)
- [OmitByKeys](#omitbykeys)
- [OmitByValues](#omitbyvalues)
Expand Down Expand Up @@ -1120,6 +1121,18 @@ m := lo.PickByValues(map[string]int{"foo": 1, "bar": 2, "baz": 3}, []int{1, 3})

[[play](https://go.dev/play/p/1zdzSvbfsJc)]

### ValuesByKeys

Returns an array of values in the same order as the given keys.
If the key in keys slice but not in the map, return error
```go

m, err := lo.ValuesByKeys(map[string]int{"foo": 1, "bar": 2, "baz": 3}, []string{"baz", "foo", "bar"})
// []int{3,1,2}
```

[[play](https://go.dev/play/p/PmmPDo1AqWl)]

### OmitBy

Returns same map type filtered by given predicate.
Expand Down
19 changes: 19 additions & 0 deletions map.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package lo

import (
"fmt"
)

// Keys creates an array of the map keys.
// Play: https://go.dev/play/p/Uu11fHASqrU
func Keys[K comparable, V any](in map[K]V) []K {
Expand Down Expand Up @@ -76,6 +80,21 @@ func PickByValues[K comparable, V comparable, Map ~map[K]V](in Map, values []V)
return r
}

// ValuesByKeys returns an array of values in the same order as the given keys.
// if the key in keys slice but not in the map, return error
// Play: https://go.dev/play/p/J0SvRMzPJo_s
func ValuesByKeys[K comparable, V any](in map[K]V, keys []K) ([]V, error) {
out := make([]V, 0, len(keys))
for i := range keys {
v, ok := in[keys[i]]
if !ok {
return nil, fmt.Errorf("ValuesByKeys: %v is not in the map", keys[i])
}
out = append(out, v)
}
return out, nil
}

// OmitBy returns same map type filtered by given predicate.
// Play: https://go.dev/play/p/EtBsR43bdsd
func OmitBy[K comparable, V any, Map ~map[K]V](in Map, predicate func(key K, value V) bool) Map {
Expand Down
11 changes: 11 additions & 0 deletions map_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ func ExamplePickByValues() {
// Output: 2 1 3
}

func ExampleValuesByKeys() {
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}

result, err := ValuesByKeys(kv, []string{"baz", "foo", "bar"})
if err != nil {
return
}
fmt.Printf("%v %v %v", len(result), result[0], result[1])
// Output: 3 3 1
}

func ExampleOmitBy() {
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}

Expand Down
18 changes: 18 additions & 0 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,24 @@ func TestPickByValues(t *testing.T) {
is.IsType(after, before, "type preserved")
}

func TestValuesByKeys(t *testing.T) {
t.Parallel()
is := assert.New(t)

result1, err1 := ValuesByKeys(map[string]int{"foo": 1, "bar": 2, "baz": 3}, []string{"baz", "foo", "bar"})
is.NoError(err1)
is.Equal(len(result1), 3)
is.ElementsMatch(result1, []int{3, 1, 2})

result2, err2 := ValuesByKeys(map[string]int{"": 0, "foobar": 6, "baz": 3}, []string{"baz", "foobar"})
is.NoError(err2)
is.Equal(len(result2), 2)
is.ElementsMatch(result2, []int{3, 6})

_, err3 := ValuesByKeys(map[string]int{"foo": 1, "baz": 3}, []string{"baz", "foo", "bar"})
is.Errorf(err3, "ValuesByKeys: bar is not in the map")
}

func TestOmitBy(t *testing.T) {
t.Parallel()
is := assert.New(t)
Expand Down