๐Ÿ“ text-smart-trimmer

April 22, 2025 ยท View on GitHub

A lightweight TypeScript utility library for intelligently trimming text strings. This library provides an easy way to trim text to specified lengths while preserving whole words or cutting mid-word, and handling punctuation. Perfect for summaries, previews, and UI components where clean text presentation matters.

npm build license TypeScript codecov

๐Ÿ“ฆ View on npm

โšก Installation

Choose your package manager:

npm install text-smart-trimmer
yarn add text-smart-trimmer
pnpm add text-smart-trimmer

๐Ÿš€ Usage

import { smartTrim } from 'text-smart-trimmer';

// Basic usage with default options
smartTrim('This is a long sentence that needs to be trimmed', 20);
// Output: "This is a long..."

CommonJS

const { smartTrim } = require('text-smart-trimmer');

// Basic usage with default options
smartTrim('This is a long sentence that needs to be trimmed', 20);
// Output: "This is a long..."

๐Ÿ’ก Examples

// Returns original string if shorter than maxLength
smartTrim('Short text', 20); // "Short text"

// Custom suffix
smartTrim('This is a long sentence', 15, { suffix: ' [more]' });
// Output: "This is [more]"

// Cut words in the middle (disable whole word preservation)
smartTrim('This is a long sentence', 15, { preserveWholeWords: false });
// Output: "This is a lo..."

// Remove trailing punctuation
smartTrim('This is a sentence, with punctuation.', 22, { preservePunctuation: false });
// Output: "This is a sentence..."

๐Ÿ“‹ API

smartTrim(str, maxLength, options?)

function smartTrim(
  str: string,
  maxLength: number,
  options?: SmartTrimOptions
): string

Parameters

ParameterTypeDescription
strstringThe text to trim
maxLengthnumberMaximum character length (including suffix)
optionsSmartTrimOptionsOptional configuration

SmartTrimOptions

OptionTypeDefaultDescription
suffixstring"..."Text to append after trimming
preserveWholeWordsbooleantrueKeep words intact or cut mid-word
preservePunctuationbooleantrueKeep or remove trailing punctuation

โœจ Key Features

  • Smart word handling: Automatically trims at word boundaries (configurable)
  • Customizable suffix: Use any string as the truncation indicator
  • Punctuation awareness: Intelligently handles trailing punctuation
  • Thoroughly tested: Handles all edge cases reliably
  • Zero dependencies: Lightweight and efficient
  • TypeScript ready: Full type definitions included

๐Ÿงฉ Behavior Details

  • Returns the original string if it's already shorter than maxLength
  • Truncates the suffix if maxLength is smaller than the suffix length
  • When preserveWholeWords is enabled with very long words, returns only the suffix
  • Strict input validation ensures predictable behavior

๐Ÿ“ฆ Why Use This Library?

While text trimming seems simple, handling all the edge cases correctly can be tricky:

  • What if there are no spaces?
  • How should punctuation be handled?
  • What if the max length is very small?

This library has been thoroughly tested against dozens of edge cases so you don't have to worry about them.

๐Ÿ“„ License

MIT