-
Notifications
You must be signed in to change notification settings - Fork 25
/
cron-builder.js
287 lines (250 loc) · 10.7 KB
/
cron-builder.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
var DEFAULT_INTERVAL = ['*'];
var CronValidator = (function() {
/**
* Contains the position-to-name mapping of the cron expression
* @type {Object}
* @const
*/
var MeasureOfTimeMap = {
0: 'minute',
1: 'hour',
2: 'dayOfTheMonth',
3: 'month',
4: 'dayOfTheWeek'
},
/**
* contains every permissible 'measureOfTime' string constant
* @const
* @type {Array}
*/
MeasureOfTimeValues = Object.keys(MeasureOfTimeMap).map(function (key) {
return MeasureOfTimeMap[key];
});
/**
* validates a given cron expression (object) for length, then calls validateValue on each value
* @param {!{
minute: Array.string,
hour: Array.string,
dayOfTheMonth: Array.string,
month: Array.string,
dayOfTheWeek: Array.string,
* }} expression - rich object containing the state of the cron expression
* @throws {Error} if expression contains more than 5 keys
*/
var validateExpression = function(expression) {
// don't care if it's less than 5, we'll just set those to the default '*'
if (Object.keys(expression).length > 5) {
throw new Error('Invalid cron expression; limited to 5 values.');
}
for (var measureOfTime in expression) {
if (expression.hasOwnProperty(measureOfTime)) {
this.validateValue(measureOfTime, expression[measureOfTime]);
}
}
},
/**
* validates a given cron expression (string) for length, then calls validateValue on each value
* @param {!String} expression - an optionally empty string containing at most 5 space delimited expressions.
* @throws {Error} if the string contains more than 5 space delimited parts.
*/
validateString = function(expression) {
var splitExpression = expression.split(' ');
if (splitExpression.length > 5) {
throw new Error('Invalid cron expression; limited to 5 values.');
}
for (var i = 0; i < splitExpression.length; i++) {
this.validateValue(MeasureOfTimeMap[i], splitExpression[i]);
}
},
/**
* validates any given measureOfTime and corresponding value
* @param {!String} measureOfTime - as expected
* @param {!String} value - the cron-ish interval specifier
* @throws {Error} if measureOfTime is bogus
* @throws {Error} if value contains an unsupported character
*/
validateValue = function(measureOfTime, value) {
var validatorObj = {
minute: {min: 0, max: 59},
hour: {min: 0, max: 23},
dayOfTheMonth: {min: 1, max: 31},
month: {min: 1, max: 12},
dayOfTheWeek: {min: 0, max: 7}
},
range,
validChars = /^[0-9*-]/;
if (!validatorObj[measureOfTime]) {
throw new Error('Invalid measureOfTime; Valid options are: ' + MeasureOfTimeValues.join(', '));
}
if (!validChars.test(value)) {
throw new Error('Invalid value; Only numbers 0-9, "-", and "*" chars are allowed');
}
if (value !== '*') {
// check to see if value is within range if value is not '*'
if (value.indexOf('-') >= 0) {
// value is a range and must be split into high and low
range = value.split('-');
if (!range[0] || range[0] < validatorObj[measureOfTime].min) {
throw new Error('Invalid value; bottom of range is not valid for "' + measureOfTime + '". Limit is ' + validatorObj[measureOfTime].min + '.');
}
if (!range[1] || range[1] > validatorObj[measureOfTime].max) {
throw new Error('Invalid value; top of range is not valid for "' + measureOfTime + '". Limit is ' + validatorObj[measureOfTime].max + '.');
}
} else {
if (parseInt(value) < validatorObj[measureOfTime].min) {
throw new Error('Invalid value; given value is not valid for "' + measureOfTime + '". Minimum value is "' + validatorObj[measureOfTime].min + '".');
}
if (parseInt(value) > validatorObj[measureOfTime].max) {
throw new Error('Invalid value; given value is not valid for "' + measureOfTime + '". Maximum value is "' + validatorObj[measureOfTime].max + '".');
}
}
}
};
return {
measureOfTimeValues: MeasureOfTimeValues,
validateExpression: validateExpression,
validateString: validateString,
validateValue: validateValue
}
}());
/**
* Initializes a CronBuilder with an optional initial cron expression.
* @param {String=} initialExpression - if provided, it must be up to 5 space delimited parts
* @throws {Error} if the initialExpression is bogus
* @constructor
*/
var CronBuilder = (function() {
function CronBuilder(initialExpression) {
var splitExpression,
expression;
if (initialExpression) {
CronValidator.validateString(initialExpression);
splitExpression = initialExpression.split(' ');
// check to see if initial expression is valid
expression = {
minute: splitExpression[0] ? [splitExpression[0]] : DEFAULT_INTERVAL,
hour: splitExpression[1] ? [splitExpression[1]] : DEFAULT_INTERVAL,
dayOfTheMonth: splitExpression[2] ? [splitExpression[2]] : DEFAULT_INTERVAL,
month: splitExpression[3] ? [splitExpression[3]] : DEFAULT_INTERVAL,
dayOfTheWeek: splitExpression[4] ? [splitExpression[4]] : DEFAULT_INTERVAL,
};
} else {
expression = {
minute: DEFAULT_INTERVAL,
hour: DEFAULT_INTERVAL,
dayOfTheMonth: DEFAULT_INTERVAL,
month: DEFAULT_INTERVAL,
dayOfTheWeek: DEFAULT_INTERVAL,
};
}
/**
* builds a working cron expression based on the state of the cron object
* @returns {string} - working cron expression
*/
this.build = function () {
return [
expression.minute.join(','),
expression.hour.join(','),
expression.dayOfTheMonth.join(','),
expression.month.join(','),
expression.dayOfTheWeek.join(','),
].join(' ');
};
/**
* adds a value to what exists currently (builds)
* @param {!String} measureOfTime
* @param {!Number} value
* @throws {Error} if measureOfTime or value fail validation
*/
this.addValue = function (measureOfTime, value) {
CronValidator.validateValue(measureOfTime, value);
if (expression[measureOfTime].length === 1 && expression[measureOfTime][0] === '*') {
expression[measureOfTime] = [value];
} else {
if (expression[measureOfTime].indexOf(value) < 0) {
expression[measureOfTime].push(value);
}
}
};
/**
* removes a single explicit value (subtracts)
* @param {!String} measureOfTime - as you might guess
* @param {!String} value - the offensive value
* @throws {Error} if measureOfTime is bogus.
*/
this.removeValue = function (measureOfTime, value) {
if (!expression[measureOfTime]) {
throw new Error('Invalid measureOfTime: Valid options are: ' + CronValidator.measureOfTimeValues.join(', '));
}
if (expression[measureOfTime].length === 1 && expression[measureOfTime][0] === '*') {
return 'The value for "' + measureOfTime + '" is already at the default value of "*" - this is a no-op.';
}
expression[measureOfTime] = expression[measureOfTime].filter(function (timeValue) {
return value !== timeValue;
});
if (!expression[measureOfTime].length) {
expression[measureOfTime] = DEFAULT_INTERVAL;
}
};
/**
* returns the current state of a given measureOfTime
* @param {!String} measureOfTime one of "minute", "hour", etc
* @returns {!String} comma separated blah blah
* @throws {Error} if the measureOfTime is not one of the permitted values.
*/
this.get = function (measureOfTime) {
if (!expression[measureOfTime]) {
throw new Error('Invalid measureOfTime: Valid options are: ' + CronValidator.measureOfTimeValues.join(', '));
}
return expression[measureOfTime].join(',');
};
/**
* sets the state of a given measureOfTime
* @param {!String} measureOfTime - yup
* @param {!Array.<String>} value - the 5 tuple array of values to set
* @returns {!String} the comma separated version of the value that you passed in
* @throws {Error} if your "value" is not an Array<String>
* @throws {Error} when any item in your value isn't a legal cron-ish descriptor
*/
this.set = function (measureOfTime, value) {
if (!Array.isArray(value)) {
throw new Error('Invalid value; Value must be in the form of an Array.');
}
for(var i = 0; i < value.length; i++) {
CronValidator.validateValue(measureOfTime, value[i]);
}
expression[measureOfTime] = value;
return expression[measureOfTime].join(',');
};
/**
* Returns a rich object that describes the current state of the cron expression.
* @returns {!{
minute: Array.string,
hour: Array.string,
dayOfTheMonth: Array.string,
month: Array.string,
dayOfTheWeek: Array.string,
* }}
*/
this.getAll = function () {
return expression;
};
/**
* sets the state for the entire cron expression
* @param {!{
minute: Array.string,
hour: Array.string,
dayOfTheMonth: Array.string,
month: Array.string,
dayOfTheWeek: Array.string,
* }} expToSet - the entirety of the cron expression.
* @throws {Error} as usual
*/
this.setAll = function (expToSet) {
CronValidator.validateExpression(expToSet);
expression = expToSet;
};
}
return CronBuilder;
}());
module.exports = CronBuilder;