Replace null returns with TypeError for invalid input - #73
pavanvamsi3 with Copilot wants to merge 4 commits into
Conversation
Co-authored-by: pavanvamsi3 <4750389+pavanvamsi3@users.noreply.github.com>
Co-authored-by: pavanvamsi3 <4750389+pavanvamsi3@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR updates the math-stats public API to fail fast by throwing TypeError('Input must be an array') for non-array inputs, instead of returning null, and adjusts the test suite accordingly.
Changes:
- Updated core statistical functions to throw
TypeError('Input must be an array')on non-array inputs. - Updated unit tests to assert exceptions rather than
nullreturn values. - Updated
package-lock.json(including alockfileVersionbump).
Reviewed changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| index.js | Replaces null returns with TypeError throws for non-array inputs across statistical functions. |
| test/test.js | Updates invalid-input assertions to expect thrown TypeError with the standardized message. |
| package-lock.json | Regenerated lockfile; bumps lockfileVersion to 3 and restructures lockfile format. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| it('Not an array error variance', function() { | ||
| var result = stats.variance(1); | ||
| expect(result).to.equal(null); | ||
| expect(function() { | ||
| stats.variance(1); | ||
| }).to.throw(TypeError, 'Input must be an array'); | ||
| }); |
There was a problem hiding this comment.
standardDeviation() now throws on non-array inputs indirectly via variance(), but there isn’t a corresponding test asserting this new behavior. Add a test similar to the other “Not an array error …” cases so the contract change for standardDeviation(1) is covered explicitly.
There was a problem hiding this comment.
Added test case "Not an array error standard deviation" to explicitly cover the exception-throwing behavior when standardDeviation() receives non-array input (commit 0a09edc).
Co-authored-by: pavanvamsi3 <4750389+pavanvamsi3@users.noreply.github.com>
Plan: Add exceptions instead of returning null for invalid input
mean()function to throw TypeError for non-array inputmedian()function to throw TypeError for non-array inputmode()function to throw TypeError for non-array inputvariance()function to throw TypeError for non-array inputharmonicMean()function to throw TypeError for non-array inputgeometricMean()function to throw TypeError for non-array inputstandardDeviation()exception behavior (inherits from variance)Summary
Successfully implemented exception handling for invalid input across all statistical functions. Functions now throw
TypeErrorwith message "Input must be an array" instead of returningnullwhen non-array input is provided.Changes:
index.jsto throw TypeError for invalid inputtest/test.jsto expect thrown exceptionsstandardDeviation()error handling (it throws viavariance())Security Summary:
No security vulnerabilities were introduced or discovered by this change. CodeQL analysis found 0 alerts.
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.