GeezerVison is showing github.com — fix(core): toBeOdd fails on negative odd numbers by KeylaMunnoz · Pull Request #209 · stackbuilders/assertive-ts
Conversation
Fixes #191
Description
In JavaScript, the remainder operator (%) retains the sign of the dividend (e.g., -13 % 2 === -1). As a result, NumberAssertion.toBeOdd() was failing for negative odd numbers because it checked this.actual % 2 === 1.
Changes
- Fixed
NumberAssertion.toBeOdd to check Math.abs(this.actual % 2) === 1.
- Documented parity behavior in JSDoc for both
toBeEven and toBeOdd, clarifying that parity applies strictly to integers: non-integers, NaN, and Infinity are neither even nor odd, while 0 and -0 are considered even.
- Added comprehensive unit tests in
NumberAssertion.test.ts covering negative odd numbers (-13), negative even numbers (-2), 0, -0, non-integers (1.5, -1.5), NaN, and Infinity.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
rojasTob
commented
Nov 13, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
Thanks for the fix! Math.abs(this.actual % 2) === 1 handles negative odd numbers, fractions, NaN, ±Infinity and -0 correctly. A few things before merging:
Please add these tests:
-1 for toBeOdd (the smallest negative odd number)
-0 for toBeOdd (it should fail directly and pass under .not)
-Infinity for both matchers, and -1.5 for toBeEven
The bug only shows up with negative numbers, so these are the cases most likely to catch a regression. AGENTS.md also asks for a boundary case and a negative-number case for this kind of matcher.
Optional:
- The
.not errors for toBeOdd/toBeEven don't show the value ("Expected the value NOT to be odd"). Since you're already in these methods, Expected <${this.actual}> NOT to be odd would match the other matchers.
Number.isInteger(x) && x % 2 !== 0 states the "only integers have parity" rule from your JSDoc more directly than relying on % returning NaN or a fraction.
- The JSDoc note mentions
Infinity; it could say ±Infinity.
Happy to approve once the tests are in.
|
KeylaMunnoz reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Hey @rojasTob ! Thanks for the review !
Added all the requested test cases for negative numbers, negative zero, negative infinity, and negative decimals, including the expected behavior with .not.
|
rojasTob reacted with eyes emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This branch has not been deployed
No deployments
Reviewers
rojasTob
rojasTob approved these changes
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|