Skip to content

flex-development/string-wrap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

string-wrap

ci github release npm npm downloads install size codecov module type: esm license conventional commits typescript vitest yarn

Wrap a string

Contents

What is this?

This is a small, but useful package for word-wrapping a string to a specified column width.

Features

🌈 ansi support
🦄 emoji and fullwidth character support
➡️ indent and pad lines

Install

This package is ESM only.

In Node.js with yarn:

yarn add @flex-development/string-wrap
See 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.

Use

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 }))

preview

API

This package exports the following identifiers:

The default export is wrap.

lines(thing, config[, options])

Get info about the lines of a wrapped string.

Overloads

  • lines(thing: unknown, config: number | string, options?: Options | null | undefined): LinesInfo
  • lines(thing: unknown, config: Config | number | string): LinesInfo
Parameters
  • thing (unknown) — the thing to wrap. non-string values will be converted to strings
  • config (Config | number | string) — the wrap configuration or the number of columns to wrap the string to
  • options (Options | null | undefined, optional) — options for wrapping
Returns

(LinesInfo) Info about the lines forming the wrapped string

wrap(thing, config[, options])

Wrap a string to the specified column width.

Overloads

  • wrap(thing: unknown, config: number | string, options?: Options | null | undefined): string
  • wrap(thing: unknown, config: Config | number | string): string
Parameters
  • thing (unknown) — the thing to wrap. non-string values will be converted to strings
  • config (Config | number | string) — the wrap configuration or the number of columns to wrap the string to
  • options (Options | null | undefined, optional) — options for wrapping
Returns

(string) The wrapped string

Types

This package is fully typed with TypeScript.

Config

String wrapping configuration (interface).

Extends

Properties

  • columns (number | string) — the number of columns to wrap the string to

LinePadding

The strings used to pad either side of each line (type).

type LinePadding = [left: string, right: string]

LinesInfo

Info about the lines of a wrapped string (interface).

Properties

  • eol (string) — the character, or characters, used to mark the end of a line
  • indent (string) — the string used to indent each line
  • lines (readonly string[]) — the list of lines forming the wrapped string
  • padding (LinePadding) — the strings used to pad either side of each line

Options

Options for wrapping a string (interface).

Properties

  • eol? (string | null | undefined, optional) — the character, or characters, used to mark the end of a line
    • default: '\n'
  • 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 of columns

    👉 note: setting this to true will break words.

  • hard? (boolean | null | undefined, optional) — whether to hard wrap words at the specified number of columns.
    by default, long words remain unbroken and push onto the next line if they don't fit on the current line.
    setting this to true will break long words.

    👉 note: setting this to true will break words.

  • indent? (number | string | null | undefined, optional) — the size of the string to use for indenting each line (as a number or numeric), or the string itself
  • padLeft? (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 itself
  • padRight? (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 itself
  • stringify? (ToString | null | undefined, optional) — convert a value to a string
  • stripAnsi? (StripAnsi | boolean | null | undefined, optional) — whether to remove ANSI escape codes before wrapping, or a function to remove ANSI escape codes
  • tabSize? (number | null | undefined, optional) — the number of spaces a tab is equivalent to
    • default: 2
  • 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

StripAnsi

Remove ANSI escape codes from a string (type).

type StripAnsi = (this: void, string: string) => string

Parameters

  • string (string) — the string containing ANSI escape codes

Returns

(string) The string with ANSI escape codes removed

ToString<[T]>

Convert value to a string (type).

type ToString<T = any> = (this: void, value: T) => string

Type Parameters

  • T (any, optional) — the thing to stringify

Parameters

  • value (T) — the thing to stringify

Returns

(string) The stringified value

Contribute

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.

Sponsor this project

 

Packages

 
 
 

Contributors 3

  •  
  •  
  •