mirror of
https://github.com/zadam/trilium.git
synced 2025-10-20 23:29:02 +02:00
feat(server/script): enable a few dayjs plugins (closes #6080)
This commit is contained in:
parent
f32f9d4326
commit
fe3350f39f
@ -36,6 +36,17 @@ import type Becca from "../becca/becca-interface.js";
|
|||||||
import type { NoteParams } from "./note-interface.js";
|
import type { NoteParams } from "./note-interface.js";
|
||||||
import type { ApiParams } from "./backend_script_api_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
|
* A whole number
|
||||||
* @typedef {number} int
|
* @typedef {number} int
|
||||||
|
@ -60,17 +60,55 @@ describe("Script", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("dayjs", () => {
|
describe("dayjs", () => {
|
||||||
|
const scriptNote = note("dayjs", {
|
||||||
|
type: "code",
|
||||||
|
mime: "application/javascript;env=backend",
|
||||||
|
});
|
||||||
|
|
||||||
it("dayjs is available", () => {
|
it("dayjs is available", () => {
|
||||||
cls.init(() => {
|
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");`);
|
const bundle = getScriptBundle(scriptNote.note, true, "backend", [], `return api.dayjs().format("YYYY-MM-DD");`);
|
||||||
expect(bundle).toBeDefined();
|
expect(bundle).toBeDefined();
|
||||||
const result = executeBundle(bundle!);
|
const result = executeBundle(bundle!);
|
||||||
expect(result).toMatch(/^\d{4}-\d{2}-\d{2}$/);
|
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");
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user