Skip to content

Add a clamp function to math.js #24

Mergedsyntaqx merged into mainrequested by syntaqx

Approved by g1t

ResolvedAdd a clamp function to math.js#23

Merged into main by syntaqx · 8848cc1

g1t finished its merge queue run

Done

Agent confidence: Medium — 2 revisions

g1tbotopened this pull request for syntaqx

I added clamp(value, min, max) to math.js. It returns value limited to [min, max] and throws a RangeError when min > max. It has a JSDoc block, which the House style check in checks.yml requires, and it's added to the exports. test.js now has tests for a value inside the range, below it, above it, min == max, and the min > max error. node test.js passes locally. I did not run the House style check itself.

The new function and its export edit sit just above the module.exports line. PRs #5, #12 and #14 also edit that line in math.js, so whichever merges second will probably need a small conflict resolution there.

syntaqxmarked this ready for review·
g1tbotrequested a review from g1t·
g1tbotapproved these changes

The clamp function is correctly implemented and well-tested. Tests pass (node test.js), House style check passes (JSDoc block present), and the function handles all cases: values inside, below, and above the range, the edge case where min == max, and the error case where min > max. The implementation using Math.min(Math.max(value, min), max) is correct and the error message is clear. Code style is consistent with existing functions in the file.

Reviewed by g1t on Claude Haiku 4.5.

syntaqxadded this to the merge queue·
g1tbotwas taken out of the merge queue. Its change conflicts with the default branch in math.js. Bring it up to date with the default branch, and merge it again.·
g1tbotsent g1t back to fix what failed in the merge queue·
g1tbotrequested a review from g1t·
g1tbotapproved these changes

Checked clamp(value, min, max) in math.js and its tests in test.js against issue #23.

  • Logic: Math.min(Math.max(value, min), max) returns value limited to [min, max] for min <= max. Throws RangeError when min > max, as the issue requires.
  • Tests: node test.js passes. Cases cover inside range, below, above, min == max, and the min > max error.
  • House style: clamp has a /** */ JSDoc block directly above it and is added to module.exports. I ran the House style loop from .g1t/workflows/checks.yml locally and it passes.
  • Nothing else in the file changed behavior.

Non-blocking: if min or max is NaN, the min > max check is false, so no error is thrown and the function returns NaN. The issue does not cover this case, so it can stay as is.

Conclusion: the change does what issue #23 asks and does not break existing functions or tests.

Reviewed by g1t on Claude Haiku 5.5.

g1tbotasked g1t to bring this up to date with main·
syntaqxadded this to the merge queue·
g1tbotwas taken out of the merge queue. Its change conflicts with the default branch in math.js. Bring it up to date with the default branch, and merge it again.·
g1tbotsent g1t back to fix what failed in the merge queue·
g1tbotrequested a review from g1t·
g1tbotapproved these changes

Checked the diff against issue #23 and read the surrounding code in math.js and test.js.

  • clamp(value, min, max) returns Math.min(Math.max(value, min), max) and throws RangeError when min > max. This matches the issue.
  • The new export is added to module.exports.
  • node test.js passes locally (exit 0).
  • I ran the House style loop from .g1t/workflows/checks.yml against the current math.js. It passes, because clamp has a one-line /** ... */ directly above it.
  • The tests cover inside range, below min, above max, min == max, and the min > max error, as the PR describes.
  • Edge cases: clamp(NaN, 0, 10) returns NaN, which is consistent with Math.max/Math.min. It is not documented and not tested, but it is not a defect for this issue.

No blocking problems. Per the PR description, PRs #5, #12 and #14 edit the same module.exports line, so the second one to merge will need a small conflict resolution.

Reviewed by g1t on Claude Haiku 5.5.

syntaqxadded this to the merge queue·
syntaqxmerged this·

All checks have passed

8 passed · on 8848cc1

Workflows

After build / workflow_run — After build passedDetails
Build / push — Build passedDetails
CI / push — CI passedDetails
Checks / merge_group — Checks passedDetails
Checks / pull_request — Checks passedDetails
Checks / push — Checks passedDetails
Dependency review — No dependency changesDetails
g1t / deploy — Production is live

Sign in to comment.