-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
54 lines (46 loc) · 1.4 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
module.exports = function (str) {
var d = new Date()
if (str === 'now' || !str) return d
var op = str[0]
str = str.slice(1)
var qty = parseInt(str, 10)
var unit = str.slice(String(qty).length).trim()
var YEARS = ['years', 'year', 'yr', 'Y']
var MONTHS = ['months', 'month', 'mo', 'M']
var WEEKS = ['weeks', 'week', 'w']
var DAYS = ['days', 'day', 'd']
var HOURS = ['hours', 'hour', 'h']
var MINUTES = ['minutes', 'minute', 'min', 'm']
var SECONDS = ['seconds', 'second', 'sec', 's']
var MILLISECONDS = ['milliseconds', 'millisecond', 'ms']
function compare (unit) {
return function (a) {
return a.indexOf(unit) > -1
}
}
function calc (d, qty, unit) {
qty = (op === '+' ? 1 : -1) * qty
var is = compare(unit)
if (is(YEARS)) {
d.setFullYear(d.getFullYear() + qty)
} else if (is(MONTHS)) {
d.setMonth(d.getMonth() + qty)
} else if (is(WEEKS)) {
return calc(d, qty * 7, 'days')
} else if (is(DAYS)) {
d.setDate(d.getDate() + qty)
} else if (is(HOURS)) {
d.setHours(d.getHours() + qty)
} else if (is(MINUTES)) {
d.setMinutes(d.getMinutes() + qty)
} else if (is(SECONDS)) {
d.setSeconds(d.getSeconds() + qty)
} else if (is(MILLISECONDS)) {
d.setMilliseconds(d.getMilliseconds() + qty)
} else {
throw new Error('Invalid unit: ' + unit)
}
return d
}
return calc(d, qty, unit)
}