mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 21:02:27 +08:00
feat: add Join
This commit is contained in:
@@ -90,6 +90,7 @@ import (
|
||||
- [UpdateAt](#UpdateAt)
|
||||
- [Without](#Without)
|
||||
- [KeyBy](#KeyBy)
|
||||
- [Join](#Join)
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
@@ -2417,3 +2418,36 @@ func main() {
|
||||
// map[1:a 2:ab 3:abc]
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Join">Join</span>
|
||||
|
||||
<p>Join the slice item with specify separator.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func Join[T any](s []T, separator string) string
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/slice"
|
||||
)
|
||||
|
||||
func main() {
|
||||
nums := []int{1, 2, 3, 4, 5}
|
||||
|
||||
result1 := slice.Join(nums, ",")
|
||||
result2 := slice.Join(nums, "-")
|
||||
|
||||
fmt.Println(result1)
|
||||
fmt.Println(result2)
|
||||
|
||||
// Output:
|
||||
// 1,2,3,4,5
|
||||
// 1-2-3-4-5
|
||||
}
|
||||
```
|
||||
|
||||
@@ -90,6 +90,7 @@ import (
|
||||
- [UpdateAt](#UpdateAt)
|
||||
- [Without](#Without)
|
||||
- [KeyBy](#KeyBy)
|
||||
- [Join](#Join)
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
@@ -2390,7 +2391,7 @@ func main() {
|
||||
|
||||
### <span id="KeyBy">KeyBy</span>
|
||||
|
||||
<p>将切片每个元素调用函数后转为map</p>
|
||||
<p>将切片每个元素调用函数后转为map。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
@@ -2417,3 +2418,36 @@ func main() {
|
||||
// map[1:a 2:ab 3:abc]
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Join">Join</span>
|
||||
|
||||
<p>用指定的分隔符链接切片元素。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func Join[T any](s []T, separator string) string
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
|
||||
```go
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/slice"
|
||||
)
|
||||
|
||||
func main() {
|
||||
nums := []int{1, 2, 3, 4, 5}
|
||||
|
||||
result1 := slice.Join(nums, ",")
|
||||
result2 := slice.Join(nums, "-")
|
||||
|
||||
fmt.Println(result1)
|
||||
fmt.Println(result2)
|
||||
|
||||
// Output:
|
||||
// 1,2,3,4,5
|
||||
// 1-2-3-4-5
|
||||
}
|
||||
```
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"math/rand"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"golang.org/x/exp/constraints"
|
||||
@@ -1188,3 +1189,13 @@ func KeyBy[T any, U comparable](slice []T, iteratee func(item T) U) map[U]T {
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// Join the slice item with specify separator.
|
||||
// Play: todo
|
||||
func Join[T any](s []T, separator string) string {
|
||||
str := Map(s, func(_ int, item T) string {
|
||||
return fmt.Sprint(item)
|
||||
})
|
||||
|
||||
return strings.Join(str, separator)
|
||||
}
|
||||
|
||||
@@ -1032,3 +1032,17 @@ func ExampleKeyBy() {
|
||||
// Output:
|
||||
// map[1:a 2:ab 3:abc]
|
||||
}
|
||||
|
||||
func ExampleJoin() {
|
||||
nums := []int{1, 2, 3, 4, 5}
|
||||
|
||||
result1 := Join(nums, ",")
|
||||
result2 := Join(nums, "-")
|
||||
|
||||
fmt.Println(result1)
|
||||
fmt.Println(result2)
|
||||
|
||||
// Output:
|
||||
// 1,2,3,4,5
|
||||
// 1-2-3-4-5
|
||||
}
|
||||
|
||||
@@ -1161,3 +1161,17 @@ func TestRepeat(t *testing.T) {
|
||||
assert.Equal([]string{}, Repeat("a", 0))
|
||||
assert.Equal([]string{"a", "a", "a", "a", "a", "a"}, Repeat("a", 6))
|
||||
}
|
||||
|
||||
func TestJoin(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
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