Skip to content

Commit

Permalink
refactor: add slice utils to remove items from slice
Browse files Browse the repository at this point in the history
  • Loading branch information
42atomys committed Jul 16, 2022
1 parent 6e6c6f3 commit 4219b44
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
5 changes: 3 additions & 2 deletions internal/webhooks/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,9 @@ func (p *processor) githubHandler(data []byte) error {
}
flagsList = append(flagsList, typesgen.FlagSponsor.String())
case "cancelled":
utils.Remove(
flagsList, typesgen.FlagSponsor.String(),
flagsList = utils.Remove(
flagsList,
typesgen.FlagSponsor.String(),
typesgen.FlagBeta.String(),
typesgen.FlagDiscord.String(),
)
Expand Down
10 changes: 6 additions & 4 deletions pkg/utils/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package utils

// Remove an item from a slice
func Remove[T comparable](slice []T, items ...T) []T {
for i, element := range slice {
var result = make([]T, 0)
for _, element := range slice {
if Contains(items, element) {
return append(slice[:i], slice[i+1:]...)
continue
}
result = append(result, element)
}
return slice
return result
}

// Contains returns true if the item is in the slice
Expand All @@ -22,7 +24,7 @@ func Contains[T comparable](slice []T, item T) bool {

// Uniq returns a new slice with unique items from the given slice
func Uniq[T comparable](slice []T) []T {
var unique []T
var unique []T = make([]T, 0)
for _, element := range slice {
if Contains(unique, element) {
continue
Expand Down
58 changes: 58 additions & 0 deletions pkg/utils/slice_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package utils

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestRemove(t *testing.T) {
tests := []struct {
name string
sliceBase []string
itemsToRemove []string
want []string
}{
{"remove one item", []string{"a", "b", "c"}, []string{"b"}, []string{"a", "c"}},
{"remove multiple items", []string{"a", "b", "c"}, []string{"b", "c"}, []string{"a"}},
{"remove all items", []string{"a", "b", "c"}, []string{"a", "b", "c"}, []string{}},
{"remove nothing", []string{"a", "b", "c"}, []string{"d", "e", "f"}, []string{"a", "b", "c"}},
}
for _, tt := range tests {
assert.Equal(t, tt.want, Remove(tt.sliceBase, tt.itemsToRemove...), tt.name)
}
}


func TestContains(t *testing.T) {
tests := []struct {
name string
sliceBase []string
item string
want bool
}{
{"contains one item", []string{"a", "b", "c"}, "b", true},
{"contains multiple items", []string{"a", "b", "c"}, "b", true},
{"contains all items", []string{"a", "b", "c"}, "a", true},
{"contains nothing", []string{"a", "b", "c"}, "d", false},
}
for _, tt := range tests {
assert.Equal(t, tt.want, Contains(tt.sliceBase, tt.item), tt.name)
}
}

func TestUniq(t *testing.T) {
tests := []struct {
name string
sliceBase []string
want []string
}{
{"uniq one item", []string{"a", "b", "c"}, []string{"a", "b", "c"}},
{"uniq multiple items", []string{"a", "b", "c", "a", "b", "c"}, []string{"a", "b", "c"}},
{"uniq all items", []string{"a", "b", "c", "a", "b", "c"}, []string{"a", "b", "c"}},
{"uniq nothing", []string{"a", "b", "c", "d", "e", "f"}, []string{"a", "b", "c", "d", "e", "f"}},
}
for _, tt := range tests {
assert.Equal(t, tt.want, Uniq(tt.sliceBase), tt.name)
}
}

0 comments on commit 4219b44

Please sign in to comment.