mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-11 08:12:26 +08:00
rename: rename InsertByIndex -> InsertAt, UpdateByIndex -> UpdateAt, DeleteByIndex -> DeleteAt for release 2.0
This commit is contained in:
@@ -433,11 +433,11 @@ func IntSlice(slice interface{}) []int {
|
||||
return out
|
||||
}
|
||||
|
||||
// DeleteByIndex delete the element of slice from start index to end index - 1.
|
||||
func DeleteByIndex[T any](slice []T, start int, end ...int) []T {
|
||||
// DeleteAt delete the element of slice from start index to end index - 1.
|
||||
func DeleteAt[T any](slice []T, start int, end ...int) []T {
|
||||
size := len(slice)
|
||||
|
||||
if start < 0 || start > size-1 {
|
||||
if start < 0 || start >= size {
|
||||
return slice
|
||||
}
|
||||
|
||||
@@ -482,8 +482,8 @@ func Drop[T any](slice []T, n int) []T {
|
||||
return slice[n:size]
|
||||
}
|
||||
|
||||
// InsertByIndex insert the value or other slice into slice at index.
|
||||
func InsertByIndex[T any](slice []T, index int, value interface{}) []T {
|
||||
// InsertAt insert the value or other slice into slice at index.
|
||||
func InsertAt[T any](slice []T, index int, value interface{}) []T {
|
||||
size := len(slice)
|
||||
|
||||
if index < 0 || index > size {
|
||||
@@ -505,8 +505,8 @@ func InsertByIndex[T any](slice []T, index int, value interface{}) []T {
|
||||
return slice
|
||||
}
|
||||
|
||||
// UpdateByIndex update the slice element at index.
|
||||
func UpdateByIndex[T any](slice []T, index int, value T) []T {
|
||||
// UpdateAt update the slice element at index.
|
||||
func UpdateAt[T any](slice []T, index int, value T) []T {
|
||||
size := len(slice)
|
||||
|
||||
if index < 0 || index >= size {
|
||||
|
||||
@@ -291,21 +291,21 @@ func TestInterfaceSlice(t *testing.T) {
|
||||
assert.Equal(expect, InterfaceSlice(strs))
|
||||
}
|
||||
|
||||
func TestDeleteByIndex(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestDeleteByIndex")
|
||||
func TestDeleteAt(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestDeleteAt")
|
||||
|
||||
assert.Equal([]string{"a", "b", "c"}, DeleteByIndex([]string{"a", "b", "c"}, -1))
|
||||
assert.Equal([]string{"a", "b", "c"}, DeleteByIndex([]string{"a", "b", "c"}, 3))
|
||||
assert.Equal([]string{"b", "c"}, DeleteByIndex([]string{"a", "b", "c"}, 0))
|
||||
assert.Equal([]string{"a", "c"}, DeleteByIndex([]string{"a", "b", "c"}, 1))
|
||||
assert.Equal([]string{"a", "b"}, DeleteByIndex([]string{"a", "b", "c"}, 2))
|
||||
assert.Equal([]string{"a", "b", "c"}, DeleteAt([]string{"a", "b", "c"}, -1))
|
||||
assert.Equal([]string{"a", "b", "c"}, DeleteAt([]string{"a", "b", "c"}, 3))
|
||||
assert.Equal([]string{"b", "c"}, DeleteAt([]string{"a", "b", "c"}, 0))
|
||||
assert.Equal([]string{"a", "c"}, DeleteAt([]string{"a", "b", "c"}, 1))
|
||||
assert.Equal([]string{"a", "b"}, DeleteAt([]string{"a", "b", "c"}, 2))
|
||||
|
||||
assert.Equal([]string{"b", "c"}, DeleteByIndex([]string{"a", "b", "c"}, 0, 1))
|
||||
assert.Equal([]string{"c"}, DeleteByIndex([]string{"a", "b", "c"}, 0, 2))
|
||||
assert.Equal([]string{}, DeleteByIndex([]string{"a", "b", "c"}, 0, 3))
|
||||
assert.Equal([]string{}, DeleteByIndex([]string{"a", "b", "c"}, 0, 4))
|
||||
assert.Equal([]string{"a"}, DeleteByIndex([]string{"a", "b", "c"}, 1, 3))
|
||||
assert.Equal([]string{"a"}, DeleteByIndex([]string{"a", "b", "c"}, 1, 4))
|
||||
assert.Equal([]string{"b", "c"}, DeleteAt([]string{"a", "b", "c"}, 0, 1))
|
||||
assert.Equal([]string{"c"}, DeleteAt([]string{"a", "b", "c"}, 0, 2))
|
||||
assert.Equal([]string{}, DeleteAt([]string{"a", "b", "c"}, 0, 3))
|
||||
assert.Equal([]string{}, DeleteAt([]string{"a", "b", "c"}, 0, 4))
|
||||
assert.Equal([]string{"a"}, DeleteAt([]string{"a", "b", "c"}, 1, 3))
|
||||
assert.Equal([]string{"a"}, DeleteAt([]string{"a", "b", "c"}, 1, 4))
|
||||
}
|
||||
|
||||
func TestDrop(t *testing.T) {
|
||||
@@ -325,26 +325,28 @@ func TestDrop(t *testing.T) {
|
||||
assert.Equal([]int{}, Drop([]int{1, 2, 3, 4, 5}, -6))
|
||||
}
|
||||
|
||||
func TestInsertByIndex(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestInsertByIndex")
|
||||
func TestInsertAt(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestInsertAt")
|
||||
|
||||
strs := []string{"a", "b", "c"}
|
||||
assert.Equal([]string{"a", "b", "c"}, InsertByIndex(strs, -1, "1"))
|
||||
assert.Equal([]string{"a", "b", "c"}, InsertByIndex(strs, 4, "1"))
|
||||
assert.Equal([]string{"1", "a", "b", "c"}, InsertByIndex(strs, 0, "1"))
|
||||
assert.Equal([]string{"a", "b", "c", "1"}, InsertByIndex(strs, 3, "1"))
|
||||
assert.Equal([]string{"1", "2", "3", "a", "b", "c"}, InsertByIndex(strs, 0, []string{"1", "2", "3"}))
|
||||
assert.Equal([]string{"a", "b", "c", "1", "2", "3"}, InsertByIndex(strs, 3, []string{"1", "2", "3"}))
|
||||
assert.Equal([]string{"a", "b", "c"}, InsertAt(strs, -1, "1"))
|
||||
assert.Equal([]string{"a", "b", "c"}, InsertAt(strs, 4, "1"))
|
||||
assert.Equal([]string{"1", "a", "b", "c"}, InsertAt(strs, 0, "1"))
|
||||
assert.Equal([]string{"a", "1", "b", "c"}, InsertAt(strs, 1, "1"))
|
||||
assert.Equal([]string{"a", "b", "1", "c"}, InsertAt(strs, 2, "1"))
|
||||
assert.Equal([]string{"a", "b", "c", "1"}, InsertAt(strs, 3, "1"))
|
||||
assert.Equal([]string{"1", "2", "3", "a", "b", "c"}, InsertAt(strs, 0, []string{"1", "2", "3"}))
|
||||
assert.Equal([]string{"a", "b", "c", "1", "2", "3"}, InsertAt(strs, 3, []string{"1", "2", "3"}))
|
||||
t.Log(strs)
|
||||
}
|
||||
|
||||
func TestUpdateByIndex(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestUpdateByIndex")
|
||||
func TestUpdateAt(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestUpdateAt")
|
||||
|
||||
assert.Equal([]string{"a", "b", "c"}, UpdateByIndex([]string{"a", "b", "c"}, -1, "1"))
|
||||
assert.Equal([]string{"1", "b", "c"}, UpdateByIndex([]string{"a", "b", "c"}, 0, "1"))
|
||||
assert.Equal([]string{"a", "b", "2"}, UpdateByIndex([]string{"a", "b", "c"}, 2, "2"))
|
||||
assert.Equal([]string{"a", "b", "c"}, UpdateByIndex([]string{"a", "b", "c"}, 3, "2"))
|
||||
assert.Equal([]string{"a", "b", "c"}, UpdateAt([]string{"a", "b", "c"}, -1, "1"))
|
||||
assert.Equal([]string{"1", "b", "c"}, UpdateAt([]string{"a", "b", "c"}, 0, "1"))
|
||||
assert.Equal([]string{"a", "b", "2"}, UpdateAt([]string{"a", "b", "c"}, 2, "2"))
|
||||
assert.Equal([]string{"a", "b", "c"}, UpdateAt([]string{"a", "b", "c"}, 3, "2"))
|
||||
}
|
||||
|
||||
func TestUnique(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user