Word wrapping for plain text.
Wrap some text in a 20 character column.
import wordwrap from 'wordwrapjs'
const text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'
const result = wordwrap.wrap(text, { width: 20 })
result
now looks like this:
Lorem ipsum dolor
sit amet,
consectetur
adipiscing elit, sed
do eiusmod tempor
incididunt ut labore
et dolore magna
aliqua.
Force long words to wrap by setting the break
flag.
Welcome to Node.js v16.6.2.
> wrap = require('wordwrapjs')
> url = 'https://github.com/75lb/wordwrapjs'
> wrap.lines(url, { width: 18 })
[ 'https://github.com/75lb/wordwrapjs' ]
> wrap.lines(url, { width: 18, break: true })
[ 'https://github.com', '/75lb/wordwrapjs' ]
This library is compatible with Node.js, the Web and any style of module loader. It can be loaded anywhere, natively without transpilation.
Node.js:
const wordwrap = require('wordwrapjs')
Within Node.js with ECMAScript Module support enabled:
import wordwrap from 'wordwrapjs'
Within an modern browser ECMAScript Module:
import wordwrap from './node_modules/wordwrapjs/dist/index.mjs'
Old browser (adds window.wordwrapjs
):
<script nomodule src="./node_modules/wordwrapjs/dist/index.js"></script>
{{#module name="wordwrapjs"}} {{>body~}} {{>member-index~}} {{>members~}} {{/module}}
© 2015-21 Lloyd Brookes <[email protected]>. Documented by jsdoc-to-markdown.