@knownasrazi/truncatex
v1.0.0
Published
Intelligent text truncation with ellipsis, word-boundary awareness, and custom limits.
Maintainers
Readme
truncatex
Intelligent text truncation, word-aware.
Zero dependencies - ~2 KB gzipped - TypeScript - Node + Bun + browser
Why truncatex?
Cuts text cleanly — no mid-word carnage, no off-by-one ellipsis bugs.
"hello world example" -> truncate("...", 11) -> "hello..."
"hello world example" -> truncate("...", { length: 14, wordBoundary: true }) -> "hello..."Install
bun add @knownasrazi/truncatex # or
npm install @knownasrazi/truncatexUsage
import { truncate, truncateWords } from "@knownasrazi/truncatex";
truncate("hello world", 8); // "hello..."
truncate("hello world foo bar", { length: 12, wordBoundary: true }); // "hello..."
truncateWords("one two three four", 2); // "one two"API
truncate(input: string, options?: TruncateOptions | number): string
| Option | Type | Default | Description |
| ------------ | --------- | ------- | ----------------------------------------- |
| length | number | 30 | Max output length including ellipsis |
| ellipsis | string | "..." | Suffix appended when truncating |
| wordBoundary | boolean | false | Break on word boundaries |
- Throws
RangeErrorwhenlength < 0orellipsis.length > length. - Shorthand
truncate(str, 12)equals{ length: 12 }.
truncateWords(input: string, wordLimit: number): string
Truncates to wordLimit words; 0 yields the empty string.
Development
git clone https://github.com/knownasrazi/truncatex.git
cd truncatex
bun install
bun test # run tests
bun run build # tsup -> dist/
bun run lint # biome checkLicense
truncatex - truncate without the ugliness.
