UNPKG

rrule-rust

Version:

RRule implementation for Node.js written in Rust

174 lines (173 loc) 6.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RRule = exports.Weekday = exports.Month = exports.Frequency = void 0; const datetime_1 = require("./datetime"); const lib_1 = require("./lib"); var Frequency; (function (Frequency) { Frequency[Frequency["Yearly"] = 0] = "Yearly"; Frequency[Frequency["Monthly"] = 1] = "Monthly"; Frequency[Frequency["Weekly"] = 2] = "Weekly"; Frequency[Frequency["Daily"] = 3] = "Daily"; Frequency[Frequency["Hourly"] = 4] = "Hourly"; Frequency[Frequency["Minutely"] = 5] = "Minutely"; Frequency[Frequency["Secondly"] = 6] = "Secondly"; })(Frequency || (exports.Frequency = Frequency = {})); var Month; (function (Month) { Month[Month["January"] = 1] = "January"; Month[Month["February"] = 2] = "February"; Month[Month["March"] = 3] = "March"; Month[Month["April"] = 4] = "April"; Month[Month["May"] = 5] = "May"; Month[Month["June"] = 6] = "June"; Month[Month["July"] = 7] = "July"; Month[Month["August"] = 8] = "August"; Month[Month["September"] = 9] = "September"; Month[Month["October"] = 10] = "October"; Month[Month["November"] = 11] = "November"; Month[Month["December"] = 12] = "December"; })(Month || (exports.Month = Month = {})); var Weekday; (function (Weekday) { Weekday[Weekday["Monday"] = 0] = "Monday"; Weekday[Weekday["Tuesday"] = 1] = "Tuesday"; Weekday[Weekday["Wednesday"] = 2] = "Wednesday"; Weekday[Weekday["Thursday"] = 3] = "Thursday"; Weekday[Weekday["Friday"] = 4] = "Friday"; Weekday[Weekday["Saturday"] = 5] = "Saturday"; Weekday[Weekday["Sunday"] = 6] = "Sunday"; })(Weekday || (exports.Weekday = Weekday = {})); class RRule { constructor(rruleOrFrequency = {}) { if (typeof rruleOrFrequency === 'object' && rruleOrFrequency !== null) { this.frequency = rruleOrFrequency.frequency ?? Frequency.Daily; this.interval = rruleOrFrequency.interval; this.until = rruleOrFrequency.until; this.count = rruleOrFrequency.count; this.byWeekday = rruleOrFrequency.byWeekday ?? []; this.byHour = rruleOrFrequency.byHour ?? []; this.byMinute = rruleOrFrequency.byMinute ?? []; this.bySecond = rruleOrFrequency.bySecond ?? []; this.byMonthday = rruleOrFrequency.byMonthday ?? []; this.bySetpos = rruleOrFrequency.bySetpos ?? []; this.byMonth = rruleOrFrequency.byMonth ?? []; this.byWeekno = rruleOrFrequency.byWeekno ?? []; this.byYearday = rruleOrFrequency.byYearday ?? []; this.weekstart = rruleOrFrequency.weekstart; } else { this.frequency = rruleOrFrequency; this.byWeekday = []; this.byHour = []; this.byMinute = []; this.bySecond = []; this.byMonthday = []; this.bySetpos = []; this.byMonth = []; this.byWeekno = []; this.byYearday = []; } } /** * Parses a string into an RRule. */ static parse(str) { const rust = lib_1.RRule.parse(str); return this.fromRust(rust); } /** * @internal */ static fromRust(rust) { const rrule = new this({ frequency: rust.frequency, interval: rust.interval ?? undefined, until: rust.until === null ? undefined : datetime_1.DateTime.fromNumeric(rust.until), count: rust.count ?? undefined, byWeekday: rust.byWeekday, byHour: rust.byHour, byMinute: rust.byMinute, bySecond: rust.bySecond, byMonthday: rust.byMonthday, bySetpos: rust.bySetpos, byMonth: rust.byMonth, byWeekno: rust.byWeekno, byYearday: rust.byYearday, weekstart: rust.weekstart ?? undefined, }); rrule.rust = rust; return rrule; } setFrequency(frequency) { return new RRule({ ...this.toObject(), frequency }); } setInterval(interval) { return new RRule({ ...this.toObject(), interval }); } setCount(count) { return new RRule({ ...this.toObject(), count }); } setByWeekday(weekdays) { return new RRule({ ...this.toObject(), byWeekday: weekdays }); } setByHour(hours) { return new RRule({ ...this.toObject(), byHour: hours }); } setByMinute(minutes) { return new RRule({ ...this.toObject(), byMinute: minutes }); } setBySecond(seconds) { return new RRule({ ...this.toObject(), bySecond: seconds }); } setByMonthday(days) { return new RRule({ ...this.toObject(), byMonthday: days }); } setBySetpos(poses) { return new RRule({ ...this.toObject(), bySetpos: poses }); } setByMonth(months) { return new RRule({ ...this.toObject(), byMonth: months }); } setByWeekno(weekNumbers) { return new RRule({ ...this.toObject(), byWeekno: weekNumbers }); } setByYearday(days) { return new RRule({ ...this.toObject(), byYearday: days }); } setWeekstart(day) { return new RRule({ ...this.toObject(), weekstart: day }); } setUntil(datetime) { return new RRule({ ...this.toObject(), until: datetime }); } toString() { return this.toRust().toString(); } /** * @internal */ toRust() { this.rust ??= new lib_1.RRule(this.frequency, this.interval, this.count, this.weekstart, this.until?.toNumeric(), this.byWeekday, this.byHour, this.byMinute, this.bySecond, this.byMonthday, this.bySetpos, this.byMonth, this.byWeekno, this.byYearday); return this.rust; } toObject() { return { frequency: this.frequency, interval: this.interval, count: this.count, byWeekday: this.byWeekday, byHour: this.byHour, byMinute: this.byMinute, bySecond: this.bySecond, byMonthday: this.byMonthday, bySetpos: this.bySetpos, byMonth: this.byMonth, byWeekno: this.byWeekno, byYearday: this.byYearday, weekstart: this.weekstart, until: this.until, }; } } exports.RRule = RRule;