Skip to content

srcagency/split-text-to-chunks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

1 Commit
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Split text into chunks

Prefers splitting at whitespace characters, but falls back to "hard wrapping", and obeys existing newlines.

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
$ 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

About

Split a text string to chunks for e.g. word wrapping

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published