0
# Strip Indent
1
2
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.
3
4
## Package Information
5
6
- **Package Name**: strip-indent
7
- **Package Type**: npm
8
- **Language**: JavaScript (with TypeScript definitions)
9
- **Installation**: `npm install strip-indent`
10
11
## Core Imports
12
13
```javascript
14
import stripIndent from "strip-indent";
15
```
16
17
For CommonJS (dynamic import required since this is a pure ESM module):
18
19
```javascript
20
const stripIndent = (await import('strip-indent')).default;
21
```
22
23
## Basic Usage
24
25
```javascript
26
import stripIndent from "strip-indent";
27
28
const string = '\tunicorn\n\t\tcake';
29
/*
30
unicorn
31
cake
32
*/
33
34
stripIndent(string);
35
/*
36
unicorn
37
cake
38
*/
39
```
40
41
## Capabilities
42
43
### Strip Indentation
44
45
Removes leading whitespace from each line in a string, normalizing indentation by determining the minimum indentation level across all non-empty lines.
46
47
```javascript { .api }
48
/**
49
* Strip leading whitespace from each line in a string.
50
* The line with the least number of leading whitespace, ignoring empty lines,
51
* determines the number to remove.
52
*
53
* @param {string} string - The input string to process
54
* @returns {string} The string with leading whitespace stripped
55
*/
56
function stripIndent(string: string): string;
57
```
58
59
**Usage Examples:**
60
61
```javascript
62
import stripIndent from "strip-indent";
63
64
// Basic example with tabs
65
const tabIndented = '\tunicorn\n\t\tcake';
66
stripIndent(tabIndented);
67
// Result: 'unicorn\n\tcake'
68
69
// Example with spaces
70
const spaceIndented = ' hello\n world\n there';
71
stripIndent(spaceIndented);
72
// Result: 'hello\n world\nthere'
73
74
// HTML template example
75
const html = stripIndent(`
76
<!doctype html>
77
<html>
78
<body>
79
<h1>Hello world!</h1>
80
</body>
81
</html>
82
`);
83
// Result: '<!doctype html>\n<html>\n\t<body>\n\t\t<h1>Hello world!</h1>\n\t</body>\n</html>'
84
85
// Empty lines are ignored when determining minimum indent
86
const mixedContent = '\n\t\n\t\tunicorn\n\n\n\n\t\t\tunicorn';
87
stripIndent(mixedContent);
88
// Result: '\n\t\nunicorn\n\n\n\n\tunicorn'
89
```
90
91
**Behavior Details:**
92
93
- Empty lines (containing only whitespace or completely empty) are ignored when determining the minimum indentation level
94
- The function handles both tabs and spaces as indentation
95
- If no indentation is found (minimum indent is 0), the original string is returned unchanged
96
- Preserves the relative indentation structure while removing the common leading whitespace
97
- Uses a regular expression to efficiently strip the determined amount of whitespace from each line
98
99
**Error Handling:**
100
101
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.