From 5ae746a1f0068f3b95e6fa721cac4c4c8877a315 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Thu, 10 Feb 2022 11:25:34 +0800 Subject: [PATCH] rename: rename InsertByIndex -> InsertAt, UpdateByIndex -> UpdateAt, DeleteByIndex -> DeleteAt for release 2.0 --- README.md | 6 ++--- README_zh-CN.md | 6 ++--- slice/slice.go | 14 ++++++------ slice/slice_test.go | 56 +++++++++++++++++++++++---------------------- 4 files changed, 42 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 2bf7fd7..24f6ea2 100644 --- a/README.md +++ b/README.md @@ -440,7 +440,7 @@ func Concat[T any](slice []T, values ...[]T) []T //creates a new slice concatena func Difference[T comparable](slice1, slice2 []T) []T //creates an slice of whose element not included in the other given slice func DifferenceBy[T any](slice []T, comparedSlice []T, iteratee func(index int, t T) T) []T //it accepts iteratee which is invoked for each element of slice and values to generate the criterion by which they're compared. func DifferenceWith[T any](slice []T, comparedSlice []T, comparator func(value, otherValue T) bool) []T //accepts comparator which is invoked to compare elements of slice to values. The order and references of result values are determined by the first slice. -func DeleteByIndex[T any](slice []T, start int, end ...int) []T //delete the element of slice from start index to end index - 1 +func DeleteAt[T any](slice []T, start int, end ...int) []T //delete the element of slice from start index to end index - 1 func Drop[T any](slice []T, n int) []T //creates a slice with `n` elements dropped from the beginning when n > 0, or `n` elements dropped from the ending when n < 0 func Every[T any](slice []T, predicate func(index int, t T) bool) bool //return true if all of the values in the slice pass the predicate function func None[T any](slice []T, predicate func(index int, t T) bool) bool // return true if all the values in the slice mismatch the criteria @@ -452,7 +452,7 @@ func ForEach [T any] (slice []T, iteratee func(index int, t T)) //iterates over func IntSlice(slice interface{}) ([]int, error) //convert value to int slice func InterfaceSlice(slice interface{}) []interface{} //convert value to interface{} slice func Intersection[T comparable](slices ...[]T) []T //creates a slice of unique values that included by all slices. -func InsertByIndex[T any](slice []T, index int, value interface{}) []T //insert the value or other slice into slice at index. +func InsertAt[T any](slice []T, index int, value interface{}) []T //insert the value or other slice into slice at index. func Map [T any, U any] (slice []T, iteratee func(index int, t T) U) []U //creates an slice of values by running each element of slice thru iteratee function. func Reverse[T any](slice []T)//revere slice func Reduce[T any](slice []T, iteratee func(index int, t1, t2 T) T, initial T) T //creates an slice of values by running each element of slice thru iteratee function @@ -462,7 +462,7 @@ func Some[T any](slice []T, predicate func(index int, t T) bool) bool //return t func StringSlice(slice interface{}) []string //convert value to string slice func Unique[T comparable](slice []T) []T //remove duplicate elements in slice func Union[T comparable](slices ...[]T) []T //Union creates a slice of unique values, in order, from all given slices. using == for equality comparisons. -func UpdateByIndex[T any](slice []T, index int, value T) []T //update the slice element at index. +func UpdateAt[T any](slice []T, index int, value T) []T //update the slice element at index. func Without[T comparable](slice []T, values ...T) []T //creates a slice excluding all given values func GroupBy[T any](slice []T, groupFn func(index int, t T) bool) ([]T, []T) //iterate over elements of the slice, each element will be group by criteria, returns two slices func Count[T any](slice []T, predicate func(index int, t T) bool) int // iterates over elements of slice, returns a count of all matched elements diff --git a/README_zh-CN.md b/README_zh-CN.md index af8ad60..8be7b0e 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -440,7 +440,7 @@ func Concat[T any](slice []T, values ...[]T) []T //连接values到slice中 func Difference[T comparable](slice1, slice2 []T) []T //返回切片,其元素在slice1中,不在slice2中 func DifferenceBy[T any](slice []T, comparedSlice []T, iteratee func(index int, t T) T) []T //将slice 和comparedSlice中每个元素调用iterateeFn后作比较,如果不相等返回slice中的元素 func DifferenceWith[T any](slice []T, comparedSlice []T, comparator func(value, otherValue T) bool) []T //将slice 和comparedSlice中每个元素调用comparator后作比较,如果为false,返回slice中的元素 -func DeleteByIndex[T any](slice []T, start int, end ...int) []T //删除切片中start到end位置的值(不包含end) +func DeleteAt[T any](slice []T, start int, end ...int) []T //删除切片中start到end位置的值(不包含end) func Drop[T any](slice []T, n int) []T //创建一个新切片,当n大于0时删除原切片前n个元素,当n小于0时删除原切片后n个元素 func Every[T any](slice []T, predicate func(index int, t T) bool) bool//slice中所有元素都符合函数条件时返回true, 否则返回false func None[T any](slice []T, predicate func(index int, t T) bool) bool //slice中所有元素都不符合函数条件时返回true, 否则返回false @@ -452,7 +452,7 @@ func ForEach [T any] (slice []T, iteratee func(index int, t T)) //遍历切片 func IntSlice(slice interface{}) ([]int, error) //转成int切片 func InterfaceSlice(slice interface{}) []interface{} //转成interface{}切片 func Intersection[T comparable](slices ...[]T) []T //slice交集,去重 -func InsertByIndex[T any](slice []T, index int, value interface{}) []T //在切片中index位置插入value +func InsertAt[T any](slice []T, index int, value interface{}) []T //在切片中index位置插入value func Map [T any, U any] (slice []T, iteratee func(index int, t T) U) []U //遍历切片,返回遍历结果 func Reverse[T any](slice []T) //反转切片 func Reduce[T any](slice []T, iteratee func(index int, t1, t2 T) T, initial T) T //切片reduce操作 @@ -462,7 +462,7 @@ func SortByField(slice interface{}, field string, sortType ...string) error // func StringSlice(slice interface{}) []string //转为string切片 func Unique[T comparable](slice []T) []T //去重切片 func Union[T comparable](slices ...[]T) []T //slice并集, 去重 -func UpdateByIndex[T any](slice []T, index int, value T) []T //在切片中index位置更新value +func UpdateAt[T any](slice []T, index int, value T) []T //在切片中index位置更新value func Without[T comparable](slice []T, values ...T) []T //slice去除values func GroupBy[T any](slice []T, groupFn func(index int, t T) bool) ([]T, []T) //根据函数groupFn的逻辑分slice为两组slice func Count[T any](slice []T, predicate func(index int, t T) bool) int //遍历slice的元素,返回所有匹配元素的数量 diff --git a/slice/slice.go b/slice/slice.go index fa7e993..ba12896 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -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 { diff --git a/slice/slice_test.go b/slice/slice_test.go index 14792ae..09ae4de 100644 --- a/slice/slice_test.go +++ b/slice/slice_test.go @@ -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) {