diff --git a/lists.go b/lists.go index 0ee040f..ccc53bf 100644 --- a/lists.go +++ b/lists.go @@ -154,4 +154,34 @@ func DeleteList(listName string) error { } } return fmt.Errorf("list '%s' doesn't exist", listName) +} + +func DeleteItemByContent(listName string, item string) bool { + var list, err = GetList(listName) + if err != nil { + return false + } + var newList []string + for _, value := range list { + if value != item { + newList = append(newList, value) + } + } + lists[listName] = newList + return true +} + +func DeleteItemByIndex(listName string, index int) bool { + var list, err = GetList(listName) + if err != nil { + return false + } + var newList []string + for i, value := range list { + if i != index { + newList = append(newList, value) + } + } + lists[listName] = newList + return true } \ No newline at end of file diff --git a/tests/lists_test.go b/tests/lists_test.go index 6306a9c..114346b 100644 --- a/tests/lists_test.go +++ b/tests/lists_test.go @@ -176,4 +176,32 @@ func TestLists(t *testing.T) { t.Errorf("error: %v", err) } }) + + t.Run("Delete item from list by content", func(t *testing.T) { + var listName string = "tmp-list-with-item" + var newItem string = "delete-me" + sider.RPush(listName, newItem) + if sider.DeleteItemByContent(listName, newItem) == false { + t.Errorf("Error deleting item: %v", newItem) + } + if result, _ := sider.GetList(listName); len(result) != 0 { + t.Errorf("[2] Error deleting item: %s", newItem) + } + }) + + t.Run("Delete item from list by index", func(t *testing.T) { + var listName string = "delete-list-by-index" + var newItem string = "delete-item" + sider.RPush(listName, newItem) + if index, err := sider.IndexOf(listName, newItem); err != nil { + t.Errorf("error finding index for item: %s", newItem) + } else { + if sider.DeleteItemByIndex(listName, index) == false { + t.Errorf("[2] Error deleting item by index: %s", newItem) + } + if result, _ := sider.GetList(listName); len(result) != 0 { + t.Errorf("[3] Error deleting item: %s", newItem) + } + } + }) }