or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-strip-indent

Strip leading whitespace from each line in a string

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

To install, run

npx @tessl/cli install tessl/npm-strip-indent@4.0.0

index.mddocs/

Strip Indent

Strip Indent provides a utility function for normalizing string indentation by removing leading whitespace from each line. It intelligently determines the minimum indentation level across all non-empty lines and strips that amount of whitespace from every line, making it particularly useful for processing template strings, code snippets, or any multi-line text that needs consistent formatting.

Package Information

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

Core Imports

import stripIndent from "strip-indent";

For CommonJS (dynamic import required since this is a pure ESM module):

const stripIndent = (await import('strip-indent')).default;

Basic Usage

import stripIndent from "strip-indent";

const string = '\tunicorn\n\t\tcake';
/*
	unicorn
		cake
*/

stripIndent(string);
/*
unicorn
	cake
*/

Capabilities

Strip Indentation

Removes leading whitespace from each line in a string, normalizing indentation by determining the minimum indentation level across all non-empty lines.

/**
 * Strip leading whitespace from each line in a string.
 * The line with the least number of leading whitespace, ignoring empty lines, 
 * determines the number to remove.
 * 
 * @param {string} string - The input string to process
 * @returns {string} The string with leading whitespace stripped
 */
function stripIndent(string: string): string;

Usage Examples:

import stripIndent from "strip-indent";

// Basic example with tabs
const tabIndented = '\tunicorn\n\t\tcake';
stripIndent(tabIndented);
// Result: 'unicorn\n\tcake'

// Example with spaces
const spaceIndented = '  hello\n    world\n  there';
stripIndent(spaceIndented);
// Result: 'hello\n  world\nthere'

// HTML template example
const html = stripIndent(`
		<!doctype html>
		<html>
			<body>
				<h1>Hello world!</h1>
			</body>
		</html>
`);
// Result: '<!doctype html>\n<html>\n\t<body>\n\t\t<h1>Hello world!</h1>\n\t</body>\n</html>'

// Empty lines are ignored when determining minimum indent
const mixedContent = '\n\t\n\t\tunicorn\n\n\n\n\t\t\tunicorn';
stripIndent(mixedContent);
// Result: '\n\t\nunicorn\n\n\n\n\tunicorn'

Behavior Details:

  • Empty lines (containing only whitespace or completely empty) are ignored when determining the minimum indentation level
  • The function handles both tabs and spaces as indentation
  • If no indentation is found (minimum indent is 0), the original string is returned unchanged
  • Preserves the relative indentation structure while removing the common leading whitespace
  • Uses a regular expression to efficiently strip the determined amount of whitespace from each line

Error Handling:

The function expects a string input. If non-string values are passed, JavaScript's standard type coercion behavior will apply through the underlying min-indent dependency and regex operations.