forked from GitHubMirrors/silverbullet-icalendar
fix(icalendar): Correctly format nested objects (dates) in RRULE properties
This commit is contained in:
16
icalendar.ts
16
icalendar.ts
@@ -29,6 +29,20 @@ const RRULE_KEY_MAP: Record<string, string> = {
|
|||||||
"freq": "FREQ", // Just in case
|
"freq": "FREQ", // Just in case
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Formats an RRULE value for the string representation.
|
||||||
|
* Specifically handles Date objects and nested date objects from ts-ics.
|
||||||
|
*/
|
||||||
|
function formatRRuleValue(v: any): string {
|
||||||
|
if (v instanceof Date) {
|
||||||
|
return v.toISOString().replace(/[-:]/g, "").split(".")[0] + "Z";
|
||||||
|
}
|
||||||
|
if (typeof v === "object" && v !== null && v.date instanceof Date) {
|
||||||
|
return v.date.toISOString().replace(/[-:]/g, "").split(".")[0] + "Z";
|
||||||
|
}
|
||||||
|
return String(v);
|
||||||
|
}
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
// Utility Functions
|
// Utility Functions
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
@@ -186,7 +200,7 @@ export function expandRecurrences(icsEvent: any, windowDays = 365): any[] {
|
|||||||
cleanRule = Object.entries(rruleStr)
|
cleanRule = Object.entries(rruleStr)
|
||||||
.map(([k, v]) => {
|
.map(([k, v]) => {
|
||||||
const standardKey = RRULE_KEY_MAP[k.toLowerCase()] || k.toUpperCase();
|
const standardKey = RRULE_KEY_MAP[k.toLowerCase()] || k.toUpperCase();
|
||||||
return `${standardKey}=${v}`;
|
return `${standardKey}=${formatRRuleValue(v)}`;
|
||||||
})
|
})
|
||||||
.join(";");
|
.join(";");
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -207,7 +207,7 @@ Deno.test("expandRecurrences - object rrule (Reproduction of missing events)", (
|
|||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
Deno.test("expandRecurrences - object rrule with until (Reproduction)", () => {
|
Deno.test("expandRecurrences - object rrule with until", () => {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const start = new Date(now.getTime() - 10 * 86400000);
|
const start = new Date(now.getTime() - 10 * 86400000);
|
||||||
const startStr = localDateString(start);
|
const startStr = localDateString(start);
|
||||||
@@ -216,25 +216,12 @@ Deno.test("expandRecurrences - object rrule with until (Reproduction)", () => {
|
|||||||
const icsEvent = {
|
const icsEvent = {
|
||||||
summary: "Object RRULE UNTIL Event",
|
summary: "Object RRULE UNTIL Event",
|
||||||
start: startStr,
|
start: startStr,
|
||||||
rrule: { frequency: "DAILY", until: { date: untilDate } } // Common ts-ics structure
|
rrule: { frequency: "DAILY", until: { date: untilDate } }
|
||||||
};
|
};
|
||||||
|
|
||||||
// Spy on console.error
|
const results = expandRecurrences(icsEvent, 30);
|
||||||
let errorLogged = false;
|
// Should now return multiple occurrences
|
||||||
const originalConsoleError = console.error;
|
assert(results.length > 1, `Expected > 1 occurrences, got ${results.length}`);
|
||||||
console.error = (...args) => {
|
assertEquals(results[0].recurrent, true);
|
||||||
if (args[0].includes("Error expanding recurrence for Object RRULE UNTIL Event") &&
|
|
||||||
args[1].message.includes("Invalid UNTIL value")) {
|
|
||||||
errorLogged = true;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
|
||||||
expandRecurrences(icsEvent, 30);
|
|
||||||
} finally {
|
|
||||||
console.error = originalConsoleError;
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(errorLogged, "Should have logged an error for invalid UNTIL object value");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user