smartwrap
Textwrap for javascript/nodejs. Correctly handles wide characters (宽字符) and emojis (😃). Wraps strings with option to break on words.
Last updated 2 months ago by tecfu .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install smartwrap 
SYNC missed versions from official npm registry.

smartwrap

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

Installation

npm install smartwrap

CLI (global):

npm install -g smartwrap

Usage

// 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
// word
console.log(smartwrap('break at word', { width: 10, breakword: true }))
// break at w
// ord

Options (SmartwrapOptions)

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

CLI

echo "somestring you want to wrap" | smartwrap --width=3 --paddingLeft=1
smartwrap --help

Development

npm install
npm run build   # tsc → dist/
npm test

Source is pure TypeScript under src/. Published artifacts live in dist/.

Breaking changes in 3.0.0

  • Node.js ≥ 22 required (matches breakword@2.1.0).
  • Display width is now measured by breakword.width() instead of wcwidth: a generated Unicode 18.0.0 East Asian Width table plus UAX #51 Emoji_Presentation, no dependencies of its own. Output changes where the two disagree — code points wcwidth 1.x scored 1 cell but measure 2 (e.g. ⚡ U+26A1) now wrap wider.
  • Source rewritten in TypeScript; main / types point at dist/.
  • Removed unused grapheme-splitter and the array.prototype.flat polyfill.
  • See prior changelog notes on the modernize release for dependency cleanup details.

License

MIT

Current Tags

  • 4.0.0                                ...           latest (2 months ago)

26 Versions

  • 4.0.0                                ...           2 months ago
  • 3.0.0                                ...           2 months ago
  • 2.0.2                                ...           6 years ago
  • 2.0.1                                ...           7 years ago
  • 2.0.0 [deprecated]           ...           7 years ago
  • 1.2.5 [deprecated]           ...           7 years ago
  • 1.2.4 [deprecated]           ...           7 years ago
  • 1.2.3 [deprecated]           ...           7 years ago
  • 1.2.2 [deprecated]           ...           7 years ago
  • 1.2.1 [deprecated]           ...           7 years ago
  • 1.2.0 [deprecated]           ...           7 years ago
  • 1.1.2 [deprecated]           ...           7 years ago
  • 1.1.1 [deprecated]           ...           7 years ago
  • 1.1.0 [deprecated]           ...           7 years ago
  • 1.0.12 [deprecated]           ...           7 years ago
  • 1.0.11 [deprecated]           ...           8 years ago
  • 1.0.10 [deprecated]           ...           8 years ago
  • 1.0.9 [deprecated]           ...           9 years ago
  • 1.0.8 [deprecated]           ...           9 years ago
  • 1.0.7 [deprecated]           ...           9 years ago
  • 1.0.6 [deprecated]           ...           9 years ago
  • 1.0.5 [deprecated]           ...           9 years ago
  • 1.0.3 [deprecated]           ...           10 years ago
  • 1.0.2 [deprecated]           ...           10 years ago
  • 1.0.1 [deprecated]           ...           10 years ago
  • 1.0.0 [deprecated]           ...           10 years ago
Maintainers (1)
Downloads
Total 76
Today 0
This Week 9
This Month 66
Last Day 0
Last Week 21
Last Month 1
Dependencies (3)
Dev Dependencies (7)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE