Skip to content

Commit fedd0b6

Browse files
authored
doc: explain chunkstring inconsistency (#789)
* doc: explain chunkstring inconsistency * doc: explain chunkstring inconsistency
1 parent 8211383 commit fedd0b6

File tree

8 files changed

+23
-3
lines changed

8 files changed

+23
-3
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2035,6 +2035,8 @@ lo.ChunkString("1", 2)
20352035
// []string{"1"}
20362036
```
20372037

2038+
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).
2039+
20382040
[[play](https://go.dev/play/p/__FLTuJVz54)]
20392041

20402042
### RuneLength

docs/data/core-chunk.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,8 @@ lo.Chunk([]int{0, 1, 2, 3, 4, 5, 6}, 2)
3030
// [][]int{{0, 1}, {2, 3}, {4, 5}, {6}}
3131
```
3232

33+
## Note
3334

35+
`lo.ChunkString` and `lo.Chunk` functions behave inconsistently for empty input: `lo.ChunkString("", n)` returns `[""]` instead of `[]`.
36+
37+
See https://github.com/samber/lo/issues/788

docs/data/it-chunk.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,10 @@ for chunk := range chunks {
3333
result = append(result, chunk)
3434
}
3535
// result contains [1, 2], [3, 4], [5]
36-
```
36+
```
37+
38+
## Note
39+
40+
`it.ChunkString` and `it.Chunk` functions behave inconsistently for empty input: `it.ChunkString("", n)` returns `[""]` instead of `[]`.
41+
42+
See https://github.com/samber/lo/issues/788

docs/src/pages/community.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ function Community() {
1717
<h1>Community</h1>
1818
<div className="hero--subtitle">
1919
These are places where you can ask questions and find your soulmate (no promises).
20+
<br/>
21+
"If you want to go fast, go alone. If you want to go far, go together."
2022
</div>
2123
<img className={styles.headerImg} src="/img/go-community.png" />
2224
</div>

it/string.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import "iter"
77
// ChunkString returns a sequence of strings split into groups of length size. If the string can't be split evenly,
88
// the final chunk will be the remaining characters.
99
// Play: https://go.dev/play/p/Y4mN8bB2cXw
10+
//
11+
// Note: it.ChunkString and it.Chunk functions behave inconsistently for empty input: it.ChunkString("", n) returns [""] instead of [].
12+
// See https://github.com/samber/lo/issues/788
1013
func ChunkString[T ~string](str T, size int) iter.Seq[T] {
1114
if size <= 0 {
1215
panic("it.ChunkString: size must be greater than 0")

it/string_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestChunkString(t *testing.T) {
2626
is.Equal([]string{"123456"}, slices.Collect(result4))
2727

2828
result5 := ChunkString("", 2)
29-
is.Equal([]string{""}, slices.Collect(result5))
29+
is.Equal([]string{""}, slices.Collect(result5)) // @TODO: should be [] - see https://github.com/samber/lo/issues/788
3030

3131
result6 := ChunkString("明1好休2林森", 2)
3232
is.Equal([]string{"明1", "好休", "2林", "森"}, slices.Collect(result6))

string.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ func Substring[T ~string](str T, offset int, length uint) T {
127127
// ChunkString returns a slice of strings split into groups of length size. If the string can't be split evenly,
128128
// the final chunk will be the remaining characters.
129129
// Play: https://go.dev/play/p/__FLTuJVz54
130+
//
131+
// Note: lo.ChunkString and lo.Chunk functions behave inconsistently for empty input: lo.ChunkString("", n) returns [""] instead of [].
132+
// See https://github.com/samber/lo/issues/788
130133
func ChunkString[T ~string](str T, size int) []T {
131134
if size <= 0 {
132135
panic("lo.ChunkString: size must be greater than 0")

string_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func TestChunkString(t *testing.T) {
4848
is.Equal([]string{"123456"}, result4)
4949

5050
result5 := ChunkString("", 2)
51-
is.Equal([]string{""}, result5)
51+
is.Equal([]string{""}, result5) // @TODO: should be [] - see https://github.com/samber/lo/issues/788
5252

5353
result6 := ChunkString("明1好休2林森", 2)
5454
is.Equal([]string{"明1", "好休", "2林", "森"}, result6)

0 commit comments

Comments
 (0)