@fiquu/shorten
Version:
Shortens a string (truncate) optionally keeping whole words.
40 lines (39 loc) • 1.25 kB
TypeScript
export interface ShortenOptions {
/**
* Maximum string length.
*/
length?: number;
/**
* String to use as ellipsis.
*/
ellipsis?: string;
/**
* Whether to keep whole words.
*/
words?: boolean;
/**
* Whether to count ellipsis' length in the maximum length.
*/
lax?: boolean;
}
/**
* Shortens (truncates) a string to a max length keeping whole words by default.
*
* @param {string} value The string to shorten.
* @param {object} options The options object.
* @param {number} options.length Maximum length to return. Defaults to `50`.
* @param {string} options.ellipsis Ellipsis string to use. Defaults to `'...'`.
* @param {boolean} options.words Whether to keep words. Defaults to `true`.
* @param {boolean} options.lax Whether to not include the ellipsis length in the maximum length. Defaults to `false`.
*
* @example
* // Returns 'The...'
* shorten('The strnig is logn and full of erorrs.');
* @example
* // Returns 'The strnig is...'
* shorten('The strnig is logn and full of erorrs.', { length: 20 });
*
* @returns {string} The shortened (truncated) string.
*/
declare const shorten: (value: string, options?: ShortenOptions) => string;
export default shorten;