Skip to content

Commit ea0c92a

Browse files
fix: use maxLength parameter in truncateOnWord instead of hardcoded value (calcom#27961)
Signed-off-by: Aritra Dey <adey01027@gmail.com> Co-authored-by: Romit <85230081+romitg2@users.noreply.github.com>
1 parent 2abefc4 commit ea0c92a

2 files changed

Lines changed: 86 additions & 3 deletions

File tree

packages/lib/text.test.ts

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from "vitest";
22

3-
import { truncate } from "./text";
3+
import { truncate, truncateOnWord } from "./text";
44

55
describe("Text util tests", () => {
66
describe("fn: truncate", () => {
@@ -69,4 +69,84 @@ describe("Text util tests", () => {
6969
}
7070
});
7171
});
72+
describe("fn: truncateOnWord", () => {
73+
it("should return the original text when it is shorter than the max length", () => {
74+
const cases = [
75+
{
76+
input: "Hello world",
77+
maxLength: 100,
78+
expected: "Hello world",
79+
},
80+
{
81+
input: "Hello world",
82+
maxLength: 11,
83+
expected: "Hello world",
84+
},
85+
];
86+
87+
for (const { input, maxLength, expected } of cases) {
88+
const result = truncateOnWord(input, maxLength);
89+
90+
expect(result).toEqual(expected);
91+
}
92+
});
93+
94+
it("should return the truncated text on the last word when it is longer than the max length", () => {
95+
const cases = [
96+
{
97+
input: "The quick brown fox jumps over the lazy dog",
98+
maxLength: 12,
99+
expected: "The quick...",
100+
},
101+
{
102+
input: "Cal.com is the scheduling infrastructure for everyone",
103+
maxLength: 14,
104+
expected: "Cal.com is...",
105+
},
106+
];
107+
108+
for (const { input, maxLength, expected } of cases) {
109+
const result = truncateOnWord(input, maxLength);
110+
111+
expect(result).toEqual(expected);
112+
}
113+
});
114+
115+
it("should return the truncated text without ellipsis when it is longer than the max length and ellipsis is false", () => {
116+
const cases = [
117+
{
118+
input: "The quick brown fox jumps over the lazy dog",
119+
maxLength: 12,
120+
ellipsis: false,
121+
expected: "The quick",
122+
},
123+
];
124+
125+
for (const { input, maxLength, ellipsis, expected } of cases) {
126+
const result = truncateOnWord(input, maxLength, ellipsis);
127+
128+
expect(result).toEqual(expected);
129+
}
130+
});
131+
132+
it("should fallback to character truncation when no spaces are present in the truncated segment", () => {
133+
const cases = [
134+
{
135+
input: "supercalifragilisticexpialidocious",
136+
maxLength: 10,
137+
expected: "supercalif...",
138+
},
139+
{
140+
input: "https://cal.com/pro/30min/extremely-long-url-without-any-spaces",
141+
maxLength: 20,
142+
expected: "https://cal.com/pro/...",
143+
},
144+
];
145+
146+
for (const { input, maxLength, expected } of cases) {
147+
const result = truncateOnWord(input, maxLength);
148+
expect(result).toEqual(expected);
149+
}
150+
});
151+
});
72152
});

packages/lib/text.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ export const truncateOnWord = (text: string, maxLength: number, ellipsis = true)
88
if (text.length <= maxLength) return text;
99

1010
// First split on maxLength chars
11-
let truncatedText = text.substring(0, 148);
11+
let truncatedText = text.substring(0, maxLength);
1212

1313
// Then split on the last space, this way we split on the last word,
1414
// which looks just a bit nicer.
15-
truncatedText = truncatedText.substring(0, Math.min(truncatedText.length, truncatedText.lastIndexOf(" ")));
15+
const lastSpaceIndex = truncatedText.lastIndexOf(" ");
16+
if (lastSpaceIndex !== -1) {
17+
truncatedText = truncatedText.substring(0, lastSpaceIndex);
18+
}
1619

1720
if (ellipsis) truncatedText += "...";
1821

0 commit comments

Comments
 (0)