Split a text string to chunks for e.g. word wrapping
npm install split-text-to-chunksPrefers splitting at whitespace characters, but falls back to "hard wrapping",
and obeys existing newlines.
``js
const {split,width} = require('split-text-to-chunks')
const str = 'A lazy š¶ made a pile of š©'
split(str, 8/columns/)
// -> [ 'A lazy š¶', 'made a ', 'pile of ', 'š©' ]
split('A lazy dog', 3)
// -> [ 'A ', 'laz', 'y ', 'dog' ]
split('A lazy\ndog', 10)
// -> [ 'A lazy', 'dog' ]
width(str)
// -> 25
width(str, 20/ max, stop counting /)
// -> 20
width('one\ntwo\nthree')
// -> 5
`
`sh
$ npm i -g split-text-to-chunks
$ printf "A lazy š¶ made a pile of š©" | wordwrap --columns 8 # default: 80
A lazy š¶
made a
pile of
š©
$ printf "A lazy š¶\nmade a pile of š©" | wordwrap --width
16
``