From 02614b3781f6acdfc6df0e7b07d856b2779c4ac7 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Sun, 9 Jun 2024 19:35:34 +0200 Subject: feat: Per repository configuration --- pkg/u/list.go | 8 ++++++++ pkg/u/list_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) (limited to 'pkg/u') diff --git a/pkg/u/list.go b/pkg/u/list.go index cf71909..7271ef3 100644 --- a/pkg/u/list.go +++ b/pkg/u/list.go @@ -16,6 +16,14 @@ func FirstOrZero[T any](v []T) T { return v[0] } +func Map[T any, V any](ts []T, fun func(T) V) []V { + rs := make([]V, len(ts)) + for i := range ts { + rs[i] = fun(ts[i]) + } + return rs +} + func ChunkBy[T any](items []T, chunkSize int) [][]T { var chunks = make([][]T, 0, (len(items)/chunkSize)+1) for chunkSize < len(items) { diff --git a/pkg/u/list_test.go b/pkg/u/list_test.go index 805a209..3a856b9 100644 --- a/pkg/u/list_test.go +++ b/pkg/u/list_test.go @@ -3,6 +3,7 @@ package u import ( + "strconv" "testing" "github.com/google/go-cmp/cmp" @@ -129,3 +130,32 @@ func TestFirstOrZero(t *testing.T) { }) } } + +func TestMap(t *testing.T) { + testCases := []struct { + name string + in []int + out []string + }{ + { + name: "empty", + in: []int{}, + out: []string{}, + }, + { + name: "not empty", + in: []int{1, 2, 3}, + out: []string{"1", "2", "3"}, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + out := Map(tc.in, func(v int) string { return strconv.Itoa(v) }) + + if diff := cmp.Diff(tc.out, out); diff != "" { + t.Errorf("Map error:\n%s", diff) + } + }) + } +} -- cgit v1.2.3