-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple-recur.js
175 lines (154 loc) · 4.42 KB
/
simple-recur.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Generated by CoffeeScript 1.9.0
(function() {
var DateString, Recur, moment;
moment = this.moment || require('moment');
DateString = function(date) {
return moment(date).format('YYYY-MM-DD');
};
Recur = (function() {
Recur.attributes = {
units: Number,
measure: String,
start: DateString,
end: DateString,
from: DateString
};
function Recur(data) {
if (data == null) {
data = {};
}
this.set(data);
this.setDefaults();
}
Recur.prototype.set = function(data) {
var attr;
if (data == null) {
data = {};
}
for (attr in this.constructor.attributes) {
if (data[attr] != null) {
this[attr] = data[attr];
}
}
return this;
};
Recur.prototype.setDefaults = function() {
if (this.units == null) {
this.units = 1;
}
if (this.measure == null) {
this.measure = 'month';
}
return this.start != null ? this.start : this.start = moment();
};
Recur.prototype.next = function(begin, end) {
var i, _i, _results;
if (begin == null) {
begin = 1;
}
if (!end) {
end = begin;
begin = 0;
}
_results = [];
for (i = _i = begin; begin <= end ? _i < end : _i > end; i = begin <= end ? ++_i : --_i) {
_results.push(this.nextAt(i));
}
return _results;
};
Recur.prototype.nextAt = function(count) {
var correction, next;
if (count == null) {
count = 1;
}
correction = this.getFromDateCorrection();
next = moment(this.start).startOf('day').add((count + correction) * this.units, this.measure);
if (this.isAfterEndDate(next)) {
return null;
} else {
return next;
}
};
Recur.prototype.getFromDateCorrection = function() {
var from, start;
if (!this.from) {
return 0;
}
if (moment(this.from).isBefore(this.start)) {
return 0;
}
start = moment(this.start).startOf('day');
from = moment(this.from).startOf('day');
return Math.ceil(Math.abs(start.diff(from, this.measure, true)) / this.units);
};
Recur.prototype.matches = function(date) {
var diff, start;
if (this.isBeforeFromDate(date)) {
return false;
}
if (this.isBeforeStartDate(date)) {
return false;
}
if (this.isAfterEndDate(date)) {
return false;
}
date = moment(date).startOf('day');
start = moment(this.start).startOf('day');
diff = start.diff(date, this.measure, true);
if (diff % this.units === 0) {
return true;
}
return Math.floor(diff) % this.units === 0 && this.isRecurringOnLastDayOfMonth(date);
};
Recur.prototype.isBeforeFromDate = function(date) {
return this.from && moment(this.from).startOf('day').isAfter(date);
};
Recur.prototype.isBeforeStartDate = function(date) {
return moment(this.start).startOf('day').isAfter(date);
};
Recur.prototype.isAfterEndDate = function(date) {
var endOfEndDay;
if (!this.end) {
return false;
}
endOfEndDay = moment(this.end).endOf('day');
return moment(date).isAfter(endOfEndDay);
};
Recur.prototype.isRecurringOnLastDayOfMonth = function(date) {
var dateMonthEndDay, recurringMonthDay;
if (this.measure !== 'month') {
return false;
}
recurringMonthDay = moment(this.start).date();
dateMonthEndDay = moment(date).endOf('month').date();
return (recurringMonthDay > dateMonthEndDay) && moment(date).date() === dateMonthEndDay;
};
Recur.prototype.every = function(_at_units, _at_measure) {
this.units = _at_units;
this.measure = _at_measure;
return this;
};
Recur.prototype.clone = function(data) {
return new this.constructor(this).set(data);
};
Recur.prototype.toJSON = function() {
var attr, json, type, _ref;
json = {};
_ref = this.constructor.attributes;
for (attr in _ref) {
type = _ref[attr];
if (this[attr] != null) {
json[attr] = type(this[attr]);
}
}
delete json.from;
return json;
};
return Recur;
})();
if ((typeof module !== "undefined" && module !== null ? module.exports : void 0) != null) {
module.exports = Recur;
} else {
this.Recur = Recur;
}
}).call(this);