forked from rotaready/moment-range
-
Notifications
You must be signed in to change notification settings - Fork 1
/
moment-range.js
359 lines (317 loc) · 8.66 KB
/
moment-range.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
//-----------------------------------------------------------------------------
// Contstants
//-----------------------------------------------------------------------------
const moment = require('moment');
const INTERVALS = {
year: true,
month: true,
week: true,
day: true,
hour: true,
minute: true,
second: true
};
//-----------------------------------------------------------------------------
// Date Ranges
//-----------------------------------------------------------------------------
/**
* DateRange class to store ranges and query dates.
*
* @constructor
* @param {(Moment|Date)} start Start of interval
* @param {(Moment|Date)} end End of interval
*//**
* DateRange class to store ranges and query dates.
*
* @constructor
* @param {!Array} range Array containing start and end dates.
*//**
* DateRange class to store ranges and query dates.
*
* @constructor
* @param {!String} range String formatted as an IS0 8601 time interval
*/
function DateRange(start, end) {
var parts;
var s = start;
var e = end;
if (arguments.length === 1 || end === undefined) {
if (typeof start === 'object' && start.length === 2) {
s = start[0];
e = start[1];
}
else if (typeof start === 'string') {
parts = start.split('/');
s = parts[0];
e = parts[1];
}
}
this.start = (s === null) ? moment(-8640000000000000) : moment(s);
this.end = (e === null) ? moment(8640000000000000) : moment(e);
}
/**
* Constructor for prototype.
*
* @type {DateRange}
*/
DateRange.prototype.constructor = DateRange;
/**
* Deep clone range.
*
* @return {!DateRange}
*/
DateRange.prototype.clone = function() {
return moment().range(this.start, this.end);
};
/**
* Determine if the current interval contains a given moment/date/range.
*
* @param {(Moment|Date|DateRange)} other Date to check
* @param {!boolean} exclusive True if the to value is exclusive
*
* @return {!boolean}
*/
DateRange.prototype.contains = function(other, exclusive) {
var start = this.start;
var end = this.end;
if (other instanceof DateRange) {
return start <= other.start && (end > other.end || (end.isSame(other.end) && !exclusive));
}
else {
return start <= other && (end > other || (end.isSame(other) && !exclusive));
}
};
/**
* Determine if the current date range overlaps a given date range.
*
* @param {!DateRange} range Date range to check
*
* @return {!boolean}
*/
DateRange.prototype.overlaps = function(range) {
return this.intersect(range) !== null;
};
/**
* Determine the intersecting periods from one or more date ranges.
*
* @param {!DateRange} other A date range to intersect with this one
*
* @return {DateRange} Returns the intersecting date or `null` if the ranges do
* not intersect
*/
DateRange.prototype.intersect = function(other) {
var start = this.start;
var end = this.end;
if ((start <= other.start) && (other.start < end) && (end < other.end)) {
return new DateRange(other.start, end);
}
else if ((other.start < start) && (start < other.end) && (other.end <= end)) {
return new DateRange(start, other.end);
}
else if ((other.start < start) && (start <= end) && (end < other.end)) {
return this;
}
else if ((start <= other.start) && (other.start <= other.end) && (other.end <= end)) {
return other;
}
return null;
};
/**
* Merge date ranges if they intersect.
*
* @param {!DateRange} other A date range to add to this one
*
* @return {DateRange} Returns the new `DateRange` or `null` if they do not
* overlap
*/
DateRange.prototype.add = function(other) {
if (this.overlaps(other)) {
return new DateRange(moment.min(this.start, other.start), moment.max(this.end, other.end));
}
return null;
};
/**
* Subtract one range from another.
*
* @param {!DateRange} other A date range to substract from this one
*
* @return {!Array<DateRange>}
*/
DateRange.prototype.subtract = function(other) {
var start = this.start;
var end = this.end;
if (this.intersect(other) === null) {
return [this];
}
else if ((other.start <= start) && (start < end) && (end <= other.end)) {
return [];
}
else if ((other.start <= start) && (start < other.end) && (other.end < end)) {
return [new DateRange(other.end, end)];
}
else if ((start < other.start) && (other.start < end) && (end <= other.end)) {
return [new DateRange(start, other.start)];
}
else if ((start < other.start) && (other.start < other.end) && (other.end < end)) {
return [new DateRange(start, other.start), new DateRange(other.end, end)];
}
else if ((start < other.start) && (other.start < end) && (other.end < end)) {
return [new DateRange(start, other.start), new DateRange(other.start, end)];
}
};
/**
* Iterate over the date range by a given date range, executing a function
* for each sub-range.
*
* @param {(!DateRange|String)} range Date range to be used for iteration or
* shorthand string (shorthands:
* http://momentjs.com/docs/#/manipulating/add/)
* @param {!DateRange~by} hollaback Callback
* @param {!boolean} exclusive Indicate that the end of the range should not
* be included in the iter.
*
* @return {DateRange} `this`
*/
DateRange.prototype.by = function(range, hollaback, exclusive) {
if (typeof range === 'string') {
_byString.call(this, range, hollaback, exclusive);
}
else {
_byRange.call(this, range, hollaback, exclusive);
}
return this;
};
/**
* Callback executed for each sub-range.
*
* @callback DateRange~by
*
* @param {!Moment} current Current moment object for iteration
*/
/**
* @private
*/
function _byString(interval, hollaback, exclusive) {
var current = moment(this.start);
while (this.contains(current, exclusive)) {
hollaback.call(this, current.clone());
current.add(1, interval);
}
}
/**
* @private
*/
function _byRange(interval, hollaback, exclusive) {
var div = this / interval;
var l = Math.floor(div);
if (l === Infinity) { return; }
if (l === div && exclusive) {
l--;
}
for (var i = 0; i <= l; i++) {
hollaback.call(this, moment(this.start.valueOf() + interval.valueOf() * i));
}
}
/**
* Date range formatted as an [ISO8601 Time
* Interval](http://en.wikipedia.org/wiki/ISO_8601#Time_intervals).
*
* @return {!String}
*/
DateRange.prototype.toString = function() {
return this.start.format() + '/' + this.end.format();
};
/**
* Date range in milliseconds. Allows basic coercion math of date ranges.
*
* @return {!number}
*/
DateRange.prototype.valueOf = function() {
return this.end - this.start;
};
/**
* Center date of the range.
*
* @return {!Moment}
*/
DateRange.prototype.center = function() {
var center = this.start + this.diff() / 2;
return moment(center);
};
/**
* Date range toDate
*
* @return {!Array<Date>}
*/
DateRange.prototype.toDate = function() {
return [this.start.toDate(), this.end.toDate()];
};
/**
* Determine if this date range is the same as another.
*
* @param {!DateRange} other Another date range to compare to
*
* @return {!boolean}
*/
DateRange.prototype.isSame = function(other) {
return this.start.isSame(other.start) && this.end.isSame(other.end);
};
/**
* The difference of the end vs start.
*
* @param {number} unit Unit of difference, if no unit is passed in
* milliseconds are returned. E.g.: `"days"`, `"months"`,
* etc...
*
* @return {!number}
*/
DateRange.prototype.diff = function(unit) {
return this.end.diff(this.start, unit);
};
//-----------------------------------------------------------------------------
// Moment Extensions
//-----------------------------------------------------------------------------
/**
* Build a date range.
*
* @param {(Moment|Date)} start Start of range
* @param {(Moment|Date)} end End of range
*
* @this {Moment}
*
* @return {!DateRange}
*/
moment.range = function(start, end) {
if (start in INTERVALS) {
return new DateRange(moment(this).startOf(start), moment(this).endOf(start));
}
else {
return new DateRange(start, end);
}
};
/**
* Expose constructor
*
* @const
*/
moment.range.constructor = DateRange;
/**
* @deprecated
*/
moment.fn.range = moment.range;
/**
* Check if the current moment is within a given date range.
*
* @param {!DateRange} range Date range to check
*
* @this {Moment}
*
* @return {!boolean}
*/
moment.fn.within = function(range) {
return range.contains(this._d);
};
//-----------------------------------------------------------------------------
// Export
//-----------------------------------------------------------------------------
module.exports = DateRange;