or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-lodash--truncate

The lodash method _.truncate exported as a module for string truncation with Unicode support and flexible formatting options.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash.truncate@4.4.x

To install, run

npx @tessl/cli install tessl/npm-lodash--truncate@4.4.0

index.mddocs/

Lodash Truncate

Lodash 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.

Package Information

  • Package Name: lodash.truncate
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash.truncate

Core Imports

const truncate = require('lodash.truncate');

For ES modules:

import truncate from 'lodash.truncate';

Basic Usage

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 [...]'

Capabilities

String Truncation

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:

  • string (string, optional, default=''): The string to truncate. Any non-string value will be converted to string.
  • options (Object, optional, default={}): Configuration object with the following properties:
    • length (number, optional, default=30): The maximum string length for the truncated result
    • omission (string, optional, default='...'): String to append to indicate text was omitted
    • separator (RegExp|string, optional): Pattern to truncate at for smart word/phrase boundary handling

Returns: (string) The truncated string, or the original string if no truncation was needed.

Key Features:

  • Unicode Support: Properly handles astral plane characters, combining marks, and zero-width joiners
  • Smart Truncation: When separator is provided, truncates at word/phrase boundaries instead of character boundaries
  • Flexible Separators: Supports both string patterns and regular expressions for separator matching
  • Type Coercion: Automatically converts non-string inputs to strings before processing
  • Precision Length Calculation: Uses Unicode-aware length calculation for accurate character counting

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)

Types

// 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
}