mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-15 18:22:27 +08:00
feat: add Repeate function for slice
This commit is contained in:
@@ -417,6 +417,15 @@ func ReplaceAll[T comparable](slice []T, old T, new T) []T {
|
|||||||
return Replace(slice, old, new, -1)
|
return Replace(slice, old, new, -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Repeat creates a slice with length n whose elements are param `item`.
|
||||||
|
func Repeat[T any](item T, n int) []T {
|
||||||
|
result := make([]T, n)
|
||||||
|
for i := range result {
|
||||||
|
result[i] = item
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
// InterfaceSlice convert param to slice of interface.
|
// InterfaceSlice convert param to slice of interface.
|
||||||
func InterfaceSlice(slice any) []any {
|
func InterfaceSlice(slice any) []any {
|
||||||
sv := sliceValue(slice)
|
sv := sliceValue(slice)
|
||||||
|
|||||||
@@ -684,3 +684,11 @@ func TestKeyBy(t *testing.T) {
|
|||||||
|
|
||||||
assert.Equal(result, map[int]string{1: "a", 2: "ab", 3: "abc"})
|
assert.Equal(result, map[int]string{1: "a", 2: "ab", 3: "abc"})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRepeat(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestRepeat")
|
||||||
|
|
||||||
|
result := Repeat("a", 6)
|
||||||
|
|
||||||
|
assert.Equal(result, []string{"a", "a", "a", "a", "a", "a"})
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user