Textwrap for JavaScript/Node.js, written in TypeScript.
Correctly handles wide characters (宽字符) and emojis (😃).
Optionally break words when wrapping strings. Preserves ANSI escape codes.
Ships compiled CommonJS + declaration files (.d.ts).
npm install smartwrapCLI (global):
npm install -g smartwrap// ESM
import smartwrap from 'smartwrap'
// or: import { smartwrap } from 'smartwrap'
// CommonJS
const smartwrap = require('smartwrap')
console.log(smartwrap('宽字符', { width: 2 }))
// 宽
// 字
// 符console.log(smartwrap('break at word', { width: 10 }))
// break at
// wordconsole.log(smartwrap('break at word', { width: 10, breakword: true }))
// break at w
// ord| Option | Type | Default | Description |
|---|---|---|---|
width |
number | 10 |
Target line width in terminal columns |
breakword |
boolean | false |
Break words that exceed remaining space |
minWidth |
1 | 2 | 2 |
Minimum usable width (use 1 only if no wide chars) |
paddingLeft |
number | 0 |
Spaces prepended to each line |
paddingRight |
number | 0 |
Spaces appended to each line |
splitAt |
string[] | [" ","\t"] |
Characters that split words |
trim |
boolean | true |
Trim leading/trailing whitespace from input |
errorChar |
string | "�" |
Replacement when a single wide char cannot fit |
echo "somestring you want to wrap" | smartwrap --width=3 --paddingLeft=1
smartwrap --helpnpm install
npm run build # tsc → dist/
npm testSource is pure TypeScript under src/. Published artifacts live in dist/.
- Node.js ≥ 22 required (matches breakword@2.1.0).
- Display width is now measured by
breakword.width()instead ofwcwidth: a generated Unicode 18.0.0 East Asian Width table plus UAX #51Emoji_Presentation, no dependencies of its own. Output changes where the two disagree — code pointswcwidth1.x scored 1 cell but measure 2 (e.g.⚡U+26A1) now wrap wider.
- Source rewritten in TypeScript;
main/typespoint atdist/. - Removed unused
grapheme-splitterand thearray.prototype.flatpolyfill. - See prior changelog notes on the modernize release for dependency cleanup details.
Release notes: CHANGELOG.md