1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = void 0;
|
7 |
|
8 | var _momentTimezone = _interopRequireDefault(require("moment-timezone"));
|
9 |
|
10 | var _sortedIndex = _interopRequireDefault(require("lodash/sortedIndex"));
|
11 |
|
12 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
13 |
|
14 | const NEXT_MAPPING = {
|
15 | month: {
|
16 | year: 1
|
17 | },
|
18 | date: {
|
19 | month: 1
|
20 | },
|
21 | day: {
|
22 | week: 1
|
23 | },
|
24 | hour: {
|
25 | day: 1
|
26 | },
|
27 | minute: {
|
28 | hour: 1
|
29 | }
|
30 | };
|
31 |
|
32 | const getFirst = values => values !== undefined ? values[0] : 0;
|
33 |
|
34 | const setFirstAvailable = (date, unit, values) => {
|
35 | if (values === undefined) {
|
36 | return;
|
37 | }
|
38 |
|
39 | const curr = date.get(unit);
|
40 | const next = values[(0, _sortedIndex.default)(values, curr) % values.length];
|
41 |
|
42 | if (curr === next) {
|
43 | return;
|
44 | }
|
45 |
|
46 | const timestamp = +date;
|
47 | date.set(unit, next);
|
48 |
|
49 | if (timestamp > +date) {
|
50 | date.add(NEXT_MAPPING[unit]);
|
51 | }
|
52 |
|
53 | return true;
|
54 | };
|
55 |
|
56 | var _default = (schedule, fromDate) => {
|
57 | let date = (0, _momentTimezone.default)(fromDate).set({
|
58 | second: 0,
|
59 | millisecond: 0
|
60 | }).add({
|
61 | minute: 1
|
62 | });
|
63 | const minute = schedule.minute,
|
64 | hour = schedule.hour,
|
65 | dayOfMonth = schedule.dayOfMonth,
|
66 | month = schedule.month,
|
67 | dayOfWeek = schedule.dayOfWeek;
|
68 | setFirstAvailable(date, 'minute', minute);
|
69 |
|
70 | if (setFirstAvailable(date, 'hour', hour)) {
|
71 | date.set('minute', getFirst(minute));
|
72 | }
|
73 |
|
74 | let loop;
|
75 | let i = 0;
|
76 |
|
77 | do {
|
78 | loop = false;
|
79 |
|
80 | if (setFirstAvailable(date, 'month', month)) {
|
81 | date.set({
|
82 | date: 1,
|
83 | hour: getFirst(hour),
|
84 | minute: getFirst(minute)
|
85 | });
|
86 | }
|
87 |
|
88 | let newDate = date.clone();
|
89 |
|
90 | if (dayOfMonth === undefined) {
|
91 | if (dayOfWeek !== undefined) {
|
92 | setFirstAvailable(newDate, 'day', dayOfWeek);
|
93 | }
|
94 | } else if (dayOfWeek === undefined) {
|
95 | setFirstAvailable(newDate, 'date', dayOfMonth);
|
96 | } else {
|
97 | const dateDay = newDate.clone();
|
98 | setFirstAvailable(dateDay, 'date', dayOfMonth);
|
99 | setFirstAvailable(newDate, 'day', dayOfWeek);
|
100 | newDate = _momentTimezone.default.min(dateDay, newDate);
|
101 | }
|
102 |
|
103 | if (+date !== +newDate) {
|
104 | loop = date.month() !== newDate.month();
|
105 | date = newDate.set({
|
106 | hour: getFirst(hour),
|
107 | minute: getFirst(minute)
|
108 | });
|
109 | }
|
110 | } while (loop && ++i < 5);
|
111 |
|
112 | if (loop) {
|
113 | throw new Error('no solutions found for this schedule');
|
114 | }
|
115 |
|
116 | return date;
|
117 | };
|
118 |
|
119 | exports.default = _default;
|
120 |
|
\ | No newline at end of file |