-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
44 lines (40 loc) · 1.61 KB
/
index.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
36
37
38
39
40
41
42
43
44
export const aldent = (
strings: string | TemplateStringsArray,
...values: string[]
): string => {
// Combine the strings and values into a single string with each value's
// lines indented according to the indentation before the value
let completeString = "";
if (typeof strings === "string") {
completeString = strings;
} else {
for (let i = 0; i < values.length; i++) {
const indentSpaces = strings[i]?.match(/ +$/)?.[0].length || 0;
completeString +=
strings[i] +
(values[i] || "").split("\n").join("\n" + " ".repeat(indentSpaces));
}
completeString += strings[strings.length - 1];
}
// Trim everything up to and including the last newline character before the first non-whitespace character
const firstNonWhitespaceIndex = completeString.search(/\S/);
const lastNewlineIndex = completeString.lastIndexOf(
"\n",
firstNonWhitespaceIndex
);
const trimmedString = completeString.slice(lastNewlineIndex + 1);
const splitString = trimmedString.split("\n");
// Find the minimum number of spaces at the beginning of each line
let minLeadingSpaceCount = Infinity;
for (const line of splitString.filter((l) => l.trim() != "")) {
let leadingSpaceCount = line.match(/^ */)?.[0]?.length ?? 0;
minLeadingSpaceCount = Math.min(minLeadingSpaceCount, leadingSpaceCount);
if (minLeadingSpaceCount === 0) break;
}
// Remove minLeadingSpaceCount spaces from the beginning of each line and remove trailing newlines
return splitString
.map((line) => line.slice(minLeadingSpaceCount))
.join("\n")
.replace(/\n+$/, "");
};
export default aldent;