mirror of
https://github.com/duke-git/lancet.git
synced 2026-11-16 05:12:11 +08:00
fix(slice): return safely when DeleteAt receives no elements (#381)
This commit is contained in:
1 parent
752f1ed63d
commit
eb3b1e33d2
2 files changed
+5
No files matched your search
@@ -712,6 +712,9 @@ func IntSlice(slice any) []int {
|
||||
// Play: https://go.dev/play/p/800B1dPBYyd
|
||||
func DeleteAt[T any](slice []T, index int) []T {
|
||||
result := append([]T(nil), slice...)
|
||||
if len(result) == 0 {
|
||||
return result
|
||||
}
|
||||
|
||||
if index < 0 || index >= len(slice) {
|
||||
return result[:len(slice)-1]
|
||||
|
||||
@@ -801,6 +801,8 @@ func TestDeleteAt(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert := internal.NewAssert(t, "TestDeleteAt")
|
||||
assert.Equal(0, len(DeleteAt([]int(nil), 0)))
|
||||
assert.Equal(0, len(DeleteAt([]int{}, -1)))
|
||||
|
||||
tests := []struct {
|
||||
slice []int
|
||||
|
||||
Reference in new issue
Block a user