fix(package): [stream] bound Limit capacity by source length (#400)

This commit is contained in:
Yusuf Efe authored and GitHub committed 2026-10-22 10:39:28 +08:00
1 parent f525703d9f
commit 70b8fc764e
2 files changed
+16

No files matched your search

+13
View File
@@ -0,0 +1,13 @@
package stream
import (
"math"
"testing"
)
func TestLimitLargerThanSource(t *testing.T) {
got := Of(1, 2).Limit(math.MaxInt).ToSlice()
if len(got) != 2 || got[0] != 1 || got[1] != 2 {
t.Errorf("unexpected limited stream: %v", got)
}
}
+3
View File
@@ -219,6 +219,9 @@ func (s Stream[T]) Limit(maxSize int) Stream[T] {
if maxSize < 0 {
return FromSlice([]T{})
}
if maxSize > len(s.source) {
maxSize = len(s.source)
}
source := make([]T, 0, maxSize)