mirror of
https://github.com/duke-git/lancet.git
synced 2026-11-16 05:12:11 +08:00
fix(package): [stream] bound Limit capacity by source length (#400)
This commit is contained in:
1 parent
f525703d9f
commit
70b8fc764e
2 files changed
+16
No files matched your search
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -219,6 +219,9 @@ func (s Stream[T]) Limit(maxSize int) Stream[T] {
|
|||||||
if maxSize < 0 {
|
if maxSize < 0 {
|
||||||
return FromSlice([]T{})
|
return FromSlice([]T{})
|
||||||
}
|
}
|
||||||
|
if maxSize > len(s.source) {
|
||||||
|
maxSize = len(s.source)
|
||||||
|
}
|
||||||
|
|
||||||
source := make([]T, 0, maxSize)
|
source := make([]T, 0, maxSize)
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user