@mintlify/common 1.0.518 → 1.0.519
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Truncates a string at word boundaries to ensure it doesn't exceed the specified length
|
|
3
|
+
* @param text - The text to truncate
|
|
4
|
+
* @param maxLength - Maximum allowed length
|
|
5
|
+
* @returns Truncated text that ends on a complete word, or character-truncated fallback if no word fits
|
|
6
|
+
*/
|
|
7
|
+
export declare function truncateAtWordBoundary(text: string, maxLength: number): string;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Truncates a string at word boundaries to ensure it doesn't exceed the specified length
|
|
3
|
+
* @param text - The text to truncate
|
|
4
|
+
* @param maxLength - Maximum allowed length
|
|
5
|
+
* @returns Truncated text that ends on a complete word, or character-truncated fallback if no word fits
|
|
6
|
+
*/
|
|
7
|
+
export function truncateAtWordBoundary(text, maxLength) {
|
|
8
|
+
const normalizedText = text.replace(/\s+/g, ' ').trim();
|
|
9
|
+
if (normalizedText.length <= maxLength)
|
|
10
|
+
return normalizedText;
|
|
11
|
+
const slice = normalizedText.slice(0, maxLength + 1);
|
|
12
|
+
const lastSpace = slice.lastIndexOf(' ');
|
|
13
|
+
return lastSpace === -1
|
|
14
|
+
? normalizedText.slice(0, maxLength).trim()
|
|
15
|
+
: slice.slice(0, lastSpace).trim();
|
|
16
|
+
}
|