Skip to content

Commit

Permalink
add utility to use in app (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
RoxyFarhad authored Jun 6, 2024
1 parent a27f13b commit 3c719ee
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
11 changes: 11 additions & 0 deletions string.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ func (ss StringSet) Remove(value string) {
delete(ss, value)
}

// ToList converts the set into a list of strings.
func (ss StringSet) ToList() []string {
list := make([]string, len(ss))
i := 0
for key := range ss {
list[i] = key
i++
}
return list
}

// StringSliceRemove removes an element from the slice at the given position.
func StringSliceRemove(from []string, at int) []string {
if at >= len(from) {
Expand Down
10 changes: 10 additions & 0 deletions string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ func TestStringSet(t *testing.T) {
ss.Remove("no-op")
_, ok = ss["foo"]
test.That(t, ok, test.ShouldBeTrue)

ss.Remove("foo")
test.That(t, len(ss), test.ShouldEqual, 0)

ss.Add("hello")
ss.Add("bye")

listView := ss.ToList()
test.That(t, len(listView), test.ShouldEqual, 2)
test.That(t, listView, test.ShouldResemble, []string{"hello", "bye"})
}

func TestStringSliceRemove(t *testing.T) {
Expand Down

0 comments on commit 3c719ee

Please sign in to comment.