1 | import { __extends } from "tslib";
|
2 | import RRule from './rrule';
|
3 | import dateutil from './dateutil';
|
4 | import { includes } from './helpers';
|
5 | import { iterSet } from './iterset';
|
6 | import { rrulestr } from './rrulestr';
|
7 | import { optionsToString } from './optionstostring';
|
8 | function 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 | }
|
25 | var RRuleSet = (function (_super) {
|
26 | __extends(RRuleSet, _super);
|
27 | |
28 |
|
29 |
|
30 |
|
31 |
|
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 |
|
49 |
|
50 |
|
51 |
|
52 | RRuleSet.prototype.rrule = function (rrule) {
|
53 | _addRule(rrule, this._rrule);
|
54 | };
|
55 | |
56 |
|
57 |
|
58 |
|
59 |
|
60 | RRuleSet.prototype.exrule = function (rrule) {
|
61 | _addRule(rrule, this._exrule);
|
62 | };
|
63 | |
64 |
|
65 |
|
66 |
|
67 |
|
68 | RRuleSet.prototype.rdate = function (date) {
|
69 | _addDate(date, this._rdate);
|
70 | };
|
71 | |
72 |
|
73 |
|
74 |
|
75 |
|
76 | RRuleSet.prototype.exdate = function (date) {
|
77 | _addDate(date, this._exdate);
|
78 | };
|
79 | |
80 |
|
81 |
|
82 |
|
83 |
|
84 | RRuleSet.prototype.rrules = function () {
|
85 | return this._rrule.map(function (e) { return rrulestr(e.toString()); });
|
86 | };
|
87 | |
88 |
|
89 |
|
90 |
|
91 |
|
92 | RRuleSet.prototype.exrules = function () {
|
93 | return this._exrule.map(function (e) { return rrulestr(e.toString()); });
|
94 | };
|
95 | |
96 |
|
97 |
|
98 |
|
99 |
|
100 | RRuleSet.prototype.rdates = function () {
|
101 | return this._rdate.map(function (e) { return new Date(e.getTime()); });
|
102 | };
|
103 | |
104 |
|
105 |
|
106 |
|
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 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | RRuleSet.prototype.toString = function () {
|
139 | return this.valueOf().join('\n');
|
140 | };
|
141 | |
142 |
|
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));
|
154 | export default RRuleSet;
|
155 | function _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 | }
|
163 | function _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 | }
|
172 | function 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 |
|
\ | No newline at end of file |