mirror of
https://github.com/zadam/trilium.git
synced 2025-11-01 02:45:54 +01:00
chore(monorepo/client): integrate test support
This commit is contained in:
@@ -2,4 +2,5 @@ export * from "./i18n.js";
|
||||
export * from "./options_interface.js";
|
||||
export * from "./keyboard_actions_interface.js";
|
||||
export * from "./hidden_subtree.js";
|
||||
export * from "./rows.js";
|
||||
export * from "./rows.js";
|
||||
export * from "./test-utils.js"
|
||||
14
packages/commons/src/test-utils.spec.ts
Normal file
14
packages/commons/src/test-utils.spec.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { trimIndentation } from "./test-utils.js";
|
||||
|
||||
describe("Utils", () => {
|
||||
it("trims indentation", () => {
|
||||
expect(trimIndentation`\
|
||||
Hello
|
||||
world
|
||||
123`).toBe(`\
|
||||
Hello
|
||||
world
|
||||
123`);
|
||||
});
|
||||
});
|
||||
56
packages/commons/src/test-utils.ts
Normal file
56
packages/commons/src/test-utils.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* Reads the level of indentation of the first line and trims the identation for all the text by that amount.
|
||||
*
|
||||
* For example, for:
|
||||
*
|
||||
* ```json
|
||||
* {
|
||||
* "hello": "world"
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* it results in:
|
||||
*
|
||||
* ```json
|
||||
* {
|
||||
* "hello": "world"
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* This is meant to be used as a template string, where it allows the indentation of the template without affecting whitespace changes.
|
||||
*
|
||||
* @example const html = trimIndentation`\
|
||||
* <h1>Heading 1</h1>
|
||||
* <h2>Heading 2</h2>
|
||||
* <h3>Heading 3</h3>
|
||||
* <h4>Heading 4</h4>
|
||||
* <h5>Heading 5</h5>
|
||||
* <h6>Heading 6</h6>
|
||||
* `;
|
||||
* @param strings
|
||||
* @returns
|
||||
*/
|
||||
export function trimIndentation(strings: TemplateStringsArray, ...values: any[]) {
|
||||
// Combine the strings with the values using interpolation
|
||||
let str = strings.reduce((acc, curr, index) => {
|
||||
return acc + curr + (values[index] !== undefined ? values[index] : '');
|
||||
}, '');
|
||||
|
||||
// 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");
|
||||
}
|
||||
Reference in New Issue
Block a user