UNPKG

@rareelements/lydia

Version:
54 lines 2.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Calendar = void 0; const Helper_1 = require("./utils/Helper"); const ComponentType_1 = require("./model/ComponentType"); const PropertyType_1 = require("./model/PropertyType"); class Calendar { constructor(rawCalendar) { this.rawCalendar = rawCalendar; if (rawCalendar && rawCalendar.properties) { const tzProp = rawCalendar.properties.filter((p) => p.name === PropertyType_1.PropertyType.X_WR_TIMEZONE); if (tzProp && tzProp.length === 1) this.defaultTZ = tzProp[0].value; } } *iterator() { const components = this.rawCalendar.components; if (!components) return; for (const c of components) { yield Helper_1.cookComponent(c, this.defaultTZ); } } getEvents({ start, end, limit }) { const events = []; for (const c of this.iterator()) { if (c.getType() !== ComponentType_1.ComponentType.VEVENT) { continue; } const eventStart = c.getStart(); const eventEnd = c.getEnd(); const rrule = c.getRRule(); if (rrule) { const occurences = rrule.rruleSet.between(start, end || new Date(-8640000000000000)); // console.log("occurences", occurences); if (occurences) occurences.forEach((o) => events.push([o, c])); } else if (eventStart.value.getTime() >= start.getTime() && (!end || eventEnd.value.getTime() <= end.getTime())) { events.push([eventStart.value, c]); } } events.sort((e1, e2) => e1[0].getTime() - e2[0].getTime()); if (limit && events.length >= limit) { return events.slice(0, limit); } else { return events; } } } exports.Calendar = Calendar; //# sourceMappingURL=Calendar.js.map