Indent each line in a string with customizable indentation characters and count.
npx @tessl/cli install tessl/npm-indent-string@5.0.0Indent 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.
npm install indent-stringimport 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";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'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 indentcount (number, optional, default: 1): How many times to repeat the indent stringoptions (object, optional): Configuration optionsOptions:
indent (string, optional, default: ' '): The string to use for indentationincludeEmptyLines (boolean, optional, default: false): Whether to indent empty linesReturn Value: Returns the string with each line indented according to the specified parameters.
Error Handling:
TypeError if string parameter is not a string (message: "Expected input to be a string, got type")TypeError if count parameter is not a number (message: "Expected count to be a number, got type")RangeError if count parameter is negative (message: "Expected count to be at least 0, got value")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'interface Options {
/** The string to use for the indent (default: ' ') */
readonly indent?: string;
/** Also indent empty lines (default: false) */
readonly includeEmptyLines?: boolean;
}