Skip to content

Replace null returns with TypeError for invalid input - #73

Draft
pavanvamsi3 with Copilot wants to merge 4 commits into
masterfrom
copilot/add-exceptions-for-invalid-input
Draft

pavanvamsi3 with Copilot wants to merge 4 commits into
masterfrom
copilot/add-exceptions-for-invalid-input

Conversation

Copilot AI commented Feb 10, 2026

Copy link
Copy Markdown

Plan: Add exceptions instead of returning null for invalid input

  • Update mean() function to throw TypeError for non-array input
  • Update median() function to throw TypeError for non-array input
  • Update mode() function to throw TypeError for non-array input
  • Update variance() function to throw TypeError for non-array input
  • Update harmonicMean() function to throw TypeError for non-array input
  • Update geometricMean() function to throw TypeError for non-array input
  • Update all existing tests that expect null to expect thrown exceptions
  • Add test for standardDeviation() exception behavior (inherits from variance)
  • Run tests to validate changes (all 21 tests passing)
  • Request code review (no issues found)
  • Run CodeQL security check (no vulnerabilities found)

Summary

Successfully implemented exception handling for invalid input across all statistical functions. Functions now throw TypeError with message "Input must be an array" instead of returning null when non-array input is provided.

Changes:

  • Modified 6 functions in index.js to throw TypeError for invalid input
  • Updated 6 test cases and added 1 new test case in test/test.js to expect thrown exceptions
  • Added explicit test for standardDeviation() error handling (it throws via variance())
  • All 21 tests pass successfully
  • No security vulnerabilities detected

Security Summary:
No security vulnerabilities were introduced or discovered by this change. CodeQL analysis found 0 alerts.

Original prompt

This section details on the original issue you should resolve

<issue_title>Add exceptions</issue_title>
<issue_description>If there is invalid input, the function returns null - throw valid exceptions instead.</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits February 10, 2026 19:26
Co-authored-by: pavanvamsi3 <4750389+pavanvamsi3@users.noreply.github.com>
Co-authored-by: pavanvamsi3 <4750389+pavanvamsi3@users.noreply.github.com>
Copilot AI changed the title [WIP] Add exceptions for invalid input handling Replace null returns with TypeError for invalid input Feb 10, 2026
Copilot AI requested a review from pavanvamsi3 February 10, 2026 19:30
@pavanvamsi3
pavanvamsi3 requested review from Copilot and removed request for pavanvamsi3 February 10, 2026 21:14

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 null return values.
  • Updated package-lock.json (including a lockfileVersion bump).

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.

Comment thread test/test.js
Comment on lines 36 to 40
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');
});

Copilot AI Feb 10, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add exceptions

3 participants