Skip to content

Commit

Permalink
Merge pull request #10 from nlif-m/dev
Browse files Browse the repository at this point in the history
feat: implement "In" function to check is element in slice
  • Loading branch information
nlif-m committed May 7, 2023
2 parents 683340a + 09ff777 commit 2bc117f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
13 changes: 13 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,16 @@ func Compare[T comparable](items1, items2 []T) bool {

return true
}

func In[T comparable](item T, items []T) bool {
for _, it := range items {
if it == item {
return true
}
}
return false
}

func NotIn[T comparable](item T, items []T) bool {
return !In(item, items)
}
25 changes: 25 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,28 @@ func TestUnique(t *testing.T) {
}

}

func TestIn(t *testing.T) {
type testCase[T comparable] struct {
item T
items []T
flag bool
}

tests := []testCase[string]{
{"mp3", YtdlpAudioFormats[:], true},
{"mp2", YtdlpAudioFormats[:], false},
{"", []string{"Hello", "World"}, false},
}

for _, test := range tests {
testname := fmt.Sprintf("%s in %s is %v", test.item, test.items, test.flag)
t.Run(testname, func(t *testing.T) {
answer := In(test.item, test.items)
if answer != test.flag {
t.Errorf("got %v, want %v", answer, test.flag)
}

})
}
}

0 comments on commit 2bc117f

Please sign in to comment.