Textwrap for JavaScript/Node.js.
Correctly handles wide characters (宽字符) and emojis (😃).
Optionally break words when wrapping strings. Preserves ANSI escape codes.
JavaScript's String.length counts code units, not visual terminal width.
Wide characters (CJK) and many emoji occupy 2 columns but have length 1.
This package wraps text to a target visual width using wcwidth.
npm install smartwrapCLI (global):
npm install -g smartwrapconst smartwrap = require('smartwrap')
console.log(smartwrap('宽字符', { width: 2 }))
// 宽
// 字
// 符console.log(smartwrap('break at word', {
width: 10,
breakword: false // default
}))
// 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 so
me
st
ri
ng
yo
u
wa
nt
to
wr
ap
Run smartwrap --help for all flags.
- Node.js ≥ 12 required (native
Array.prototype.flat). - Removed unused
grapheme-splitterand thearray.prototype.flatpolyfill (eliminates ~50 transitive dependencies). - Updated
yargsand other dependencies. - CLI now correctly passes
breakwordanderrorChar. - Test runner switched from Grunt to plain Mocha.
- Minor code cleanups (fixed undeclared variable, modernized style).
Existing wrapping behavior for supported inputs is intentionally preserved.
- Node.js ≥ 12
- CommonJS