code-change@1 · job_442477d0633c49ac920ed71002fb21cb
Add a clamp(value, min, max) function to src/math.js, export it, and add node:test cases covering values below, inside and above the range and min > max throwing a RangeError.
github.com/okbrk/harness-fixture- Gates
- 6/6 passed
- Charged
- €3.00
- Settled
- 23 September 2026
- pass
tests
npm run test exited 0
- pass
no-regression
base exit 0, result exit 0
- pass
diff-nonempty
2 file(s) changed
- pass
no-test-deletion
test files 1 -> 1, test cases 1 -> 5
- pass
secrets
engine=gitleaks; no findings in added lines
- pass
review
The diff adds clamp with a RangeError guard for min > max, exports it, and adds tests covering below/inside/above range and the min>max error, matching the task exactly.
diff --git a/src/math.js b/src/math.js
index 7d65831..ef24097 100644
--- a/src/math.js
+++ b/src/math.js
@@ -1,3 +1,10 @@
export function add(a, b) {
return a + b;
}
+
+export function clamp(value, min, max) {
+ if (min > max) {
+ throw new RangeError(`clamp: min (${min}) must not be greater than max (${max})`);
+ }
+ return Math.min(Math.max(value, min), max);
+}
diff --git a/test/math.test.js b/test/math.test.js
index 316deef..128d34a 100644
--- a/test/math.test.js
+++ b/test/math.test.js
@@ -1,7 +1,25 @@
import { test } from "node:test";
import assert from "node:assert/strict";
-import { add } from "../src/math.js";
+import { add, clamp } from "../src/math.js";
test("add", () => {
assert.equal(add(2, 3), 5);
});
+
+test("clamp returns min when value is below range", () => {
+ assert.equal(clamp(-5, 0, 10), 0);
+});
+
+test("clamp returns value when inside range", () => {
+ assert.equal(clamp(5, 0, 10), 5);
+ assert.equal(clamp(0, 0, 10), 0);
+ assert.equal(clamp(10, 0, 10), 10);
+});
+
+test("clamp returns max when value is above range", () => {
+ assert.equal(clamp(15, 0, 10), 10);
+});
+
+test("clamp throws RangeError when min > max", () => {
+ assert.throws(() => clamp(5, 10, 0), RangeError);
+});Signed with key e6c9e5728d1795a86be17ed63e43539b2de24e470426a27d1ef4ff4b46659ff0. Anyone can check the signature against our public key. How to verify