@azizbecha/strkit
Version:
strkit is a utility library offering a collection of essential string functions including validation, case conversion, truncation, and more. Ideal for both JavaScript and TypeScript developers to simplify string operations in their applications.
19 lines (18 loc) • 1.01 kB
TypeScript
/**
* Truncates a string to a specified length (default: 50), optionally appending an ellipsis (`...`) or a custom suffix.
*
* If the string length exceeds the specified `maxLength`, it is truncated to fit within the limit,
* including the suffix if provided.
*
* @param str - The string to truncate.
* @param maxLength - The maximum length of the truncated string, including the suffix. Defaults to `50`.
* @param suffix - A string to append to the truncated string. Defaults to `'...'`.
* @returns The truncated string with the suffix if truncation was applied, or the original string if no truncation was needed.
*
* @example
* truncate('Hello, world!', 5); // "He..."
* truncate('TypeScript is great', 10, '...'); // "TypeSc..."
* truncate('Short', 10); // "Short" (no truncation)
* truncate('A very long string that needs truncating', undefined); // "A very long string that needs trunc..."
*/
export default function truncate(str: string, maxLength?: number, suffix?: string): string;