From f94ae2032cec4c4e9d3aa2cc69d3c5ddbd601d0a Mon Sep 17 00:00:00 2001 From: YeonGyu-Kim Date: Sat, 7 Feb 2026 19:13:35 +0900 Subject: [PATCH] fix: ensure truncated result stays within maxLength limit --- src/shared/truncate-description.test.ts | 14 +++++++------- src/shared/truncate-description.ts | 10 +--------- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/src/shared/truncate-description.test.ts b/src/shared/truncate-description.test.ts index f6e6039c..7645de1c 100644 --- a/src/shared/truncate-description.test.ts +++ b/src/shared/truncate-description.test.ts @@ -21,9 +21,9 @@ describe("truncateDescription", () => { const result = truncateDescription(description) // then - expect(result.length).toBe(123) // 120 + "..." + expect(result.length).toBe(120) // 117 chars + "..." expect(result).toEndWith("...") - expect(result).toBe(description.slice(0, 120) + "...") + expect(result).toBe(description.slice(0, 117) + "...") }) it("respects custom max length parameter", () => { @@ -35,9 +35,9 @@ describe("truncateDescription", () => { const result = truncateDescription(description, maxLength) // then - expect(result.length).toBe(53) // 50 + "..." + expect(result.length).toBe(50) // 47 chars + "..." expect(result).toEndWith("...") - expect(result).toBe(description.slice(0, 50) + "...") + expect(result).toBe(description.slice(0, 47) + "...") }) it("handles empty string", () => { @@ -71,7 +71,7 @@ describe("truncateDescription", () => { const result = truncateDescription(description) // then - expect(result.length).toBe(123) // 120 + "..." + expect(result.length).toBe(120) expect(result).toContain("First sentence. Second sentence.") expect(result).toEndWith("...") }) @@ -84,7 +84,7 @@ describe("truncateDescription", () => { const result = truncateDescription(description) // then - expect(result.length).toBe(123) // 120 + "..." + expect(result.length).toBe(120) expect(result).toStartWith("Check out https://example.com") expect(result).toEndWith("...") }) @@ -97,7 +97,7 @@ describe("truncateDescription", () => { const result = truncateDescription(description) // then - expect(result.length).toBe(123) // 120 + "..." + expect(result.length).toBe(120) expect(result).toStartWith("Version 1.2.3") expect(result).toEndWith("...") }) diff --git a/src/shared/truncate-description.ts b/src/shared/truncate-description.ts index 6fe3c453..72fa3d08 100644 --- a/src/shared/truncate-description.ts +++ b/src/shared/truncate-description.ts @@ -1,11 +1,3 @@ -/** - * Truncates a description string to a maximum character length. - * If truncated, appends "..." to indicate continuation. - * - * @param description - The description string to truncate - * @param maxLength - Maximum character length (default: 120) - * @returns Truncated description with "..." appended if it was truncated - */ export function truncateDescription(description: string, maxLength: number = 120): string { if (!description) { return description @@ -15,5 +7,5 @@ export function truncateDescription(description: string, maxLength: number = 120 return description } - return description.slice(0, maxLength) + "..." + return description.slice(0, maxLength - 3) + "..." }