mirror of
https://github.com/samber/lo.git
synced 2026-04-22 23:47:11 +08:00
doc: explain chunkstring inconsistency (#789)
* doc: explain chunkstring inconsistency * doc: explain chunkstring inconsistency
This commit is contained in:
@@ -2035,6 +2035,8 @@ lo.ChunkString("1", 2)
|
||||
// []string{"1"}
|
||||
```
|
||||
|
||||
Note: `lo.ChunkString` and `lo.Chunk` functions behave inconsistently for empty input: `lo.ChunkString("", n)` returns `[""]` instead of `[]`. See [#788](https://github.com/samber/lo/issues/788).
|
||||
|
||||
[[play](https://go.dev/play/p/__FLTuJVz54)]
|
||||
|
||||
### RuneLength
|
||||
|
||||
@@ -30,4 +30,8 @@ lo.Chunk([]int{0, 1, 2, 3, 4, 5, 6}, 2)
|
||||
// [][]int{{0, 1}, {2, 3}, {4, 5}, {6}}
|
||||
```
|
||||
|
||||
## Note
|
||||
|
||||
`lo.ChunkString` and `lo.Chunk` functions behave inconsistently for empty input: `lo.ChunkString("", n)` returns `[""]` instead of `[]`.
|
||||
|
||||
See https://github.com/samber/lo/issues/788
|
||||
|
||||
@@ -33,4 +33,10 @@ for chunk := range chunks {
|
||||
result = append(result, chunk)
|
||||
}
|
||||
// result contains [1, 2], [3, 4], [5]
|
||||
```
|
||||
```
|
||||
|
||||
## Note
|
||||
|
||||
`it.ChunkString` and `it.Chunk` functions behave inconsistently for empty input: `it.ChunkString("", n)` returns `[""]` instead of `[]`.
|
||||
|
||||
See https://github.com/samber/lo/issues/788
|
||||
|
||||
@@ -17,6 +17,8 @@ function Community() {
|
||||
<h1>Community</h1>
|
||||
<div className="hero--subtitle">
|
||||
These are places where you can ask questions and find your soulmate (no promises).
|
||||
<br/>
|
||||
"If you want to go fast, go alone. If you want to go far, go together."
|
||||
</div>
|
||||
<img className={styles.headerImg} src="/img/go-community.png" />
|
||||
</div>
|
||||
|
||||
@@ -7,6 +7,9 @@ import "iter"
|
||||
// ChunkString returns a sequence of strings split into groups of length size. If the string can't be split evenly,
|
||||
// the final chunk will be the remaining characters.
|
||||
// Play: https://go.dev/play/p/Y4mN8bB2cXw
|
||||
//
|
||||
// Note: it.ChunkString and it.Chunk functions behave inconsistently for empty input: it.ChunkString("", n) returns [""] instead of [].
|
||||
// See https://github.com/samber/lo/issues/788
|
||||
func ChunkString[T ~string](str T, size int) iter.Seq[T] {
|
||||
if size <= 0 {
|
||||
panic("it.ChunkString: size must be greater than 0")
|
||||
|
||||
+1
-1
@@ -26,7 +26,7 @@ func TestChunkString(t *testing.T) {
|
||||
is.Equal([]string{"123456"}, slices.Collect(result4))
|
||||
|
||||
result5 := ChunkString("", 2)
|
||||
is.Equal([]string{""}, slices.Collect(result5))
|
||||
is.Equal([]string{""}, slices.Collect(result5)) // @TODO: should be [] - see https://github.com/samber/lo/issues/788
|
||||
|
||||
result6 := ChunkString("明1好休2林森", 2)
|
||||
is.Equal([]string{"明1", "好休", "2林", "森"}, slices.Collect(result6))
|
||||
|
||||
@@ -127,6 +127,9 @@ func Substring[T ~string](str T, offset int, length uint) T {
|
||||
// ChunkString returns a slice of strings split into groups of length size. If the string can't be split evenly,
|
||||
// the final chunk will be the remaining characters.
|
||||
// Play: https://go.dev/play/p/__FLTuJVz54
|
||||
//
|
||||
// Note: lo.ChunkString and lo.Chunk functions behave inconsistently for empty input: lo.ChunkString("", n) returns [""] instead of [].
|
||||
// See https://github.com/samber/lo/issues/788
|
||||
func ChunkString[T ~string](str T, size int) []T {
|
||||
if size <= 0 {
|
||||
panic("lo.ChunkString: size must be greater than 0")
|
||||
|
||||
+1
-1
@@ -48,7 +48,7 @@ func TestChunkString(t *testing.T) {
|
||||
is.Equal([]string{"123456"}, result4)
|
||||
|
||||
result5 := ChunkString("", 2)
|
||||
is.Equal([]string{""}, result5)
|
||||
is.Equal([]string{""}, result5) // @TODO: should be [] - see https://github.com/samber/lo/issues/788
|
||||
|
||||
result6 := ChunkString("明1好休2林森", 2)
|
||||
is.Equal([]string{"明1", "好休", "2林", "森"}, result6)
|
||||
|
||||
Reference in New Issue
Block a user