-
Notifications
You must be signed in to change notification settings - Fork 36
fix: cluster size of 0 leads to infinite loop #397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Benchmark Results
Performance regressions of 30% or more should be investigated, unless they were anticipated. Smaller regressions may be due to normal variability, as we don't use dedicated CI infrastructure. |
260b096
to
42ca8be
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @fResult, good to see more contributions from you 👍
I'll get this merged after my review is addressed
@@ -13,6 +15,14 @@ export function cluster<T, Size extends number = 2>( | |||
array: readonly T[], | |||
size: Size = 2 as Size, | |||
): Cluster<T, Size>[] { | |||
if (size <= 0 || Number.isNaN(size)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The NaN check is unnecessary, since the loop will exit when size
is NaN
.
throw new IllegalSizeError(`Size must be 1 or more, the size is ${size}`) | ||
} | ||
|
||
if (size === Number.POSITIVE_INFINITY) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here. The loop will exit just fine when size
is infinite.
@@ -13,6 +15,14 @@ export function cluster<T, Size extends number = 2>( | |||
array: readonly T[], | |||
size: Size = 2 as Size, | |||
): Cluster<T, Size>[] { | |||
if (size <= 0 || Number.isNaN(size)) { | |||
throw new IllegalSizeError(`Size must be 1 or more, the size is ${size}`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think returning []
when size <= 0
would be better. It's more concise, makes logical sense, and works fine with the Cluster<T, Size>[]
return type.
Summary
Fixes infinite loop bug when cluster() size is 0.
I chose error throwing over returning an input array due to
Cluster<T, Size>
type constraints.Additional test cases were added to verify handling of various edge cases, including negative and non-integer sizes.
Related issue, if any:
#392
For any code change,
Does this PR introduce a breaking change?
No
Bundle impact
src/array/cluster.ts
Footnotes
Function size includes the
import
dependencies of the function. ↩