-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (40 loc) · 1.26 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
function defineUnit(name, ratio) {
Object.defineProperty(Number.prototype, name, {
writeable: false,
configurable: true,
get: function() { return this * ratio }
});
}
function load() {
defineUnit('seconds', 1000);
defineUnit('second', 1000);
defineUnit('minutes', 1000 * 60);
defineUnit('minute', 1000 * 60);
defineUnit('hours', 1000 * 60 * 60);
defineUnit('hour', 1000 * 60 * 60);
defineUnit('days', 1000 * 60 * 60 * 24);
defineUnit('day', 1000 * 60 * 60 * 24);
defineUnit('weeks', 1000 * 60 * 60 * 24 * 7);
defineUnit('week', 1000 * 60 * 60 * 24 * 7);
Object.defineProperty(Number.prototype, 'ago', {
writeable: false,
configurable: true,
get: function() { return Date.now() - this }
});
Object.defineProperty(Number.prototype, 'fromNow', {
writeable: false,
configurable: true,
get: function() { return Date.now() + this }
});
Object.defineProperty(Number.prototype, 'asDate', {
writeable: false,
configurable: true,
get: function() { return new Date(this) }
});
}
function unload() {
['second', 'seconds', 'minute', 'minutes', 'hour', 'hours', 'day', 'days', 'week', 'weeks', 'ago', 'fromNow', 'asDate'].forEach((key) => {
delete Number.prototype[key]
})
}
module.exports = { load, unload }