Practical Case Studies · JavaScript · Patch · Text · Markdown

Programming change case study

A before-and-after clamp utility change that rejects reversed bounds, with a patch and six passing assertions.

Use it to: inspect a bounded code change, its acceptance test, and retained evidence.

← Back to Free Resources

Disposable programming case study

Goal: reject reversed bounds in a pure clamp(value, min, max) utility.

Acceptance criteria:

  1. values inside the interval are unchanged;
  2. values below or above are clamped;
  3. exact boundaries are preserved;
  4. min greater than max throws RangeError.

Files: before.mjs, after.mjs, change.patch, test.mjs, and test-results.txt.

Reproduce with Node 26:

command
node test.mjs

Verified on 2026-10-27 with Node v26.8.1: PASS 6 clamp assertions.

Limits: this is a synthetic disposable utility. Packaging, browser behavior, accessibility, dependencies, integration, and deployment are NOT_TESTED.

Included source files

before.mjs
export function clamp(value, min, max) {
  return Math.min(max, Math.max(min, value));
}

after.mjs
export function clamp(value, min, max) {
  if (min > max) throw new RangeError('min must not exceed max');
  return Math.min(max, Math.max(min, value));
}

change.patch

Download this file
change.patch
--- before.mjs
+++ after.mjs
@@
 export function clamp(value, min, max) {
+  if (min > max) throw new RangeError('min must not exceed max');
   return Math.min(max, Math.max(min, value));
 }

test.mjs
import assert from 'node:assert/strict';
import { clamp } from './after.mjs';

assert.equal(clamp(5, 0, 10), 5);
assert.equal(clamp(-2, 0, 10), 0);
assert.equal(clamp(12, 0, 10), 10);
assert.equal(clamp(0, 0, 10), 0);
assert.equal(clamp(10, 0, 10), 10);
assert.throws(() => clamp(5, 10, 0), RangeError);
console.log('PASS 6 clamp assertions');

test-results.txt

Download this file
test-results.txt
Date: 2026-10-27
Runtime: Node v26.8.1
Command: node test.mjs
Result: PASS 6 clamp assertions

← Back to Free Resources