Skip to content

Commit a8336b7

Browse files
fix(text): bound trimMiddle for small caps
Co-authored-by: Cameron Beeley <[email protected]>
1 parent f8d5393 commit a8336b7

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

‎src/clawsweeper-text.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export function truncateText(value: unknown, maxLength: number): string {
1111
export function trimMiddle(text: string, maxLength: number): string {
1212
if (text.length <= maxLength) return text;
1313
const edge = Math.floor((maxLength - 120) / 2);
14+
if (edge <= 0) return text.slice(0, Math.max(0, maxLength));
1415
return `${text.slice(0, edge)}\n\n... truncated ${text.length - edge * 2} chars ...\n\n${text.slice(-edge)}`;
1516
}
1617

‎test/clawsweeper-text.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import assert from "node:assert/strict";
2+
import test from "node:test";
3+
4+
import { trimMiddle } from "../dist/clawsweeper-text.js";
5+
6+
test("trimMiddle keeps small-cap output within the requested length", () => {
7+
assert.equal(trimMiddle("x".repeat(25), 20), "x".repeat(20));
8+
assert.equal(trimMiddle("x".repeat(125), 120), "x".repeat(120));
9+
assert.equal(trimMiddle("x".repeat(125), 121), "x".repeat(121));
10+
assert.equal(trimMiddle("x".repeat(25), 0), "");
11+
assert.equal(trimMiddle("x".repeat(25), -1), "");
12+
});
13+
14+
test("trimMiddle reports a valid removed count for the middle-elision path", () => {
15+
const text = "y".repeat(5000);
16+
const output = trimMiddle(text, 4000);
17+
const match = output.match(/truncated (\d+) chars/);
18+
assert.ok(match);
19+
const removed = Number(match[1]);
20+
assert.ok(removed >= 0 && removed <= text.length);
21+
assert.ok(output.length <= 4000);
22+
assert.ok(output.startsWith("y") && output.endsWith("y"));
23+
});

0 commit comments

Comments
 (0)