toMaxLineLength

toMaxLineLength and its companion toMaxLinesLength are word-wrapping utilities. They split text by word boundaries so no line exceeds a given character width — useful when building fixed-width layouts or rendering inside a Card.

Signatures

// Wrap a single string — returns an array of lines
function toMaxLineLength(str: string, width: number): string[]

// Wrap an array of pre-split lines
function toMaxLinesLength(lines: string[], width: number): string[]

Parameters

  • str / lines: The text (or pre-split lines) to wrap.
  • width: Maximum character count per output line. Words that would exceed this limit are moved to a new line.

Return Value

An array of string lines, each no wider than width characters.

Usage

import { toMaxLineLength, toMaxLinesLength } from "ts-better-console";

const paragraph = "The quick brown fox jumps over the lazy dog";
const lines = toMaxLineLength(paragraph, 20);
console.log(lines);
// ["The quick brown", "fox jumps over", "the lazy dog"]

// Wrapping an already-split array
const input = ["Line one is short", "Line two is much much longer and needs wrapping"];
const wrapped = toMaxLinesLength(input, 20);

Want to support this project?

If you find ts-better-console useful and want to support its development, consider starring the GitHub repository or buying me a coffee! Your support helps me dedicate more time to improving the library and adding new features.

Want to contribute to this project?

Contributions are welcome! If you're interested in improving the library, fixing bugs, or adding new features, feel free to check out the GitHub repository and submit a pull request. Whether you're a seasoned developer or new to open source, your contributions can make a difference!