Socket
Socket
Sign inDemoInstall

wordwrapjs

Package Overview
Dependencies
0
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    wordwrapjs

Word-wrapping for javascript.


Version published
Maintainers
1
Install size
27.0 kB
Created

Package description

What is wordwrapjs?

The wordwrapjs package is a utility for wrapping words into a specified width. It's useful for formatting text output in command-line applications or anywhere where you need to control the flow of text to fit within certain boundaries.

What are wordwrapjs's main functionalities?

Hard Wrapping

Hard wrapping ensures that the text is wrapped strictly at the specified width, even if it means breaking words apart.

const wordwrap = require('wordwrapjs');
let wrappedText = wordwrap.wrap('Some long text that needs to be wrapped', {width: 20});
console.log(wrappedText);

Soft Wrapping

Soft wrapping wraps text at the specified width but without breaking words. It moves the whole word to the next line if it doesn't fit.

const wordwrap = require('wordwrapjs');
let wrappedText = wordwrap.wrap('Some long text that needs to be wrapped', {width: 20, break: false});
console.log(wrappedText);

Custom Break Character

This feature allows specifying a custom break character for wrapping, which can be useful for adding hyphens or other characters when a word is split.

const wordwrap = require('wordwrapjs');
let wrappedText = wordwrap.wrap('Some long text that needs to be wrapped', {width: 20, break: true, cut: true});
console.log(wrappedText);

Other packages similar to wordwrapjs

Readme

Source

view on npm npm module downloads Gihub repo dependents Gihub package dependents Node.js CI js-standard-style

wordwrapjs

Word wrapping for plain text.

Synopsis

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' ]

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:

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>

API Reference

Wordwrap ⏏

Kind: Exported class

new Wordwrap(text, [options])
ParamTypeDescription
textstringThe input text to wrap.
[options]WordwrapOptions

Wordwrap.wrap(text, [options])

Kind: static method of Wordwrap

ParamTypeDescription
textstringthe input text to wrap
[options]WordwrapOptions

Wordwrap.lines(text, [options])

Wraps the input text, returning an array of strings (lines).

Kind: static method of Wordwrap

ParamTypeDescription
textstringinput text
[options]WordwrapOptions

Wordwrap.isWrappable(text) ⇒ boolean

Returns true if the input text would be wrapped if passed into .wrap().

Kind: static method of Wordwrap

ParamTypeDescription
textstringinput text

Wordwrap.getChunks(text) ⇒ Array.<string>

Splits the input text into an array of words and whitespace.

Kind: static method of Wordwrap

ParamTypeDescription
textstringinput text

Wordwrap~WordwrapOptions : Object

Wordwrap options.

Kind: inner typedef of Wordwrap
Properties

NameTypeDefaultDescription
[width]number30The max column width in characters.
[break]booleanfalseIf true, words exceeding the specified width will be forcefully broken
[noTrim]booleanfalseBy 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.

Keywords

FAQs

Last updated on 18 Aug 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc