Skip to content

Build your personal knowledge base with Trilium Notes

feat(server/script): enable a few dayjs plugins (closes #6080)

Elian Doranfe3350f39ffc
diff --git a/apps/server/src/services/backend_script_api.ts b/apps/server/src/services/backend_script_api.ts
index 2d69ee608d..b3796637cb 100644
--- a/apps/server/src/services/backend_script_api.ts
+++ b/apps/server/src/services/backend_script_api.ts
@@ -36,6 +36,17 @@ import type Becca from "../becca/becca-interface.js";
import type { NoteParams } from "./note-interface.js";
import type { ApiParams } from "./backend_script_api_interface.js";
+// Dayjs plugins
+import isSameOrBefore from "dayjs/plugin/isSameOrBefore";
+import isSameOrAfter from "dayjs/plugin/isSameOrAfter";
+import isBetween from "dayjs/plugin/isBetween";
+import advancedFormat from "dayjs/plugin/advancedFormat.js";
+
+dayjs.extend(isSameOrBefore);
+dayjs.extend(isSameOrAfter);
+dayjs.extend(isBetween);
+dayjs.extend(advancedFormat);
+
/**
* A whole number
* @typedef {number} int
diff --git a/apps/server/src/services/script.spec.ts b/apps/server/src/services/script.spec.ts
index 9059bd4638..f94c93f26e 100644
--- a/apps/server/src/services/script.spec.ts
+++ b/apps/server/src/services/script.spec.ts
@@ -60,17 +60,55 @@ describe("Script", () => {
});
describe("dayjs", () => {
+ const scriptNote = note("dayjs", {
+ type: "code",
+ mime: "application/javascript;env=backend",
+ });
+
it("dayjs is available", () => {
cls.init(() => {
- const scriptNote = note("dayjs", {
- type: "code",
- mime: "application/javascript;env=backend",
- });
const bundle = getScriptBundle(scriptNote.note, true, "backend", [], `return api.dayjs().format("YYYY-MM-DD");`);
expect(bundle).toBeDefined();
const result = executeBundle(bundle!);
expect(result).toMatch(/^\d{4}-\d{2}-\d{2}$/);
});
});
+
+ it("dayjs is-same-or-before", () => {
+ cls.init(() => {
+ const bundle = getScriptBundle(scriptNote.note, true, "backend", [], `return api.dayjs("2023-10-01").isSameOrBefore(api.dayjs("2023-10-02"));`);
+ expect(bundle).toBeDefined();
+ const result = executeBundle(bundle!);
+ expect(result).toBe(true);
+ });
+ });
+
+ it("dayjs is-same-or-after", () => {
+ cls.init(() => {
+ const bundle = getScriptBundle(scriptNote.note, true, "backend", [], `return api.dayjs("2023-10-02").isSameOrAfter(api.dayjs("2023-10-01"));`);
+ expect(bundle).toBeDefined();
+ const result = executeBundle(bundle!);
+ expect(result).toBe(true);
+ });
+ });
+
+ it("dayjs is-between", () => {
+ cls.init(() => {
+ const bundle = getScriptBundle(scriptNote.note, true, "backend", [], `return api.dayjs("2023-10-02").isBetween(api.dayjs("2023-10-01"), api.dayjs("2023-10-03"));`);
+ expect(bundle).toBeDefined();
+ const result = executeBundle(bundle!);
+ expect(result).toBe(true);
+ });
+ });
+
+ // advanced format
+ it("dayjs advanced format", () => {
+ cls.init(() => {
+ const bundle = getScriptBundle(scriptNote.note, true, "backend", [], `return api.dayjs("2023-10-01").format("Q");`);
+ expect(bundle).toBeDefined();
+ const result = executeBundle(bundle!);
+ expect(result).not.toBe("Q");
+ });
+ });
});
});