UNPKG

7.79 kBJavaScriptView Raw
1import dateutil from './dateutil';
2import IterResult from './iterresult';
3import CallbackIterResult from './callbackiterresult';
4import { fromText, parseText, toText, isFullyConvertible } from './nlp/index';
5import { Frequency } from './types';
6import { parseOptions, initializeOptions } from './parseoptions';
7import { parseString } from './parsestring';
8import { optionsToString } from './optionstostring';
9import { Cache } from './cache';
10import { Weekday } from './weekday';
11import { iter } from './iter/index';
12// =============================================================================
13// RRule
14// =============================================================================
15export var Days = {
16 MO: new Weekday(0),
17 TU: new Weekday(1),
18 WE: new Weekday(2),
19 TH: new Weekday(3),
20 FR: new Weekday(4),
21 SA: new Weekday(5),
22 SU: new Weekday(6)
23};
24export var DEFAULT_OPTIONS = {
25 freq: Frequency.YEARLY,
26 dtstart: null,
27 interval: 1,
28 wkst: Days.MO,
29 count: null,
30 until: null,
31 tzid: null,
32 bysetpos: null,
33 bymonth: null,
34 bymonthday: null,
35 bynmonthday: null,
36 byyearday: null,
37 byweekno: null,
38 byweekday: null,
39 bynweekday: null,
40 byhour: null,
41 byminute: null,
42 bysecond: null,
43 byeaster: null
44};
45export var defaultKeys = Object.keys(DEFAULT_OPTIONS);
46/**
47 *
48 * @param {Options?} options - see <http://labix.org/python-dateutil/#head-cf004ee9a75592797e076752b2a889c10f445418>
49 * The only required option is `freq`, one of RRule.YEARLY, RRule.MONTHLY, ...
50 * @constructor
51 */
52var RRule = /** @class */ (function () {
53 function RRule(options, noCache) {
54 if (options === void 0) { options = {}; }
55 if (noCache === void 0) { noCache = false; }
56 // RFC string
57 this._cache = noCache ? null : new Cache();
58 // used by toString()
59 this.origOptions = initializeOptions(options);
60 var parsedOptions = parseOptions(options).parsedOptions;
61 this.options = parsedOptions;
62 }
63 RRule.parseText = function (text, language) {
64 return parseText(text, language);
65 };
66 RRule.fromText = function (text, language) {
67 return fromText(text, language);
68 };
69 RRule.fromString = function (str) {
70 return new RRule(RRule.parseString(str) || undefined);
71 };
72 RRule.prototype._iter = function (iterResult) {
73 return iter(iterResult, this.options);
74 };
75 RRule.prototype._cacheGet = function (what, args) {
76 if (!this._cache)
77 return false;
78 return this._cache._cacheGet(what, args);
79 };
80 RRule.prototype._cacheAdd = function (what, value, args) {
81 if (!this._cache)
82 return;
83 return this._cache._cacheAdd(what, value, args);
84 };
85 /**
86 * @param {Function} iterator - optional function that will be called
87 * on each date that is added. It can return false
88 * to stop the iteration.
89 * @return Array containing all recurrences.
90 */
91 RRule.prototype.all = function (iterator) {
92 if (iterator) {
93 return this._iter(new CallbackIterResult('all', {}, iterator));
94 }
95 var result = this._cacheGet('all');
96 if (result === false) {
97 result = this._iter(new IterResult('all', {}));
98 this._cacheAdd('all', result);
99 }
100 return result;
101 };
102 /**
103 * Returns all the occurrences of the rrule between after and before.
104 * The inc keyword defines what happens if after and/or before are
105 * themselves occurrences. With inc == True, they will be included in the
106 * list, if they are found in the recurrence set.
107 * @return Array
108 */
109 RRule.prototype.between = function (after, before, inc, iterator) {
110 if (inc === void 0) { inc = false; }
111 if (!dateutil.isValidDate(after) || !dateutil.isValidDate(before))
112 throw new Error('Invalid date passed in to RRule.between');
113 var args = {
114 before: before,
115 after: after,
116 inc: inc
117 };
118 if (iterator) {
119 return this._iter(new CallbackIterResult('between', args, iterator));
120 }
121 var result = this._cacheGet('between', args);
122 if (result === false) {
123 result = this._iter(new IterResult('between', args));
124 this._cacheAdd('between', result, args);
125 }
126 return result;
127 };
128 /**
129 * Returns the last recurrence before the given datetime instance.
130 * The inc keyword defines what happens if dt is an occurrence.
131 * With inc == True, if dt itself is an occurrence, it will be returned.
132 * @return Date or null
133 */
134 RRule.prototype.before = function (dt, inc) {
135 if (inc === void 0) { inc = false; }
136 if (!dateutil.isValidDate(dt))
137 throw new Error('Invalid date passed in to RRule.before');
138 var args = { dt: dt, inc: inc };
139 var result = this._cacheGet('before', args);
140 if (result === false) {
141 result = this._iter(new IterResult('before', args));
142 this._cacheAdd('before', result, args);
143 }
144 return result;
145 };
146 /**
147 * Returns the first recurrence after the given datetime instance.
148 * The inc keyword defines what happens if dt is an occurrence.
149 * With inc == True, if dt itself is an occurrence, it will be returned.
150 * @return Date or null
151 */
152 RRule.prototype.after = function (dt, inc) {
153 if (inc === void 0) { inc = false; }
154 if (!dateutil.isValidDate(dt))
155 throw new Error('Invalid date passed in to RRule.after');
156 var args = { dt: dt, inc: inc };
157 var result = this._cacheGet('after', args);
158 if (result === false) {
159 result = this._iter(new IterResult('after', args));
160 this._cacheAdd('after', result, args);
161 }
162 return result;
163 };
164 /**
165 * Returns the number of recurrences in this set. It will have go trough
166 * the whole recurrence, if this hasn't been done before.
167 */
168 RRule.prototype.count = function () {
169 return this.all().length;
170 };
171 /**
172 * Converts the rrule into its string representation
173 * @see <http://www.ietf.org/rfc/rfc2445.txt>
174 * @return String
175 */
176 RRule.prototype.toString = function () {
177 return optionsToString(this.origOptions);
178 };
179 /**
180 * Will convert all rules described in nlp:ToText
181 * to text.
182 */
183 RRule.prototype.toText = function (gettext, language, dateFormatter) {
184 return toText(this, gettext, language, dateFormatter);
185 };
186 RRule.prototype.isFullyConvertibleToText = function () {
187 return isFullyConvertible(this);
188 };
189 /**
190 * @return a RRule instance with the same freq and options
191 * as this one (cache is not cloned)
192 */
193 RRule.prototype.clone = function () {
194 return new RRule(this.origOptions);
195 };
196 // RRule class 'constants'
197 RRule.FREQUENCIES = [
198 'YEARLY',
199 'MONTHLY',
200 'WEEKLY',
201 'DAILY',
202 'HOURLY',
203 'MINUTELY',
204 'SECONDLY'
205 ];
206 RRule.YEARLY = Frequency.YEARLY;
207 RRule.MONTHLY = Frequency.MONTHLY;
208 RRule.WEEKLY = Frequency.WEEKLY;
209 RRule.DAILY = Frequency.DAILY;
210 RRule.HOURLY = Frequency.HOURLY;
211 RRule.MINUTELY = Frequency.MINUTELY;
212 RRule.SECONDLY = Frequency.SECONDLY;
213 RRule.MO = Days.MO;
214 RRule.TU = Days.TU;
215 RRule.WE = Days.WE;
216 RRule.TH = Days.TH;
217 RRule.FR = Days.FR;
218 RRule.SA = Days.SA;
219 RRule.SU = Days.SU;
220 RRule.parseString = parseString;
221 RRule.optionsToString = optionsToString;
222 return RRule;
223}());
224export default RRule;
225//# sourceMappingURL=rrule.js.map
\No newline at end of file