forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrimIdent.ts
More file actions
27 lines (23 loc) · 894 Bytes
/
Copy pathtrimIdent.ts
File metadata and controls
27 lines (23 loc) · 894 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
export function trimIdent(text: string): string {
// Split the text into an array of lines
const lines = text.split('\n');
// Remove leading and trailing empty lines
while (lines.length > 0 && lines[0].trim() === '') {
lines.shift();
}
while (lines.length > 0 && lines[lines.length - 1].trim() === '') {
lines.pop();
}
// Find the minimum number of leading spaces in non-empty lines
const minSpaces = lines.reduce((min, line) => {
if (line.trim() === '') {
return min;
}
const leadingSpaces = line.match(/^\s*/)![0].length;
return Math.min(min, leadingSpaces);
}, Infinity);
// Remove the common leading spaces from each line
const trimmedLines = lines.map(line => line.slice(minSpaces));
// Join the trimmed lines back into a single string
return trimmedLines.join('\n');
}