rrule-rust
Version:
RRule implementation for browsers and Node.js written in Rust
103 lines (102 loc) • 2.99 kB
JavaScript
import { DateTime, } from './datetime';
import { ExDate as Rust } from './lib';
/**
* Represents exception dates (EXDATE property) for a recurrence rule.
*
* ExDate specifies date/time values that should be excluded from the
* recurrence set. This is useful for marking exceptions like holidays
* or cancelled events in a recurring series.
*
* @example
* ```typescript
* // Create with single date
* const exdate1 = new ExDate(DateTime.date(2024, 1, 15));
*
* // Create with multiple dates
* const exdate2 = new ExDate([
* DateTime.date(2024, 1, 15),
* DateTime.date(2024, 1, 22)
* ]);
*
* // Create with timezone
* const exdate3 = new ExDate({
* values: [DateTime.local(2024, 1, 15, 9, 0, 0)],
* tzid: "America/New_York"
* });
* ```
*/
export class ExDate {
constructor(valueOrValuesOrOptions, tzid) {
if (Array.isArray(valueOrValuesOrOptions) ||
valueOrValuesOrOptions instanceof 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.fromFlatInt32Array(rust.values), rust.tzid ?? undefined);
rrule.rust = rust;
return rrule;
}
static fromPlain(plain) {
return new this({
values: plain.values.map((dt) => DateTime.fromPlain(dt)),
tzid: plain.tzid,
});
}
/**
* Creates a new ExDate instance with a different timezone.
*
* @param tzid - Timezone identifier (e.g., "America/New_York") or undefined
* @returns A new ExDate instance with the specified timezone
*
* @example
* ```typescript
* const exdate = new ExDate([DateTime.date(2024, 1, 15)]);
* const withTz = exdate.setTzid("America/New_York");
* ```
*/
setTzid(tzid) {
return new ExDate(this.values, tzid);
}
/**
* Creates a new ExDate instance with different date/time values.
*
* @param datetimes - Array of new date/time values
* @returns A new ExDate instance with the specified values
*
* @example
* ```typescript
* const exdate = new ExDate([DateTime.date(2024, 1, 15)]);
* const updated = exdate.setValues([
* DateTime.date(2024, 1, 15),
* DateTime.date(2024, 1, 22)
* ]);
* ```
*/
setValues(datetimes) {
return new ExDate(datetimes, this.tzid);
}
toPlain() {
return {
values: this.values.map((dt) => dt.toPlain()),
tzid: this.tzid,
};
}
/**
* @internal
*/
toRust() {
this.rust ??= new Rust(DateTime.toFlatInt32Array(this.values), this.tzid);
return this.rust;
}
}