Wrap a string
This is a small, but useful package for word-wrapping a string to a specified column width.
🌈 ansi support
🦄 emoji and fullwidth character support
➡️ indent and pad lines
This package is ESM only.
In Node.js with yarn:
yarn add @flex-development/string-wrapSee Git - Protocols | Yarn for details regarding installing from Git.
In Deno with esm.sh:
import { wrap } from 'https://esm.sh/@flex-development/string-wrap'In browsers with esm.sh:
<script type="module">
import { wrap } from 'https://esm.sh/@flex-development/string-wrap'
</script>With bun:
bun add @flex-development/string-wrap
See bun add for more details.
import c from '@flex-development/colors'
import wrap from '@flex-development/string-wrap'
/**
* The string to wrap.
*
* @type {string}
* @const string
*/
const string = `The ${c.bold(c.italic('quick'))} brown ` +
c.red('🦊 jumped over ') +
'the ' +
c.bold('lazy ') +
c.green('🐶 and then ran away with the 🦄.')
/**
* The number of columns to wrap the string to.
*
* @type {number}
* @const columns
*/
const columns = 8
console.log(wrap(string, columns, { indent: 2 }))This package exports the following identifiers:
The default export is wrap.
Get info about the lines of a wrapped string.
lines(thing: unknown, config: number | string, options?: Options | null | undefined): LinesInfolines(thing: unknown, config: Config | number | string): LinesInfo
thing(unknown) — the thing to wrap. non-string values will be converted to stringsconfig(Config|number|string) — the wrap configuration or the number of columns to wrap the string tooptions(Options|null|undefined,optional) — options for wrapping
(LinesInfo) Info about the lines forming the wrapped string
Wrap a string to the specified column width.
wrap(thing: unknown, config: number | string, options?: Options | null | undefined): stringwrap(thing: unknown, config: Config | number | string): string
thing(unknown) — the thing to wrap. non-string values will be converted to stringsconfig(Config|number|string) — the wrap configuration or the number of columns to wrap the string tooptions(Options|null|undefined,optional) — options for wrapping
(string) The wrapped string
This package is fully typed with TypeScript.
String wrapping configuration (interface).
columns(number|string) — the number of columns to wrap the string to
Info about the lines of a wrapped string (interface).
eol(string) — the character, or characters, used to mark the end of a lineindent(SpacerFunction<string>) — get the string used to indent each linelines(readonly string[]) — the list of lines forming the wrapped stringpadLeft(SpacerFunction<string>) — get the string used to pad the left side of each linepadRight(SpacerFunction<string>) — get the string used to pad the right side of each line
Options for wrapping a string (interface).
eol?(string|null|undefined, optional) — the character, or characters, used to mark the end of a line- default:
'\n'
- default:
fill?(boolean|null|undefined, optional) — whether to completely fill each column, splitting words as necessary.
by default, splits are made at spaces, ensuring that words aren't broken and don't extend past the configured number ofcolumns👉 note: setting this to
truewill break words.hard?(boolean|null|undefined, optional) — whether to hard wrap words at the specified number ofcolumns.
by default, long words remain unbroken and push onto the next line if they don't fit on the current line.
setting this totruewill break long words.👉 note: setting this to
truewill break words.indent?(SpacerFunction|number|string|null|undefined, optional) — the size of the string to use for indenting each line (as a number or numeric), or the string itselfpadLeft?(SpacerFunction|number|string|null|undefined, optional) — the size of the string to use for padding the left side of each line (as a number or numeric), or the string itselfpadRight?(SpacerFunction|number|string|null|undefined, optional) — the size of the string to use for padding the right side of each line (as a number or numeric), or the string itselfstringify?(ToString|null|undefined, optional) — convert a value to a stringstripAnsi?(StripAnsi|boolean|null|undefined, optional) — whether to remove ANSI escape codes before wrapping, or a function to remove ANSI escape codestabSize?(number|null|undefined, optional) — the number of spaces a tab is equivalent to- default:
2
- default:
trim?(boolean|null|undefined, optional) — whether to remove whitespace from the end of each line👉 note: lines are trimmed before applying indents or padding.
- default:
true
- default:
Get a spacer configuration for the line at index (type).
Spacers can be used to indent a line, and/or pad either side of it.
type SpacerFunction<
T extends number | string | null | undefined =
| number
| string
| null
| undefined
> = (
this: void,
index: number,
lines?: readonly string[] | null | undefined
) => TT(number|string|null|undefined, optional) — the spacer configuration
index(number) — the index of the current linelines(readonly string[]|null|undefined, optional) — the current list of lines
(T) The spacer configuration
Union of spacer configurations (type).
Spacers can be used to indent a line, and/or pad either side of it.
Valid spacer configurations are functions, sizes (as a number or numeric), or the spacer itself.
type Spacer = SpacerFunction | number | stringRemove ANSI escape codes from a string (type).
type StripAnsi = (this: void, string: string) => stringstring(string) — the string containing ANSI escape codes
(string) The string with ANSI escape codes removed
Convert value to a string (type).
type ToString<T = any> = (this: void, value: T) => stringT(any, optional) — the thing to stringify
value(T) — the thing to stringify
(string) The stringified value
See CONTRIBUTING.md.
This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.
