-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
111 lines (104 loc) · 2.63 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
const units = {
'ms': 'milliseconds',
'milliseconds': 'milliseconds',
's': 'seconds',
'sec': 'seconds',
'second': 'seconds',
'seconds': 'seconds',
'Second': 'seconds',
'Seconds': 'seconds',
'm': 'minutes',
'min': 'minutes',
'minute': 'minutes',
'minutes': 'minutes',
'Min': 'minutes',
'Minute': 'minutes',
'Minutes': 'minutes',
'h': 'hours',
'hr': 'hours',
'Hr': 'hours',
'hour': 'hours',
'hours': 'hours',
'Hour': 'hours',
'Hours': 'hours',
'd': 'days',
'D': 'days',
'day': 'days',
'days': 'days',
'Day': 'days',
'Days': 'days',
'w': 'weeks',
'W': 'weeks',
'wk': 'weeks',
'Wk': 'weeks',
'week': 'weeks',
'weeks': 'weeks',
'Week': 'weeks',
'Weeks': 'weeks',
'M': 'months',
'mo': 'months',
'mon': 'months',
'month': 'months',
'months': 'months',
'Month': 'months',
'Months': 'months',
'y': 'years',
'Y': 'years',
'yr': 'years',
'yrs': 'years',
'year': 'years',
'years': 'years',
'Year': 'years',
'Years': 'years',
'q': 'quarters',
'qr': 'quarters',
'Q': 'quarters',
'Qr': 'quarters',
'qtr': 'quarters',
'quarter': 'quarters',
'quarters': 'quarters',
'Quarter': 'quarters',
'Quarters': 'quarters'
};
const unitToMilliseconds = {
'milliseconds': 1,
'seconds': 1000,
'minutes': 1000 * 60,
'hours': 1000 * 60 * 60,
'days': 1000 * 60 * 60 * 24,
'weeks': 1000 * 60 * 60 * 24 * 7,
'months': 1000 * 60 * 60 * 24 * 30, // Approximation
'years': 1000 * 60 * 60 * 24 * 365, // Approximation
'quarters': 1000 * 60 * 60 * 24 * 365 / 4 // Approximation
};
const parseableUnits = Object.keys(units).join('|');
const regExp = new RegExp(`([-+])?(\\d*)\\s?(${parseableUnits})$`);
/**
* Checks if a time expression is valid
* @param expression {string} - The time expression to check
* @return {boolean}
*/
export function isValid(expression) {
return regExp.test(expression);
}
/**
* Converts a time expression to a Date object
* @param expression {string} - The time expression to convert
* @param currentDate {Date} - The current date to use as a reference
* @return {Date|null}
*/
export function relativeToDate(expression, currentDate = new Date()) {
if (typeof expression !== 'string') {
throw new TypeError('The expression should be a string');
}
const match = expression.match(regExp);
if (!match) {
throw new Error('The expression is not a valid expression');
}
const operator = match[1] === '-' ? -1 : 1;
const amount = parseInt(match[2]);
const unit = units[match[3]];
const milliseconds = amount * unitToMilliseconds[unit];
const time = currentDate.getTime() + (operator * milliseconds);
return new Date(time);
}