Skip to content

Commit cf4e83e

Browse files
fix(repair): keep truncated slugs idempotent
Co-authored-by: Cameron Beeley <[email protected]>
1 parent 78d244f commit cf4e83e

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

‎src/repair/text-utils.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export function slug(value: unknown, fallback = "unknown", maxLength = 120) {
2727
.toLowerCase()
2828
.replace(/[^a-z0-9_.-]+/g, "-")
2929
.replace(/^-+|-+$/g, "")
30-
.slice(0, maxLength) || fallback
30+
.slice(0, maxLength)
31+
.replace(/-+$/g, "") || fallback
3132
);
3233
}

‎test/repair/text-utils.test.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import assert from "node:assert/strict";
22
import test from "node:test";
33

4-
import { compactText } from "../../dist/repair/text-utils.js";
4+
import { compactText, slug } from "../../dist/repair/text-utils.js";
55

66
test("compactText never exceeds maxLength, even for tiny caps", () => {
77
for (const n of [0, 1, 2, 3, 5, 16]) {
@@ -17,3 +17,19 @@ test("compactText never exceeds maxLength, even for tiny caps", () => {
1717
assert.equal(compactText("abcdefghijklmnopqrstuvwxyz", 16), "abcdefghijklm...");
1818
assert.equal(compactText("abcdefghijklmnopqrstuvwxyz", 17), "abcdef ... uvwxyz");
1919
});
20+
21+
test("slug remains idempotent when truncation lands on a dash", () => {
22+
for (const [value, maxLength] of [
23+
["a a", 2],
24+
["aa----bb", 3],
25+
["My Repo Name!!", 6],
26+
["a---b", 3],
27+
] as const) {
28+
const result = slug(value, "fallback", maxLength);
29+
assert.equal(result.endsWith("-"), false);
30+
assert.equal(slug(result, "fallback", maxLength), result);
31+
}
32+
33+
assert.equal(slug("a a", "fallback", 2), "a");
34+
assert.equal(slug("---", "fallback", 2), "fallback");
35+
});

0 commit comments

Comments
 (0)