-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
35 lines (35 loc) · 1.04 KB
/
mod.ts
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
28
29
30
31
32
33
34
35
export default function trimIndent(
strings: TemplateStringsArray,
...expressions: string[]
): string {
const indent = Math.min(
...strings
.flatMap((it) =>
it
.split("\n")
// don't consider lines that are ws (line.trim() == "")
// unless it's lines[-1] because s string expression will follow it (and last-last line can set indent)
.filter((_, i, lines) => i !== 0 && i === lines.length - 1)
.map((line) => line.length - line.trimStart().length)
),
);
let result = strings
.map((it, i) =>
it
.split("\n")
.map((it) =>
it.substring(0, indent).trim() === "" ? it.substring(indent) : it
)
.join("\n") +
((i < expressions.length) ? expressions[i] : "")
)
.join("");
if (result.startsWith("\n")) result = result.substring(1);
if (
result.includes("\n") &&
result.substring(result.lastIndexOf("\n")).trim() === ""
) {
result = result.substring(0, result.lastIndexOf("\n"));
}
return result;
}