File size: 574 Bytes
0ad74ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
export function clean_indent(code: string): string {
	const lines = code.split("\n");
	let min_indent: any = null;
	lines.forEach((line) => {
		const current_indent = line.match(/^(\s*)\S/);
		if (current_indent) {
			const indent_length = current_indent[1].length;
			min_indent =
				min_indent !== null
					? Math.min(min_indent, indent_length)
					: indent_length;
		}
	});
	if (min_indent === null || min_indent === 0) {
		return code.trim();
	}
	const normalized_lines = lines.map((line) => line.substring(min_indent));
	return normalized_lines.join("\n").trim();
}