-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpression.js
40 lines (36 loc) · 1.07 KB
/
expression.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
const moment = require('moment');
const { parseExpression } = require('cron-parser');
const isValidCron = expression => {
try {
parseExpression(expression);
return true;
} catch (error) {
return false;
}
};
const getNext = expression => {
switch (typeof expression) {
case 'object':
return (() => {
if (expression instanceof Date) return expression.toISOString();
const keys = Object.keys(expression);
let date = moment();
for (let k = 0; k < keys.length; k += 1) date = date.add(expression[keys[k]], keys[k]);
return date.toISOString();
})();
case 'string':
return (() => {
if (isValidCron(expression))
return parseExpression(expression)
.next()
._date.toISOString();
const date = new Date(expression);
if (date.toString() !== 'Invalid Date') return date.toISOString();
return null;
})();
default:
return null;
}
};
const getTime = expression => moment(getNext(expression)) - moment();
module.exports = { getNext, getTime };