Strip leading whitespace from each line in a string
npx @tessl/cli install tessl/npm-strip-indent@4.0.0Strip 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.
npm install strip-indentimport stripIndent from "strip-indent";For CommonJS (dynamic import required since this is a pure ESM module):
const stripIndent = (await import('strip-indent')).default;import stripIndent from "strip-indent";
const string = '\tunicorn\n\t\tcake';
/*
unicorn
cake
*/
stripIndent(string);
/*
unicorn
cake
*/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:
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.