UNPKG

6.06 kBJavaScriptView Raw
1import { __extends } from "tslib";
2import { RRule } from './rrule';
3import dateutil from './dateutil';
4import { includes } from './helpers';
5import { iterSet } from './iterset';
6import { rrulestr } from './rrulestr';
7import { optionsToString } from './optionstostring';
8function createGetterSetter(fieldName) {
9 var _this = this;
10 return function (field) {
11 if (field !== undefined) {
12 _this["_".concat(fieldName)] = field;
13 }
14 if (_this["_".concat(fieldName)] !== undefined) {
15 return _this["_".concat(fieldName)];
16 }
17 for (var i = 0; i < _this._rrule.length; i++) {
18 var field_1 = _this._rrule[i].origOptions[fieldName];
19 if (field_1) {
20 return field_1;
21 }
22 }
23 };
24}
25var RRuleSet = /** @class */ (function (_super) {
26 __extends(RRuleSet, _super);
27 /**
28 *
29 * @param {Boolean?} noCache
30 * The same stratagy as RRule on cache, default to false
31 * @constructor
32 */
33 function RRuleSet(noCache) {
34 if (noCache === void 0) { noCache = false; }
35 var _this = _super.call(this, {}, noCache) || this;
36 _this.dtstart = createGetterSetter.apply(_this, ['dtstart']);
37 _this.tzid = createGetterSetter.apply(_this, ['tzid']);
38 _this._rrule = [];
39 _this._rdate = [];
40 _this._exrule = [];
41 _this._exdate = [];
42 return _this;
43 }
44 RRuleSet.prototype._iter = function (iterResult) {
45 return iterSet(iterResult, this._rrule, this._exrule, this._rdate, this._exdate, this.tzid());
46 };
47 /**
48 * Adds an RRule to the set
49 *
50 * @param {RRule}
51 */
52 RRuleSet.prototype.rrule = function (rrule) {
53 _addRule(rrule, this._rrule);
54 };
55 /**
56 * Adds an EXRULE to the set
57 *
58 * @param {RRule}
59 */
60 RRuleSet.prototype.exrule = function (rrule) {
61 _addRule(rrule, this._exrule);
62 };
63 /**
64 * Adds an RDate to the set
65 *
66 * @param {Date}
67 */
68 RRuleSet.prototype.rdate = function (date) {
69 _addDate(date, this._rdate);
70 };
71 /**
72 * Adds an EXDATE to the set
73 *
74 * @param {Date}
75 */
76 RRuleSet.prototype.exdate = function (date) {
77 _addDate(date, this._exdate);
78 };
79 /**
80 * Get list of included rrules in this recurrence set.
81 *
82 * @return List of rrules
83 */
84 RRuleSet.prototype.rrules = function () {
85 return this._rrule.map(function (e) { return rrulestr(e.toString()); });
86 };
87 /**
88 * Get list of excluded rrules in this recurrence set.
89 *
90 * @return List of exrules
91 */
92 RRuleSet.prototype.exrules = function () {
93 return this._exrule.map(function (e) { return rrulestr(e.toString()); });
94 };
95 /**
96 * Get list of included datetimes in this recurrence set.
97 *
98 * @return List of rdates
99 */
100 RRuleSet.prototype.rdates = function () {
101 return this._rdate.map(function (e) { return new Date(e.getTime()); });
102 };
103 /**
104 * Get list of included datetimes in this recurrence set.
105 *
106 * @return List of exdates
107 */
108 RRuleSet.prototype.exdates = function () {
109 return this._exdate.map(function (e) { return new Date(e.getTime()); });
110 };
111 RRuleSet.prototype.valueOf = function () {
112 var result = [];
113 if (!this._rrule.length && this._dtstart) {
114 result = result.concat(optionsToString({ dtstart: this._dtstart }));
115 }
116 this._rrule.forEach(function (rrule) {
117 result = result.concat(rrule.toString().split('\n'));
118 });
119 this._exrule.forEach(function (exrule) {
120 result = result.concat(exrule
121 .toString()
122 .split('\n')
123 .map(function (line) { return line.replace(/^RRULE:/, 'EXRULE:'); })
124 .filter(function (line) { return !/^DTSTART/.test(line); }));
125 });
126 if (this._rdate.length) {
127 result.push(rdatesToString('RDATE', this._rdate, this.tzid()));
128 }
129 if (this._exdate.length) {
130 result.push(rdatesToString('EXDATE', this._exdate, this.tzid()));
131 }
132 return result;
133 };
134 /**
135 * to generate recurrence field such as:
136 * DTSTART:19970902T010000Z
137 * RRULE:FREQ=YEARLY;COUNT=2;BYDAY=TU
138 * RRULE:FREQ=YEARLY;COUNT=1;BYDAY=TH
139 */
140 RRuleSet.prototype.toString = function () {
141 return this.valueOf().join('\n');
142 };
143 /**
144 * Create a new RRuleSet Object completely base on current instance
145 */
146 RRuleSet.prototype.clone = function () {
147 var rrs = new RRuleSet(!!this._cache);
148 this._rrule.forEach(function (rule) { return rrs.rrule(rule.clone()); });
149 this._exrule.forEach(function (rule) { return rrs.exrule(rule.clone()); });
150 this._rdate.forEach(function (date) { return rrs.rdate(new Date(date.getTime())); });
151 this._exdate.forEach(function (date) { return rrs.exdate(new Date(date.getTime())); });
152 return rrs;
153 };
154 return RRuleSet;
155}(RRule));
156export { RRuleSet };
157function _addRule(rrule, collection) {
158 if (!(rrule instanceof RRule)) {
159 throw new TypeError(String(rrule) + ' is not RRule instance');
160 }
161 if (!includes(collection.map(String), String(rrule))) {
162 collection.push(rrule);
163 }
164}
165function _addDate(date, collection) {
166 if (!(date instanceof Date)) {
167 throw new TypeError(String(date) + ' is not Date instance');
168 }
169 if (!includes(collection.map(Number), Number(date))) {
170 collection.push(date);
171 dateutil.sort(collection);
172 }
173}
174function rdatesToString(param, rdates, tzid) {
175 var isUTC = !tzid || tzid.toUpperCase() === 'UTC';
176 var header = isUTC ? "".concat(param, ":") : "".concat(param, ";TZID=").concat(tzid, ":");
177 var dateString = rdates
178 .map(function (rdate) { return dateutil.timeToUntilString(rdate.valueOf(), isUTC); })
179 .join(',');
180 return "".concat(header).concat(dateString);
181}
182//# sourceMappingURL=rruleset.js.map
\No newline at end of file