From ba95caaf6d3fb7a22a21ec1af14e7cfe2e7397cf Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Tue, 17 Dec 2024 23:08:17 +0200 Subject: [PATCH] chore(test): add template literal for trimming indentation --- spec/support/utils.spec.ts | 14 ++++++++++++++ spec/support/utils.ts | 21 +++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 spec/support/utils.spec.ts create mode 100644 spec/support/utils.ts diff --git a/spec/support/utils.spec.ts b/spec/support/utils.spec.ts new file mode 100644 index 000000000..3c3b2ce80 --- /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 000000000..849cfe840 --- /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