Skip to content

Commit ad0d138

Browse files
authored
Added collection utilities (#32)
* Added utilities * Added licensing header
1 parent 200b993 commit ad0d138

File tree

4 files changed

+100
-1
lines changed

4 files changed

+100
-1
lines changed

changes/202111091652.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added collection utilities for removing elements from slices

utils/collection/modify.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (C) 2020-2021 Arm Limited or its affiliates and Contributors. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package collection
6+
7+
// Remove looks for elements in a slice. If they're found, it will
8+
// remove them.
9+
func Remove(slice []string, val ...string) []string {
10+
return GenericRemove(func(val1, val2 string) bool { return val1 == val2 }, slice, val...)
11+
}
12+
13+
// GenericRemove looks for elements in a slice using the equal function. If they're found, it will
14+
// remove them from the slice.
15+
func GenericRemove(equal func(string, string) bool, slice []string, val ...string) []string {
16+
if len(val) == 0 {
17+
return slice
18+
}
19+
list := make([]string, 0, len(slice))
20+
found := make([]bool, len(val))
21+
22+
for i := range slice {
23+
e := slice[i]
24+
for j := range val {
25+
found[j] = equal(e, val[j])
26+
}
27+
if !Any(found) {
28+
list = append(list, e)
29+
}
30+
}
31+
return list
32+
}

utils/collection/modify_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (C) 2020-2021 Arm Limited or its affiliates and Contributors. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package collection
6+
7+
import (
8+
"fmt"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func TestRemove(t *testing.T) {
15+
// Given a list of strings and a string
16+
// Returns the list without the string
17+
tests := []struct {
18+
SrcList []string
19+
ToDelete []string
20+
ExpectedList []string
21+
}{
22+
{
23+
SrcList: []string{"a", "b", "c", "d"},
24+
ToDelete: []string{"c"},
25+
ExpectedList: []string{"a", "b", "d"},
26+
},
27+
{
28+
SrcList: []string{"a", "b", "c", "d"},
29+
ToDelete: []string{"h"},
30+
ExpectedList: []string{"a", "b", "c", "d"},
31+
},
32+
{
33+
SrcList: []string{"a", "b", "c", "d"},
34+
ToDelete: []string{"d"},
35+
ExpectedList: []string{"a", "b", "c"},
36+
},
37+
{
38+
SrcList: []string{"d", "d", "d", "d"},
39+
ToDelete: []string{"d"},
40+
ExpectedList: []string{},
41+
},
42+
{
43+
SrcList: []string{},
44+
ToDelete: []string{"d"},
45+
ExpectedList: []string{},
46+
},
47+
{
48+
SrcList: []string{},
49+
ToDelete: []string{"d", "e"},
50+
ExpectedList: []string{},
51+
},
52+
{
53+
SrcList: []string{"a", "b", "c", "d"},
54+
ToDelete: []string{"a", "b", "d"},
55+
ExpectedList: []string{"c"},
56+
},
57+
}
58+
for i := range tests {
59+
test := tests[i]
60+
t.Run(fmt.Sprintf("test_%v", i), func(t *testing.T) {
61+
t.Parallel()
62+
newList := Remove(test.SrcList, test.ToDelete...)
63+
assert.Equal(t, test.ExpectedList, newList)
64+
})
65+
}
66+
}

utils/collection/search.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55
package collection
66

7-
// Looks for an element in a slice. If found it will
7+
// Find looks for an element in a slice. If found it will
88
// return its index and true; otherwise it will return -1 and false.
99
func Find(slice *[]string, val string) (int, bool) {
1010
for i, item := range *slice {

0 commit comments

Comments
 (0)