-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
112 lines (94 loc) · 1.82 KB
/
index.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
/**
* Expose `DateRange`.
*/
module.exports = DateRange;
/**
* Initialize a new DateRange `from` one date `to` another.
*
* @param {Date} [from]
* @param {Date} [to]
* @api public
*/
function DateRange(from, to) {
if (!(this instanceof DateRange)) return new DateRange(from, to);
if (from) this.from(from);
if (to) this.to(to);
}
/**
* Set / get from `date`.
*
* @param {Date} [date]
* @return {Date}
* @api public
*/
DateRange.prototype.from = function(date){
if (0 == arguments.length) return this._from;
this._from = date;
this.normalize();
};
/**
* Set / get to `date`.
*
* @param {Date} [date]
* @return {Date}
* @api public
*/
DateRange.prototype.to = function(date){
if (0 == arguments.length) return this._to;
this._to = date;
this.normalize();
};
/**
* Normalize the dates.
*
* @api private
*/
DateRange.prototype.normalize = function(){
if (this._from > this._to) {
var from = this._from;
this._from = this._to;
this._to = from;
}
};
/**
* Return diff in milliseconds
*
* @return {Number}
* @api public
*/
DateRange.prototype.diff = function(){
return this._to - this._from;
};
/**
* Return JSON representation.
*
* @return {Object}
* @api public
*/
DateRange.prototype.toJSON = function(){
return {
from: this._from,
to: this._to
};
};
/**
* Check if this range is identical to `other`.
*
* @param {DateRange} other
* @return {Boolean}
* @api public
*/
DateRange.prototype.equals = function(other){
if (!(other instanceof DateRange)) throw new TypeError('other must be a DateRange');
return this._from == other._from
&& this._to == other._to;
};
/**
* Return string representation.
*
* @return {String}
* @api public
*/
DateRange.prototype.toString = function(){
return '[DateRange ' + this._from + ' => ' + this._to + ']';
};