or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-indent-string

Indent each line in a string with customizable indentation characters and count.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/indent-string@5.0.x

To install, run

npx @tessl/cli install tessl/npm-indent-string@5.0.0

index.mddocs/

Indent String

Indent String is a simple and efficient utility for indenting each line in a string with customizable indentation characters and count. It provides flexible options including custom indent characters, control over empty line indentation, and precise count control.

Package Information

  • Package Name: indent-string
  • Package Type: npm
  • Language: JavaScript (ES modules with TypeScript definitions)
  • Installation: npm install indent-string

Core Imports

import indentString from "indent-string";

For CommonJS (requires Node.js dynamic import):

const { default: indentString } = await import("indent-string");

TypeScript with types:

import indentString, { Options } from "indent-string";

Basic Usage

import indentString from "indent-string";

// Basic indentation (default: 1 space)
indentString('Unicorns\nRainbows');
//=> ' Unicorns\n Rainbows'

// Custom count
indentString('Unicorns\nRainbows', 4);
//=> '    Unicorns\n    Rainbows'

// Custom indent character
indentString('Unicorns\nRainbows', 4, {indent: '♥'});
//=> '♥♥♥♥Unicorns\n♥♥♥♥Rainbows'

// Include empty lines
indentString('foo\n\nbar', 2, {includeEmptyLines: true});
//=> '  foo\n  \n  bar'

Capabilities

String Indentation

Indents each line in a string with customizable indentation.

/**
 * Indent each line in a string
 * @param string - The string to indent
 * @param count - How many times to repeat the indent string (default: 1)
 * @param options - Configuration options
 * @returns The indented string
 * @throws {TypeError} If string is not a string (message: "Expected `input` to be a `string`, got `type`")
 * @throws {TypeError} If count is not a number
 * @throws {RangeError} If count is negative
 * @throws {TypeError} If options.indent is not a string
 */
export default function indentString(string: string, count?: number, options?: Options): string;

Parameters:

  • string (string, required): The string to indent
  • count (number, optional, default: 1): How many times to repeat the indent string
  • options (object, optional): Configuration options

Options:

  • indent (string, optional, default: ' '): The string to use for indentation
  • includeEmptyLines (boolean, optional, default: false): Whether to indent empty lines

Return Value: Returns the string with each line indented according to the specified parameters.

Error Handling:

  • Throws TypeError if string parameter is not a string (message: "Expected input to be a string, got type")
  • Throws TypeError if count parameter is not a number (message: "Expected count to be a number, got type")
  • Throws RangeError if count parameter is negative (message: "Expected count to be at least 0, got value")
  • Throws TypeError if options.indent is not a string (message: "Expected options.indent to be a string, got type")

Usage Examples:

// Zero count returns original string
indentString('foo\nbar', 0);
//=> 'foo\nbar'

// Works with various line endings
indentString('foo\r\nbar', 2);
//=> '  foo\r\n  bar'

// Preserves existing indentation
indentString(' foo\n bar', 1);
//=> '  foo\n  bar'

// Empty lines not indented by default
indentString('foo\n\nbar', 2);
//=> '  foo\n\n  bar'

// Including empty lines
indentString('foo\n\nbar', 2, {includeEmptyLines: true});
//=> '  foo\n  \n  bar'

// Custom indent characters
indentString('line1\nline2', 3, {indent: '->'});
//=> '->->->line1\n->->->line2'

Types

interface Options {
  /** The string to use for the indent (default: ' ') */
  readonly indent?: string;
  /** Also indent empty lines (default: false) */
  readonly includeEmptyLines?: boolean;
}