From 70b8fc764e56d87faf034421aebdac9d7784882e Mon Sep 17 00:00:00 2001 From: Yusuf Efe <120668197+yuefdev@users.noreply.github.com> Date: Thu, 22 Oct 2026 05:39:28 +0300 Subject: [PATCH] fix(package): [stream] bound Limit capacity by source length (#400) --- stream/limit_capacity_test.go | 13 +++++++++++++ stream/stream.go | 3 +++ 2 files changed, 16 insertions(+) create mode 100644 stream/limit_capacity_test.go diff --git a/stream/limit_capacity_test.go b/stream/limit_capacity_test.go new file mode 100644 index 0000000..6c88741 --- /dev/null +++ b/stream/limit_capacity_test.go @@ -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) + } +} diff --git a/stream/stream.go b/stream/stream.go index 7c2acd8..a4797de 100644 --- a/stream/stream.go +++ b/stream/stream.go @@ -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)