UNPKG

5.98 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["_" + fieldName] = field;
13 }
14 if (_this["_" + fieldName] !== undefined) {
15 return _this["_" + 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.toString().split('\n')
121 .map(function (line) { return line.replace(/^RRULE:/, 'EXRULE:'); })
122 .filter(function (line) { return !/^DTSTART/.test(line); }));
123 });
124 if (this._rdate.length) {
125 result.push(rdatesToString('RDATE', this._rdate, this.tzid()));
126 }
127 if (this._exdate.length) {
128 result.push(rdatesToString('EXDATE', this._exdate, this.tzid()));
129 }
130 return result;
131 };
132 /**
133 * to generate recurrence field such as:
134 * DTSTART:19970902T010000Z
135 * RRULE:FREQ=YEARLY;COUNT=2;BYDAY=TU
136 * RRULE:FREQ=YEARLY;COUNT=1;BYDAY=TH
137 */
138 RRuleSet.prototype.toString = function () {
139 return this.valueOf().join('\n');
140 };
141 /**
142 * Create a new RRuleSet Object completely base on current instance
143 */
144 RRuleSet.prototype.clone = function () {
145 var rrs = new RRuleSet(!!this._cache);
146 this._rrule.forEach(function (rule) { return rrs.rrule(rule.clone()); });
147 this._exrule.forEach(function (rule) { return rrs.exrule(rule.clone()); });
148 this._rdate.forEach(function (date) { return rrs.rdate(new Date(date.getTime())); });
149 this._exdate.forEach(function (date) { return rrs.exdate(new Date(date.getTime())); });
150 return rrs;
151 };
152 return RRuleSet;
153}(RRule));
154export default RRuleSet;
155function _addRule(rrule, collection) {
156 if (!(rrule instanceof RRule)) {
157 throw new TypeError(String(rrule) + ' is not RRule instance');
158 }
159 if (!includes(collection.map(String), String(rrule))) {
160 collection.push(rrule);
161 }
162}
163function _addDate(date, collection) {
164 if (!(date instanceof Date)) {
165 throw new TypeError(String(date) + ' is not Date instance');
166 }
167 if (!includes(collection.map(Number), Number(date))) {
168 collection.push(date);
169 dateutil.sort(collection);
170 }
171}
172function rdatesToString(param, rdates, tzid) {
173 var isUTC = !tzid || tzid.toUpperCase() === 'UTC';
174 var header = isUTC ? param + ":" : param + ";TZID=" + tzid + ":";
175 var dateString = rdates
176 .map(function (rdate) { return dateutil.timeToUntilString(rdate.valueOf(), isUTC); })
177 .join(',');
178 return "" + header + dateString;
179}
180//# sourceMappingURL=rruleset.js.map
\No newline at end of file