diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2021-10-23 11:25:37 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2021-10-23 12:29:52 +1100 |
commit | ef544e6ce961add2fcac4004ba6587b2de378e64 (patch) | |
tree | 98d7fe313fe237bcd3b78d654b41c45a2ff4a38c /pkg/utils | |
parent | 629494144fcef931a9f484821202bcd4370aa23b (diff) |
add more suggestions
Diffstat (limited to 'pkg/utils')
-rw-r--r-- | pkg/utils/slice.go | 28 | ||||
-rw-r--r-- | pkg/utils/slice_test.go | 83 |
2 files changed, 111 insertions, 0 deletions
diff --git a/pkg/utils/slice.go b/pkg/utils/slice.go index f536ea056..48acdbd2d 100644 --- a/pkg/utils/slice.go +++ b/pkg/utils/slice.go @@ -116,3 +116,31 @@ func StringArraysOverlap(strArrA []string, strArrB []string) bool { return false } + +func Uniq(values []string) []string { + added := make(map[string]bool) + result := make([]string, 0, len(values)) + for _, value := range values { + if added[value] { + continue + } + added[value] = true + result = append(result, value) + } + return result +} + +func Limit(values []string, limit int) []string { + if len(values) > limit { + return values[:limit] + } + return values +} + +func Reverse(values []string) []string { + result := make([]string, len(values)) + for i, val := range values { + result[len(values)-i-1] = val + } + return result +} diff --git a/pkg/utils/slice_test.go b/pkg/utils/slice_test.go index 491968cb4..3636f44cb 100644 --- a/pkg/utils/slice_test.go +++ b/pkg/utils/slice_test.go @@ -165,3 +165,86 @@ func TestEscapeSpecialChars(t *testing.T) { }) } } + +func TestUniq(t *testing.T) { + for _, test := range []struct { + values []string + want []string + }{ + { + values: []string{"a", "b", "c"}, + want: []string{"a", "b", "c"}, + }, + { + values: []string{"a", "b", "a", "b", "c"}, + want: []string{"a", "b", "c"}, + }, + } { + if got := Uniq(test.values); !assert.EqualValues(t, got, test.want) { + t.Errorf("Uniq(%v) = %v; want %v", test.values, got, test.want) + } + } +} + +func TestLimit(t *testing.T) { + for _, test := range []struct { + values []string + limit int + want []string + }{ + { + values: []string{"a", "b", "c"}, + limit: 3, + want: []string{"a", "b", "c"}, + }, + { + values: []string{"a", "b", "c"}, + limit: 4, + want: []string{"a", "b", "c"}, + }, + { + values: []string{"a", "b", "c"}, + limit: 2, + want: []string{"a", "b"}, + }, + { + values: []string{"a", "b", "c"}, + limit: 1, + want: []string{"a"}, + }, + { + values: []string{"a", "b", "c"}, + limit: 0, + want: []string{}, + }, + { + values: []string{}, + limit: 0, + want: []string{}, + }, + } { + if got := Limit(test.values, test.limit); !assert.EqualValues(t, got, test.want) { + t.Errorf("Limit(%v, %d) = %v; want %v", test.values, test.limit, got, test.want) + } + } +} + +func TestReverse(t *testing.T) { + for _, test := range []struct { + values []string + want []string + }{ + { + values: []string{"a", "b", "c"}, + want: []string{"c", "b", "a"}, + }, + { + values: []string{}, + want: []string{}, + }, + } { + if got := Reverse(test.values); !assert.EqualValues(t, got, test.want) { + t.Errorf("Reverse(%v) = %v; want %v", test.values, got, test.want) + } + } +} |