The lodash method _.truncate exported as a module for string truncation with Unicode support and flexible formatting options.
npx @tessl/cli install tessl/npm-lodash--truncate@4.4.0Lodash Truncate provides intelligent string truncation with comprehensive Unicode support and flexible formatting options. It safely shortens text to specified lengths while preserving word boundaries and supporting complex Unicode characters including astral plane characters, combining marks, and zero-width joiners.
npm install lodash.truncateconst truncate = require('lodash.truncate');For ES modules:
import truncate from 'lodash.truncate';const truncate = require('lodash.truncate');
// Basic truncation (default: 30 characters with '...')
truncate('hi-diddly-ho there, neighborino');
// => 'hi-diddly-ho there, neighbo...'
// Custom length
truncate('hi-diddly-ho there, neighborino', { length: 24 });
// => 'hi-diddly-ho there, nei...'
// Smart word boundary truncation
truncate('hi-diddly-ho there, neighborino', {
length: 24,
separator: ' '
});
// => 'hi-diddly-ho there,...'
// Custom omission indicator
truncate('hi-diddly-ho there, neighborino', {
omission: ' [...]'
});
// => 'hi-diddly-ho there, neig [...]'Truncates strings to a specified length with intelligent Unicode handling and configurable formatting options.
/**
* Truncates string if it's longer than the given maximum string length.
* The last characters of the truncated string are replaced with the omission
* string which defaults to "...".
*
* @param {string} [string=''] - The string to truncate
* @param {Object} [options={}] - The options object
* @param {number} [options.length=30] - The maximum string length
* @param {string} [options.omission='...'] - The string to indicate text is omitted
* @param {RegExp|string} [options.separator] - The separator pattern to truncate to
* @returns {string} Returns the truncated string
*/
function truncate(string, options);Parameters:
Returns: (string) The truncated string, or the original string if no truncation was needed.
Key Features:
Usage Examples:
// Unicode string handling
truncate('πΊπΈπ¨βπ©βπ§βπ¦ Family emoji test', { length: 10 });
// Properly handles complex emoji sequences
// Regular expression separator
truncate('one, two, three, four, five', {
length: 20,
separator: /,\s*/
});
// => 'one, two, three...'
// String separator for word boundaries
truncate('The quick brown fox jumps over the lazy dog', {
length: 25,
separator: ' '
});
// => 'The quick brown fox...'
// Custom omission with length consideration
truncate('Hello world this is a test', {
length: 15,
omission: '...'
});
// => 'Hello world...' (omission length is included in total length)
// No truncation needed
truncate('Short text', { length: 50 });
// => 'Short text' (returned unchanged)
// Edge case: omission longer than allowed length
truncate('Hello', { length: 2, omission: '...' });
// => '...' (returns just the omission when content can't fit)// Options interface (TypeScript representation for documentation)
interface TruncateOptions {
length?: number; // Maximum string length (default: 30)
omission?: string; // Text to indicate truncation (default: '...')
separator?: RegExp | string; // Pattern for smart boundary truncation
}