Add ability to reorder playlist items

This commit is contained in:
Deluan 2020-06-04 19:05:41 -04:00
parent b597a34cb4
commit 331fa1d952
8 changed files with 158 additions and 34 deletions

View file

@ -25,3 +25,16 @@ func StringInSlice(a string, list []string) bool {
}
return false
}
func InsertString(array []string, value string, index int) []string {
return append(array[:index], append([]string{value}, array[index:]...)...)
}
func RemoveString(array []string, index int) []string {
return append(array[:index], array[index+1:]...)
}
func MoveString(array []string, srcIndex int, dstIndex int) []string {
value := array[srcIndex]
return InsertString(RemoveString(array, srcIndex), value, dstIndex)
}

View file

@ -48,4 +48,16 @@ var _ = Describe("Strings", func() {
Expect(StringInSlice("bbb", []string{"bbb", "aaa", "ccc"})).To(BeTrue())
})
})
Describe("MoveString", func() {
It("moves item to end of slice", func() {
Expect(MoveString([]string{"1", "2", "3"}, 0, 2)).To(ConsistOf("2", "3", "1"))
})
It("moves item to beginning of slice", func() {
Expect(MoveString([]string{"1", "2", "3"}, 2, 0)).To(ConsistOf("3", "1", "2"))
})
It("keeps item in same position if srcIndex == dstIndex", func() {
Expect(MoveString([]string{"1", "2", "3"}, 1, 1)).To(ConsistOf("1", "2", "3"))
})
})
})