-
Notifications
You must be signed in to change notification settings - Fork 23
/
timekeeper.js
205 lines (176 loc) · 4.22 KB
/
timekeeper.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
/**
* Time keeper - EEasy testing of time-dependent code.
*
* Veselin Todorov <hi@vesln.com>
* MIT License.
*/
!(function(name, root, definition) {
definition = definition();
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = definition;
}
exports[name] = definition;
} else {
root[name] = definition;
}
})('timekeeper', this, function() {
/**
* Native Date constructor reference.
*
* @type {Object}
*/
var NativeDate = Date;
/**
* `TimeKeeper`.
*
* @type {Object}
*/
var timekeeper = {};
/**
* Frozen date time container.
*
* @type {Object}
*/
var freeze = null;
/**
* Fake date time container.
*
* @type {Number}
*/
var travel = null;
/**
* Travel start container.
*
* @type {Number}
*/
var started = null;
/**
* Return the elapsed time.
*
* @returns {Number}
*/
function time() {
return travel + (NativeDate.now() - started);
}
/**
* `FakeDate` constructor.
*/
function FakeDate(Y, M, D, h, m, s, ms) {
var length = arguments.length;
if (this instanceof NativeDate) {
if (!length && freeze) return new NativeDate(freeze.getTime());
if (!length && travel) return new NativeDate(time());
var date = length == 1 && String(Y) === Y ? // isString(Y)
// We explicitly pass it through parse:
new NativeDate(NativeDate.parse(Y)) :
// We have to manually make calls depending on argument
// length here
length >= 7 ? new NativeDate(Y, M, D, h, m, s, ms) :
length >= 6 ? new NativeDate(Y, M, D, h, m, s) :
length >= 5 ? new NativeDate(Y, M, D, h, m) :
length >= 4 ? new NativeDate(Y, M, D, h) :
length >= 3 ? new NativeDate(Y, M, D) :
length >= 2 ? new NativeDate(Y, M) :
length >= 1 ? new NativeDate(Y) :
new NativeDate();
// Prevent mixups with unfixed Date object
date.constructor = NativeDate;
return date;
}
return NativeDate.apply(this, arguments);
}
// Copy any custom methods a 3rd party library may have added
(function() {
for (var key in NativeDate) {
FakeDate[key] = NativeDate[key];
}
}());
// Copy "native" methods explicitly; they may be non-enumerable
FakeDate.UTC = NativeDate.UTC;
FakeDate.parse = NativeDate.parse;
// Setup inheritance
FakeDate.prototype = NativeDate.prototype;
FakeDate.prototype.constructor = NativeDate;
/**
* Replace the original now method.
*
* Check if the time is
*
* - frozen and if so return the frozen time
* - faked and if so return the elapsed time
*
* @returns {Number}
* @api public
*/
FakeDate.now = function() {
if (freeze) return freeze.getTime();
if (travel) return time();
return NativeDate.now();
};
/**
* Set current Date Time and freeze it.
*
* @param {Object|String|Number} Date.
* @api public
*/
timekeeper.freeze = function(date) {
useFakeDate();
if (typeof date !== 'object') {
date = new NativeDate(date);
}
freeze = date;
};
/**
* Set current DateTime.
*
* @param {Object|String|Number} Date.
* @api public
*/
timekeeper.travel = function(date) {
useFakeDate();
if (typeof date !== 'object') {
date = new NativeDate(date);
}
if (freeze) {
timekeeper.freeze(date)
}
travel = date.getTime();
started = NativeDate.now();
};
/**
* Reset the `timekeeper` behavior.
*
* @api public
*/
timekeeper.reset = function() {
useNativeDate();
freeze = null;
started = null;
travel = null;
};
/**
* Reflection: Are we currently modifying the native Date object?
*
* @api public
*/
timekeeper.isKeepingTime = function() {
return Date === FakeDate;
};
/**
* Replace the `Date` with `FakeDate`.
*/
function useFakeDate() {
Date = FakeDate
}
/**
* Restore the `Date` to `NativeDate`.
*/
function useNativeDate() {
Date = NativeDate
}
/**
* Expose `timekeeper`
*/
return timekeeper;
});