Skip to content

Build your personal knowledge base with Trilium Notes

chore(test): add template literal for trimming indentation

Elian Doranba95caaf6d3f
diff --git a/spec/support/utils.spec.ts b/spec/support/utils.spec.ts
new file mode 100644
index 0000000000..3c3b2ce80e
--- /dev/null
+++ b/spec/support/utils.spec.ts
@@ -0,0 +1,14 @@
+import { trimIndentation } from "./utils.js";
+
+describe("Utils", () => {
+ it("trims indentation", () => {
+ expect(trimIndentation`\
+ Hello
+ world
+ 123`
+ ).toBe(`\
+Hello
+ world
+123`);
+ });
+});
\ No newline at end of file
diff --git a/spec/support/utils.ts b/spec/support/utils.ts
new file mode 100644
index 0000000000..849cfe8407
--- /dev/null
+++ b/spec/support/utils.ts
@@ -0,0 +1,21 @@
+export function trimIndentation(strings: TemplateStringsArray) {
+ const str = strings.toString();
+
+ // Count the number of spaces on the first line.
+ let numSpaces = 0;
+ while (str.charAt(numSpaces) == ' ' && numSpaces < str.length) {
+ numSpaces++;
+ }
+
+ // Trim the indentation of the first line in all the lines.
+ const lines = str.split("\n");
+ const output = [];
+ for (let i=0; i<lines.length; i++) {
+ let numSpacesLine = 0;
+ while (str.charAt(numSpacesLine) == ' ' && numSpacesLine < str.length) {
+ numSpacesLine++;
+ }
+ output.push(lines[i].substring(numSpacesLine));
+ }
+ return output.join("\n");
+}
\ No newline at end of file