@tienedev/datype
Version:
Modern TypeScript utility library with pragmatic typing and zero dependencies
110 lines • 3.76 kB
TypeScript
/**
* Truncates a string if it's longer than the specified length.
* The last characters of the truncated string are replaced with the omission string.
*
* @param str - The string to truncate
* @param length - The maximum length of the truncated string (default: 30)
* @param omission - The string used to indicate omitted characters (default: '...')
* @returns A truncated string if needed, otherwise the original string
*
* @example
* ```typescript
* import { truncate } from 'datype';
*
* // Basic usage
* truncate('Hello world, this is a long string', 20);
* // 'Hello world, this...'
*
* // Custom omission
* truncate('Hello world, this is a long string', 20, '…');
* // 'Hello world, this i…'
*
* // Custom omission with multiple characters
* truncate('Hello world, this is a long string', 20, ' [more]');
* // 'Hello world [more]'
*
* // String shorter than limit
* truncate('Short text', 20);
* // 'Short text'
*
* // Empty omission
* truncate('Hello world, this is a long string', 15, '');
* // 'Hello world, th'
*
* // Real-world examples
* const title = 'Understanding Advanced TypeScript Utility Types';
* truncate(title, 30); // 'Understanding Advanced...'
*
* const description = 'This is a very long product description that needs to be shortened';
* truncate(description, 50, '... read more');
* // 'This is a very long product descr... read more'
*
* // For URLs
* const url = 'https://www.example.com/very/long/path/to/resource';
* truncate(url, 30, '...');
* // 'https://www.example.com/...'
* ```
*/
export declare function truncate(str: string, length?: number, omission?: string): string;
/**
* Truncates a string at the nearest word boundary to avoid cutting words in half.
*
* @param str - The string to truncate
* @param length - The maximum length of the truncated string
* @param omission - The string used to indicate omitted characters (default: '...')
* @returns A truncated string at word boundary if possible
*
* @example
* ```typescript
* import { truncateWords } from 'datype';
*
* // Truncate at word boundary
* truncateWords('Hello world this is a test', 15);
* // 'Hello world...'
*
* // Compare with regular truncate
* truncate('Hello world this is a test', 15);
* // 'Hello world thi...'
*
* // Long single word
* truncateWords('Supercalifragilisticexpialidocious', 15);
* // 'Supercalifrag...' (falls back to character truncation)
*
* // No spaces in limit
* truncateWords('Hello world', 5);
* // 'He...' (falls back to character truncation)
* ```
*/
export declare function truncateWords(str: string, length: number, omission?: string): string;
/**
* Truncates text in the middle, keeping both the beginning and end visible.
* Useful for file paths, URLs, or other strings where both ends are important.
*
* @param str - The string to truncate
* @param length - The maximum length of the truncated string
* @param omission - The string used to indicate omitted characters (default: '...')
* @returns A string truncated in the middle
*
* @example
* ```typescript
* import { truncateMiddle } from 'datype';
*
* // File paths
* truncateMiddle('/very/long/path/to/important/file.txt', 25);
* // '/very/long/.../file.txt'
*
* // URLs
* truncateMiddle('https://www.example.com/very/long/path/page.html', 35);
* // 'https://www.example.com/.../page.html'
*
* // Email addresses
* truncateMiddle('verylongemailaddress@example.com', 25);
* // 'verylongemail...@example.com'
*
* // Custom omission
* truncateMiddle('abcdefghijklmnopqrstuvwxyz', 15, '…');
* // 'abcdef…tuvwxyz'
* ```
*/
export declare function truncateMiddle(str: string, length: number, omission?: string): string;
//# sourceMappingURL=index.d.ts.map