
Security News
How Enterprise Security Is Adapting to AI-Accelerated Threats
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.
@universal-packages/text-wrap
Advanced tools
npm install @universal-packages/text-wrap
wrap(text: string, [options: Object])Wraps a string to a given width.
import { wrapText } from '@universal-packages/text-wrap'
const wrappedText = wrapText('This is some super long text', { width: 10 })
console.log(wrappedText)
// > This is
// > some super
// > long text
align 'left' | 'right' | 'center' | 'justify' default: 'left'
Aligns the text within the width.
const wrappedTextCenter = wrapText('This is some super long text', { align: 'center', width: 9 })
console.log(wrappedTextCenter)
// > This is
// > some
// > super
// > long text
const wrappedTextRight = wrapText('This is some super long text', { align: 'right', width: 10 })
console.log(wrappedTextRight)
// > This is
// > some super
// > long text
const wrappedTextJustify = wrapText('This is some super long text', { align: 'justify', width: 10 })
console.log(wrappedTextJustify)
// > This is
// > some super
// > long text
fillBlock boolean default: false
Fills the block with spaces to the width.
const wrappedTextFillBlock = wrapText('This is some super long text', { fillBlock: true, width: 10 })
console.log(wrappedTextFillBlock)
// > This is°°°
// > some super
// > long text°
height number
Truncates the text so that it fits the height. If not provided, the text will be wrapped only to the width. if the hight is greater to the number of lines, the text will be filled with spaces if fillBlock is true.
const wrappedTextFillBlock = wrapText('This is some super long text', { width: 10, height: 2 })
console.log(wrappedTextFillBlock)
// > This is
// > some su...
const wrappedTextFillBlock = wrapText('This is some super long text', { fillBlock: true, width: 10, height: 5 })
console.log(wrappedTextFillBlock)
// > This is°°°
// > some super
// > long text°
// > °°°°°°°°°°
// > °°°°°°°°°°
hyphenate 'always' | 'word' | 'never' default: 'never'
Breaks words to fit the width, and adds a hyphen to the end of the line. Use always to always hyphenate to fit the width on each line that needs it. Use word to only hyphenate words that are too long to fit the width. Use never or do not pass the option to disable hyphenation. The hyphenation follows just a few simple rules, like we don't break words smaller than 4 characters, and we only hyphenate text aligned to the left.
const wrappedTextHyphenate = wrapText('This is some super long text', { hyphenate: 'always', width: 10 })
console.log(wrappedTextHyphenate)
// > This is s-
// > ome super
// > long text
const wrappedTextHyphenateWord = wrapText('This is some large text and a very ultramegalarge word', { hyphenate: 'word', width: 10 })
console.log(wrappedTextHyphenateWord)
// > This is
// > some large
// > text and a
// > very ultr-
// > amegalarg-
// > e word
margin number | [number. number, number. number] default: 0
Adds margin to the wrapped lines. You can pass a single number to add the same padding to all sides, or an array with 4 numbers to add padding to each side in the following order: top, right, bottom, left.
The margin adds to the final width of the block, for example, if you have a width of 10 and a margin of 2, the text will be wrapped to 10 characters but each line will have a length of 14 characters.
const wrappedTextPadding = wrapText('This is some super long text', { padding: 2, width: 10 })
console.log(wrappedTextOnFinishLine)
// > °°°°°°°°°°°°°°
// > °°°°°°°°°°°°°°
// > °°This is°°°°°
// > °°some super°°
// > °°long text°°°
// > °°°°°°°°°°°°°°
// > °°°°°°°°°°°°°°
padding number | [number. number, number. number] default: 0
Adds padding to the wrapped lines. You can pass a single number to add the same padding to all sides, or an array with 4 numbers to add padding to each side in the following order: top, right, bottom, left.
The wrapped text will be wrapped considering the horizontal padding, for example, if you have a width of 10 and a padding of 2, the text will be wrapped to 6 characters.
const wrappedTextPadding = wrapText('This is some super long text', { padding: 2, width: 10 })
console.log(wrappedTextOnFinishLine)
// > °°°°°°°°°°°°°°
// > °°°°°°°°°°°°°°
// > °°This is°°°°°
// > °°some super°°
// > °°long text°°°
// > °°°°°°°°°°°°°°
// > °°°°°°°°°°°°°°
verticalAlign 'top' | 'middle' | 'bottom' default: 'top'
When the height is provided, aligns the text vertically within the height only if fillBlock is true.
const wrappedMiddleText = wrapText('This is some super long text', { fillBlock: true, width: 10, height: 7, verticalAlign: 'middle' })
console.log(wrappedMiddleText)
// > °°°°°°°°°°
// > °°°°°°°°°°
// > This is°°°
// > some super
// > long text°
// > °°°°°°°°°°
// > °°°°°°°°°°
width number
The width of the block to wrap the text. If not provided align, fillBlock, and padding will be applied as if width was provided as the largest line in the input text.
const wrappedTextWidth = wrapText('This is some super long text', { padding: 1 })
console.log(wrappedTextWidth)
// > °°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
// > °This is some super long text°
// > °°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
This library is developed in TypeScript and shipped fully typed.
The development of this library happens in the open on GitHub, and we are grateful to the community for contributing bugfixes and improvements. Read below to learn how you can take part in improving this library.
FAQs
Text wrap utility
We found that @universal-packages/text-wrap demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
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.

Security News
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.