# wordwrapjs

> Word-wrapping for javascript.

Latest version **5.1.1** (published 2025-10-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install wordwrapjs
pnpm add wordwrapjs
yarn add wordwrapjs
bun add wordwrapjs
```

## Health

**Score 58/100 (C)** — status: stable.

Positive: has types package; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 5.1.1 |
| Published | 2025-10-09 |
| First published | 2015-08-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/wordwrapjs) |
| Module format | ESM |
| Node | >=12.17 |
| Dependencies | 0 |
| Unpacked size | 23 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 161 |
| Author | Lloyd Brookes |
| Maintainers | 75lb |
| Keywords | word, line, wrap, text, columns, wordwrap |

## Links

- npm: https://www.npmjs.com/package/wordwrapjs
- Repository: https://github.com/75lb/wordwrapjs
- Homepage: https://github.com/75lb/wordwrapjs#readme
- Issues: https://github.com/75lb/wordwrapjs/issues
- npm.io page: https://npm.io/package/wordwrapjs

## Alternatives

- [localforage](https://npm.io/package/localforage.md) — 6.2M weekly downloads
- [localforage-observable](https://npm.io/package/localforage-observable.md) — 30.8K weekly downloads
- [@y/y](https://npm.io/package/@y/y.md) — 30.1K weekly downloads
- [@metaobjectsdev/render](https://npm.io/package/@metaobjectsdev/render.md) — 3.5K weekly downloads
- [@ledgerhq/coin-algorand](https://npm.io/package/@ledgerhq/coin-algorand.md) — 1.1K weekly downloads

## Recent versions

- 5.1.1 (latest) — 2025-10-09
- 2.0.0-0 (next) — 2016-11-20
- 5.1.0 — 2021-08-18
- 5.0.2 — 2021-03-29
- 5.0.1 — 2021-03-23
- 5.0.0 — 2021-03-23
- 4.0.1 — 2021-03-13
- 4.0.0 — 2019-06-26
- 3.0.0 — 2017-08-09
- 2.0.0 — 2016-12-04
- 1.2.1 — 2016-08-06
- 1.2.0 — 2015-11-02
- 1.1.3 — 2015-10-30
- 1.1.2 — 2015-10-23
- 1.1.1 — 2015-10-19
- … 5 more at https://npm.io/package/wordwrapjs/versions

## README

[![view on npm](https://badgen.net/npm/v/wordwrapjs)](https://www.npmjs.org/package/wordwrapjs)
[![npm module downloads](https://badgen.net/npm/dt/wordwrapjs)](https://www.npmjs.org/package/wordwrapjs)
[![Gihub repo dependents](https://badgen.net/github/dependents-repo/75lb/wordwrapjs)](https://github.com/75lb/wordwrapjs/network/dependents?dependent_type=REPOSITORY)
[![Gihub package dependents](https://badgen.net/github/dependents-pkg/75lb/wordwrapjs)](https://github.com/75lb/wordwrapjs/network/dependents?dependent_type=PACKAGE)
[![Node.js CI](https://github.com/75lb/wordwrapjs/actions/workflows/node.js.yml/badge.svg)](https://github.com/75lb/wordwrapjs/actions/workflows/node.js.yml)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](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
<script nomodule src="./node_modules/wordwrapjs/dist/index.js"></script>
```

## 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) ⇒ <code>boolean</code>
            * [.getChunks(text)](#module_wordwrapjs--Wordwrap.getChunks) ⇒ <code>Array.&lt;string&gt;</code>
        * _inner_
            * [~WordwrapOptions](#module_wordwrapjs--Wordwrap..WordwrapOptions) : <code>Object</code>

<a name="exp_module_wordwrapjs--Wordwrap"></a>

### Wordwrap ⏏
**Kind**: Exported class  
<a name="new_module_wordwrapjs--Wordwrap_new"></a>

#### new Wordwrap(text, [options])

| Param | Type | Description |
| --- | --- | --- |
| text | <code>string</code> | The input text to wrap. |
| [options] | [<code>WordwrapOptions</code>](#module_wordwrapjs--Wordwrap..WordwrapOptions) |  |

<a name="module_wordwrapjs--Wordwrap.wrap"></a>

#### Wordwrap.wrap(text, [options])
**Kind**: static method of [<code>Wordwrap</code>](#exp_module_wordwrapjs--Wordwrap)  

| Param | Type | Description |
| --- | --- | --- |
| text | <code>string</code> | the input text to wrap |
| [options] | [<code>WordwrapOptions</code>](#module_wordwrapjs--Wordwrap..WordwrapOptions) |  |

<a name="module_wordwrapjs--Wordwrap.lines"></a>

#### Wordwrap.lines(text, [options])
Wraps the input text, returning an array of strings (lines).

**Kind**: static method of [<code>Wordwrap</code>](#exp_module_wordwrapjs--Wordwrap)  

| Param | Type | Description |
| --- | --- | --- |
| text | <code>string</code> | input text |
| [options] | [<code>WordwrapOptions</code>](#module_wordwrapjs--Wordwrap..WordwrapOptions) |  |

<a name="module_wordwrapjs--Wordwrap.isWrappable"></a>

#### Wordwrap.isWrappable(text) ⇒ <code>boolean</code>
Returns true if the input text would be wrapped if passed into `.wrap()`.

**Kind**: static method of [<code>Wordwrap</code>](#exp_module_wordwrapjs--Wordwrap)  

| Param | Type | Description |
| --- | --- | --- |
| text | <code>string</code> | input text |

<a name="module_wordwrapjs--Wordwrap.getChunks"></a>

#### Wordwrap.getChunks(text) ⇒ <code>Array.&lt;string&gt;</code>
Splits the input text into an array of words and whitespace.

**Kind**: static method of [<code>Wordwrap</code>](#exp_module_wordwrapjs--Wordwrap)  

| Param | Type | Description |
| --- | --- | --- |
| text | <code>string</code> | input text |

<a name="module_wordwrapjs--Wordwrap..WordwrapOptions"></a>

#### Wordwrap~WordwrapOptions : <code>Object</code>
Wordwrap options.

**Kind**: inner typedef of [<code>Wordwrap</code>](#exp_module_wordwrapjs--Wordwrap)  
**Properties**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| [width] | <code>number</code> | <code>30</code> | The max column width in characters. |
| [break] | <code>boolean</code> | <code>false</code> | If true, words exceeding the specified `width` will be forcefully broken |
| [noTrim] | <code>boolean</code> | <code>false</code> | 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] | <code>string</code> | <code>&quot;&#x27;\\n&#x27;&quot;</code> | The end of line character to use. Defaults to `\n`. |


* * *

&copy; 2015-25 Lloyd Brookes \<opensource@75lb.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).

---
_Source: https://npm.io/package/wordwrapjs · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
