[](https://www.npmjs.org/package/wordwrapjs)
[](https://www.npmjs.org/package/wordwrapjs)
[](https://github.com/75lb/wordwrapjs/network/dependents?dependent_type=REPOSITORY)
[](https://github.com/75lb/wordwrapjs/network/dependents?dependent_type=PACKAGE)
[](https://github.com/75lb/wordwrapjs/actions/workflows/node.js.yml)
[](https://github.com/feross/standard)
# wordwrapjs
Word wrapping for plain text.
## Synopsis
Wrap some text in a 20 character column.
```js
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' ]
```
## Load anywhere
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:
```js
const wordwrap = require('wordwrapjs')
```
Within Node.js with ECMAScript Module support enabled:
```js
import wordwrap from 'wordwrapjs'
```
Within an modern browser ECMAScript Module:
```js
import wordwrap from './node_modules/wordwrapjs/dist/index.mjs'
```
Old browser (adds `window.wordwrapjs`):
```html
```
## API Reference
* [wordwrapjs](#module_wordwrapjs)
* [Wordwrap](#exp_module_wordwrapjs--Wordwrap) ⏏
* [new Wordwrap(text, [options])](#new_module_wordwrapjs--Wordwrap_new)
* _static_
* [.wrap(text, [options])](#module_wordwrapjs--Wordwrap.wrap)
* [.lines(text, [options])](#module_wordwrapjs--Wordwrap.lines)
* [.isWrappable(text)](#module_wordwrapjs--Wordwrap.isWrappable) ⇒ boolean
* [.getChunks(text)](#module_wordwrapjs--Wordwrap.getChunks) ⇒ Array.<string>
* _inner_
* [~WordwrapOptions](#module_wordwrapjs--Wordwrap..WordwrapOptions) : Object
### Wordwrap ⏏
**Kind**: Exported class
#### new Wordwrap(text, [options])
| Param | Type | Description |
| --- | --- | --- |
| text | string | The input text to wrap. |
| [options] | [WordwrapOptions](#module_wordwrapjs--Wordwrap..WordwrapOptions) | |
#### Wordwrap.wrap(text, [options])
**Kind**: static method of [Wordwrap](#exp_module_wordwrapjs--Wordwrap)
| Param | Type | Description |
| --- | --- | --- |
| text | string | the input text to wrap |
| [options] | [WordwrapOptions](#module_wordwrapjs--Wordwrap..WordwrapOptions) | |
#### Wordwrap.lines(text, [options])
Wraps the input text, returning an array of strings (lines).
**Kind**: static method of [Wordwrap](#exp_module_wordwrapjs--Wordwrap)
| Param | Type | Description |
| --- | --- | --- |
| text | string | input text |
| [options] | [WordwrapOptions](#module_wordwrapjs--Wordwrap..WordwrapOptions) | |
#### Wordwrap.isWrappable(text) ⇒ boolean
Returns true if the input text would be wrapped if passed into `.wrap()`.
**Kind**: static method of [Wordwrap](#exp_module_wordwrapjs--Wordwrap)
| Param | Type | Description |
| --- | --- | --- |
| text | string | input text |
#### Wordwrap.getChunks(text) ⇒ Array.<string>
Splits the input text into an array of words and whitespace.
**Kind**: static method of [Wordwrap](#exp_module_wordwrapjs--Wordwrap)
| Param | Type | Description |
| --- | --- | --- |
| text | string | input text |
#### Wordwrap~WordwrapOptions : Object
Wordwrap options.
**Kind**: inner typedef of [Wordwrap](#exp_module_wordwrapjs--Wordwrap)
**Properties**
| Name | Type | Default | Description |
| --- | --- | --- | --- |
| [width] | number | 30 | The max column width in characters. |
| [break] | boolean | false | If true, words exceeding the specified `width` will be forcefully broken |
| [noTrim] | boolean | false | By default, each line output is trimmed. If `noTrim` is set, no line-trimming occurs - all whitespace from the input text is left in. |
| [eol] | string | "'\\n'" | The end of line character to use. Defaults to `\n`. |
* * *
© 2015-21 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).