Wrap lines using Unicode UAX #14 line breaking rules.
npm install @cto.af/linewrap
A command line interface is available: @cto.af/linewrap-cli
import {LineWrap} from '@cto.af/linewrap'
const w = new LineWrap()
w.wrap('Lorem ipsum dolor sit amet...') // A string, wrapped to your console length
for (const line of w.lines('Lorem ipsum dolor sit amet...')) {
// `line` does not have a newline at the end
}
Full API docs are available.
wrap()
for this. Note that there is no
newline at the end of the wrapped text.*lines()
method will
return a string generator, where each line is NOT ended with a newline. The
wrap()
method just calls *lines()
and joins the result
with your configured newline string.Options may be passed into the constructor in an object:
const w = new LineWrap({ width: 40 })
The following options are all optional, having the specified defaults:
ellipsis: string = '…'
(U+2026: HORIZONTAL ELLIPSIS)LineWrap.OVERFLOW_CLIP
.example7: boolean = false
firstCol: number = NaN
hyphen: string = '-'
(U+002D: HYPHEN-MINUS)LineWrap.OVERFLOW_ANYWHERE
.indent: number | string = ''
(empty string)indentChar
s at the
beginning of each line.includeANSI: boolean = false
indentChar: string = ' '
(U+0020: SPACE)indent
is a number, use that many of this string to indent.indentEmpty: boolean = false
indentFirst : boolean = true
firstCol
to control how
short the first line should be.isCJK : boolean
isNewline : RegExp | null = /[^\S\r\n\v\f\x85\u2028\u2029]*[\r\n\v\f\x85\u2028\u2029]+\s*/gu
newlineReplacement
. Ensure you do not create a regular expression
denial of service
(ReDoS)
attack. Make sure the expression has the `g` modifier. If null, no newline
replacement is done, and existing newlines will be maintained in the output, no
matter where they were originally.locale: string =
[system locale as determined by Intl.Segmenter]newline: string = '\n'
wrap()
method.newlineReplacement: string = ' '
isNewline
, replace with this
string.overflow: Symbol = LineWrapOptions.OVERFLOW_VISIBLE
LineWrapOptions.OVERFLOW_VISIBLE
: If a word is longer than
the wrappable area, allow the word to go extend past the width so that it is
not broken. This is the only way for long URLs to still be clickable.LineWrapOptions.OVERFLOW_CLIP
: If a word is longer than the
wrappable area, cut it to size, dropping the rest of the word, inserting an
ellipsis at the end.LineWrapOptions.OVERFLOW_ANYWHERE
: If a word is longer than
the wrappable area, split it into chunks that do fit, inserting a hyphen at
the end of each line.verbose : boolean = false
width: number = 80
escape: (x: string) => string = (x) => x