-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrrule.js
157 lines (127 loc) · 3.86 KB
/
rrule.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
'use strict';
const moment = require('moment');
// These are basically lodash methods, maybe import them at some point.
const isNil = v => v === undefined || v === null;
const times = (n, fn) => new Array(n).fill().map((_, i) => fn(i));
const freqToUnit = frequency => {
if (typeof frequency !== 'string') {
return;
}
const freq = frequency.trim().toLowerCase();
if (freq === 'daily') {
return 'day';
}
const unit = freq.replace(/(ly)?$/, '');
return moment.normalizeUnits(unit);
};
const defaults = {
dtstart: null,
freq: null,
interval: 1,
count: null,
until: null
};
class RRule {
constructor(options) {
const opt = Object.assign({}, defaults, options);
this.dtstart = opt.dtstart ? moment(opt.dtstart) : moment.invalid();
if (this.dtstart.isValid() === false) {
throw new Error('Invalid dtstart', options.dtstart);
}
this.unit = freqToUnit(opt.freq);
if (!this.unit) {
throw new Error(`Invalid frequency, ${options.freq}`);
}
// Any invalid value will default to 1
this.interval = Number.parseInt(opt.interval, 10) || 1;
if (!this.interval || this.interval < 1) {
throw new Error(`Invalid interval, ${options.interval} must be greater or equal 1`);
}
if (isNil(opt.count) === false) {
this.count = Number.parseInt(opt.count, 10);
if (!this.count || this.count < 1) {
throw new Error(`Invalid count, ${options.count} must be greater 0`);
}
} else if (isNil(opt.until) === false) {
this.until = moment(opt.until);
if (this.until.isValid() === false) {
throw new Error(`Invalid until, ${options.until} must be a valid date`);
}
if (this.until.isBefore(this.dtstart)) {
throw new Error(`Invalid until, ${options.until} must be after dtstart`);
}
}
}
last() {
if (this.count) {
const amount = (this.count - 1) * this.interval;
return this.dtstart.clone().add(amount, this.unit);
}
if (this.until) {
const diff = Math.floor(this.until.diff(this.dtstart, this.unit, true) / this.interval);
if (diff < 0) {
return moment.invalid();
}
const amount = diff * this.interval;
return this.dtstart.clone().add(amount, this.unit);
}
return moment.invalid();
}
at(num) {
const index = Number.parseInt(num, 10);
if (Number.isNaN(index) || index < 0) {
return moment.invalid();
}
const last = this.last();
const amount = index * this.interval;
const date = this.dtstart.clone().add(amount, this.unit);
if (date.isAfter(last)) {
return moment.invalid();
}
return date;
}
after(date) {
const m = moment(date);
const diff = Math.ceil(m.diff(this.dtstart, this.unit, true) / this.interval);
if (diff < 1) {
return this.dtstart.clone();
}
const last = this.last();
const amount = diff * this.interval;
const after = this.dtstart.clone().add(amount, this.unit);
if (after.isAfter(last)) {
return moment.invalid();
}
return after;
}
before(date) {
const m = moment(date);
const diff = Math.floor(m.diff(this.dtstart, this.unit, true) / this.interval);
if (diff < 0) {
return moment.invalid();
}
const last = this.last();
const amount = diff * this.interval;
const before = this.dtstart.clone().add(amount, this.unit);
if (before.isAfter(last)) {
return last;
}
return before;
}
between(after, before) {
const from = this.after(after);
if (from.isValid() === false) {
return [];
}
const to = this.before(before);
if (to.isValid() === false) {
return [];
}
const diff = Math.floor(to.diff(from, this.unit, true) / this.interval);
if (diff < 0) {
return [];
}
return times(diff + 1, i => from.clone().add(i * this.interval, this.unit));
}
}
module.exports = RRule;