truncate-text-between-words
Version:
Truncates the text at the last space before the given length.
22 lines (21 loc) • 1.13 kB
TypeScript
/**
* Returns the position of the last space in the string that's before the given index
* @param {String} text The text that will be searched
* @param {Number} index Searches for last space before this index
* @returns {Number} The position of the space
*/
declare function getPositionOfLastSpaceBeforeIndex(text: string, index: number): number;
type TruncateTextOptions = {
hideIfNoWords?: boolean;
dotsInsteadOfEllipsis?: boolean;
};
/**
* Truncates text on the last space before the given index, and adds '...' at the end
* @param {String} text Text to be truncated
* @param {Number} maxLength Desired max length. Output might be shorter because it's truncated on the last space found before the given maxLength
* @param {Boolean} options.hideIfNoWords Hide the ellipsis if there are no words to show
* @param {Boolean} options.ellipsisInstedOfDots Use the ellipsis unicode character U+2026 instead of three dots
* @returns Truncated text
*/
declare function truncateText(text: string, maxLength: number, options?: TruncateTextOptions): string;
export { getPositionOfLastSpaceBeforeIndex, truncateText };