rrule-rust
Version:
RRule implementation for browsers and Node.js written in Rust
107 lines (106 loc) • 3.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RDate = void 0;
const datetime_1 = require("./datetime");
const lib_1 = require("./lib");
/**
* Represents recurrence dates (RDATE property) for a recurrence rule.
*
* RDate specifies additional date/time values that should be included in the
* recurrence set. This is useful for adding extra occurrences that don't fit
* the regular recurrence pattern.
*
* @example
* ```typescript
* // Create with single date
* const rdate1 = new RDate(DateTime.date(2024, 1, 15));
*
* // Create with multiple dates
* const rdate2 = new RDate([
* DateTime.date(2024, 1, 15),
* DateTime.date(2024, 1, 22)
* ]);
*
* // Create with timezone
* const rdate3 = new RDate({
* values: [DateTime.local(2024, 1, 15, 9, 0, 0)],
* tzid: "America/New_York"
* });
* ```
*/
class RDate {
constructor(valueOrValuesOrOptions, tzid) {
if (Array.isArray(valueOrValuesOrOptions) ||
valueOrValuesOrOptions instanceof datetime_1.DateTime) {
this.values = Array.isArray(valueOrValuesOrOptions)
? valueOrValuesOrOptions
: [valueOrValuesOrOptions];
this.tzid = tzid;
}
else {
this.values = valueOrValuesOrOptions.values;
this.tzid = valueOrValuesOrOptions.tzid;
}
}
/**
* @internal
*/
static fromRust(rust) {
const rrule = new this(datetime_1.DateTime.fromFlatInt32Array(rust.values), rust.tzid ?? undefined);
rrule.rust = rust;
return rrule;
}
static fromPlain(plain) {
return new this({
values: plain.values.map((dt) => datetime_1.DateTime.fromPlain(dt)),
tzid: plain.tzid,
});
}
/**
* Creates a new RDate instance with a different timezone.
*
* @param tzid - Timezone identifier (e.g., "America/New_York") or undefined
* @returns A new RDate instance with the specified timezone
*
* @example
* ```typescript
* const rdate = new RDate([DateTime.date(2024, 1, 15)]);
* const withTz = rdate.setTzid("America/New_York");
* ```
*/
setTzid(tzid) {
return new RDate(this.values, tzid);
}
/**
* Creates a new RDate instance with different date/time values.
*
* @param datetimes - Array of new date/time values
* @returns A new RDate instance with the specified values
*
* @example
* ```typescript
* const rdate = new RDate([DateTime.date(2024, 1, 15)]);
* const updated = rdate.setValues([
* DateTime.date(2024, 1, 15),
* DateTime.date(2024, 1, 22)
* ]);
* ```
*/
setValues(datetimes) {
return new RDate(datetimes, this.tzid);
}
toPlain() {
return {
values: this.values.map((dt) => dt.toPlain()),
tzid: this.tzid,
};
}
/**
* @internal
*/
toRust() {
this.rust ??= new lib_1.RDate(datetime_1.DateTime.toFlatInt32Array(this.values), this.tzid);
return this.rust;
}
}
exports.RDate = RDate;