UNPKG

7.26 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
9
10function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
11
12function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
13
14function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
15
16function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
17
18function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
19
20function _toArray(arr) { return _arrayWithHoles(arr) || _iterableToArray(arr) || _nonIterableRest(); }
21
22function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
23
24function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
25
26function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
27
28const compareNumbers = (a, b) => a - b;
29
30const createParser = (_ref) => {
31 let _ref$fields = _toArray(_ref.fields),
32 fields = _ref$fields.slice(0),
33 presets = _extends({}, _ref.presets);
34
35 const m = fields.length;
36
37 for (let j = 0; j < m; ++j) {
38 const field = fields[j];
39 let aliases = field.aliases;
40
41 if (aliases !== undefined) {
42 let symbols = aliases;
43
44 if (Array.isArray(aliases)) {
45 aliases = {};
46
47 const _field$range = _slicedToArray(field.range, 1),
48 start = _field$range[0];
49
50 symbols.forEach((alias, i) => {
51 aliases[alias] = start + i;
52 });
53 } else {
54 symbols = Object.keys(aliases);
55 }
56
57 fields[j] = _objectSpread({}, field, {
58 aliases,
59 aliasesRegExp: new RegExp(symbols.join('|'), 'y')
60 });
61 }
62 }
63
64 let field, i, n, pattern, schedule, values;
65
66 const isDigit = c => c >= '0' && c <= '9';
67
68 const match = c => pattern[i] === c && (++i, true);
69
70 const consumeWhitespaces = () => {
71 let c;
72
73 while ((c = pattern[i]) === ' ' || c === '\t') {
74 ++i;
75 }
76 };
77
78 const parseInteger = () => {
79 let c;
80 const digits = [];
81
82 while (isDigit(c = pattern[i])) {
83 ++i;
84 digits.push(c);
85 }
86
87 if (digits.length === 0) {
88 throw new SyntaxError("".concat(field.name, ": missing integer at character ").concat(i));
89 }
90
91 return Number.parseInt(digits.join(''), 10);
92 };
93
94 const parseValue = () => {
95 let value;
96 const _field = field,
97 aliasesRegExp = _field.aliasesRegExp;
98
99 if (aliasesRegExp === undefined || isDigit(pattern[i])) {
100 value = parseInteger();
101 const _field2 = field,
102 post = _field2.post;
103
104 if (post !== undefined) {
105 value = post(value);
106 }
107 } else {
108 aliasesRegExp.lastIndex = i;
109 const matches = aliasesRegExp.exec(pattern);
110
111 if (matches === null) {
112 throw new SyntaxError("".concat(field.name, ": missing alias or integer at character ").concat(i));
113 }
114
115 const _matches = _slicedToArray(matches, 1),
116 alias = _matches[0];
117
118 i += alias.length;
119 value = field.aliases[alias];
120 }
121
122 const _field3 = field,
123 range = _field3.range;
124
125 if (value < range[0] || value > range[1]) {
126 throw new SyntaxError("".concat(field.name, ": ").concat(value, " is not between ").concat(range[0], " and ").concat(range[1]));
127 }
128
129 return value;
130 };
131
132 const parseRange = () => {
133 let end, start, step;
134
135 if (match('*')) {
136 if (!match('/')) {
137 return;
138 }
139
140 ;
141
142 var _field$range2 = _slicedToArray(field.range, 2);
143
144 start = _field$range2[0];
145 end = _field$range2[1];
146 step = parseInteger();
147 } else {
148 start = parseValue();
149
150 if (!match('-')) {
151 values.add(start);
152 return;
153 }
154
155 end = parseValue();
156 step = match('/') ? parseInteger() : 1;
157 }
158
159 for (let i = start; i <= end; i += step) {
160 values.add(i);
161 }
162 };
163
164 const parseSequence = () => {
165 do {
166 parseRange();
167 } while (match(','));
168 };
169
170 const parse = p => {
171 {
172 const schedule = presets[p];
173
174 if (schedule !== undefined) {
175 return typeof schedule === 'string' ? presets[p] = parse(schedule) : schedule;
176 }
177 }
178
179 try {
180 i = 0;
181 n = p.length;
182 pattern = p;
183 schedule = {};
184
185 for (let j = 0; j < m; ++j) {
186 consumeWhitespaces();
187 field = fields[j];
188 values = new Set();
189 parseSequence();
190
191 if (values.size !== 0) {
192 schedule[field.name] = Array.from(values).sort(compareNumbers);
193 }
194 }
195
196 consumeWhitespaces();
197
198 if (i !== n) {
199 throw new SyntaxError("unexpected character at offset ".concat(i, ", expected end"));
200 }
201
202 return schedule;
203 } finally {
204 field = pattern = schedule = values = undefined;
205 }
206 };
207
208 return parse;
209};
210
211var _default = createParser({
212 fields: [{
213 name: 'minute',
214 range: [0, 59]
215 }, {
216 name: 'hour',
217 range: [0, 23]
218 }, {
219 name: 'dayOfMonth',
220 range: [1, 31]
221 }, {
222 aliases: 'jan feb mar apr may jun jul aug sep oct nov dec'.split(' '),
223 name: 'month',
224 range: [0, 11]
225 }, {
226 aliases: 'sun mon tue wen thu fri sat'.split(' '),
227 name: 'dayOfWeek',
228 post: value => value === 7 ? 0 : value,
229 range: [0, 6]
230 }],
231 presets: {
232 '@annually': '0 0 1 jan *',
233 '@daily': '0 0 * * *',
234 '@hourly': '0 * * * *',
235 '@monthly': '0 0 1 * *',
236 '@weekly': '0 0 * * sun',
237 '@yearly': '0 0 1 jan *'
238 }
239});
240
241exports.default = _default;
242//# sourceMappingURL=parse.js.map
\No newline at end of file