mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-13 17:22:27 +08:00
feat: add Join
This commit is contained in:
@@ -968,3 +968,16 @@ func AppendIfAbsent(slice interface{}, value interface{}) interface{} {
|
||||
}
|
||||
return out.Interface()
|
||||
}
|
||||
|
||||
// AppendIfAbsent only absent append the value
|
||||
func Join(slice interface{}, separator string) string {
|
||||
sv := sliceValue(slice)
|
||||
|
||||
strs := make([]string, sv.Len())
|
||||
|
||||
for i := 0; i < sv.Len(); i++ {
|
||||
strs[i] = fmt.Sprint(sv.Index(i).Interface())
|
||||
}
|
||||
|
||||
return strings.Join(strs, separator)
|
||||
}
|
||||
|
||||
@@ -624,3 +624,15 @@ func TestAppendIfAbsent(t *testing.T) {
|
||||
assert.Equal([]string{"a", "b"}, AppendIfAbsent(str1, "a"))
|
||||
assert.Equal([]string{"a", "b", "c"}, AppendIfAbsent(str1, "c"))
|
||||
}
|
||||
|
||||
func TestJoin(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestJoin")
|
||||
|
||||
nums := []int{1, 2, 3, 4, 5}
|
||||
|
||||
result1 := Join(nums, ",")
|
||||
result2 := Join(nums, "-")
|
||||
|
||||
assert.Equal("1,2,3,4,5", result1)
|
||||
assert.Equal("1-2-3-4-5", result2)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user