From 76565da2ce08ed1a3f1b2efb0fde1d596f84f1ce Mon Sep 17 00:00:00 2001 From: Aiden <12055114+aidenwallis@users.noreply.github.com> Date: Sat, 26 Nov 2022 19:58:46 +0000 Subject: [PATCH 1/2] Add utils.MapKeys and utils.MapValues --- utils/map_keys.go | 10 ++++++++++ utils/map_keys_test.go | 30 ++++++++++++++++++++++++++++++ utils/map_values.go | 0 utils/map_values_test.go | 30 ++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+) create mode 100644 utils/map_keys.go create mode 100644 utils/map_keys_test.go create mode 100644 utils/map_values.go create mode 100644 utils/map_values_test.go diff --git a/utils/map_keys.go b/utils/map_keys.go new file mode 100644 index 0000000..cef4c65 --- /dev/null +++ b/utils/map_keys.go @@ -0,0 +1,10 @@ +package utils + +// MapKeys returns all keys in a given map, **the order will not be stable, you should sort these values if needed.** +func MapKeys[K comparable, V any](v map[K]V) []K { + resp := make([]K, 0, len(v)) + for k := range v { + resp = append(resp, k) + } + return resp +} diff --git a/utils/map_keys_test.go b/utils/map_keys_test.go new file mode 100644 index 0000000..03af1bc --- /dev/null +++ b/utils/map_keys_test.go @@ -0,0 +1,30 @@ +package utils_test + +import ( + "sort" + "testing" + + "github.com/aidenwallis/go-utils/internal/assert" + "github.com/aidenwallis/go-utils/utils" +) + +func TestMapKeys(t *testing.T) { + t.Parallel() + + out := []int{1, 2, 3} + in := map[int]struct{}{ + 1: {}, + 2: {}, + 3: {}, + } + + v := utils.MapKeys(in) + sort.SliceStable(v, func(i, j int) bool { + return v[i] < v[j] + }) + + assert.Equal(t, 3, len(v)) + for i := range out { + assert.Equal(t, out[i], v[i]) + } +} diff --git a/utils/map_values.go b/utils/map_values.go new file mode 100644 index 0000000..e69de29 diff --git a/utils/map_values_test.go b/utils/map_values_test.go new file mode 100644 index 0000000..8d30469 --- /dev/null +++ b/utils/map_values_test.go @@ -0,0 +1,30 @@ +package utils_test + +import ( + "sort" + "testing" + + "github.com/aidenwallis/go-utils/internal/assert" + "github.com/aidenwallis/go-utils/utils" +) + +func TestMapValues(t *testing.T) { + t.Parallel() + + out := []int{1, 2, 3} + in := map[string]int{ + "one": 1, + "two": 2, + "three": 3, + } + + v := utils.MapValues(in) + sort.SliceStable(v, func(i, j int) bool { + return v[i] < v[j] + }) + + assert.Equal(t, 3, len(v)) + for i := range out { + assert.Equal(t, out[i], v[i]) + } +} From 37bacb2b723e6452608d11595d814250c9a14109 Mon Sep 17 00:00:00 2001 From: Aiden <12055114+aidenwallis@users.noreply.github.com> Date: Sat, 26 Nov 2022 19:59:41 +0000 Subject: [PATCH 2/2] Update map_values.go --- utils/map_values.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/utils/map_values.go b/utils/map_values.go index e69de29..69510ff 100644 --- a/utils/map_values.go +++ b/utils/map_values.go @@ -0,0 +1,10 @@ +package utils + +// MapValues returns all values in a given map, **the order is not stable, you should sort it if you need a stable order.** +func MapValues[K comparable, V any](in map[K]V) []V { + out := make([]V, 0, len(in)) + for _, v := range in { + out = append(out, v) + } + return out +}