Skip to content

Commit ff20119

Browse files
authored
Remove duplicate day of week values upon stringify (#295)
1 parent cac06b9 commit ff20119

File tree

2 files changed

+41
-22
lines changed

2 files changed

+41
-22
lines changed

lib/expression.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -816,9 +816,16 @@ CronExpression.prototype.stringify = function stringify(includeSeconds) {
816816
for (var i = includeSeconds ? 0 : 1, c = CronExpression.map.length; i < c; ++i) {
817817
var field = CronExpression.map[i];
818818
var value = this.fields[field];
819-
var constraint = field === 'dayOfMonth' && this.fields.month.length === 1
820-
? { min: 1, max: CronExpression.daysInMonth[this.fields.month[0] - 1] }
821-
: CronExpression.constraints[i];
819+
var constraint = CronExpression.constraints[i];
820+
821+
if (field === 'dayOfMonth' && this.fields.month.length === 1) {
822+
constraint = { min: 1, max: CronExpression.daysInMonth[this.fields.month[0] - 1] };
823+
} else if (field === 'dayOfWeek') {
824+
// Prefer 0-6 range when serializing day of week field
825+
constraint = { min: 0, max: 6 };
826+
value = value[value.length - 1] === 7 ? value.slice(0, -1) : value;
827+
}
828+
822829
resultArr.push(stringifyField(value, constraint.min, constraint.max));
823830
}
824831
return resultArr.join(' ');

test/stringify.js

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ var test = require('tap').test;
44
var CronParser = require('../lib/parser');
55

66
test('stringify cron expression all stars no seconds', function (t) {
7-
87
try {
98
var expected = '0 * * * * *';
109
var interval = CronParser.parseExpression('* * * * *', {});
@@ -21,7 +20,6 @@ test('stringify cron expression all stars no seconds', function (t) {
2120
});
2221

2322
test('stringify cron expression all stars no seconds (discard seconds)', function (t) {
24-
2523
try {
2624
var expected = '* * * * *';
2725
var interval = CronParser.parseExpression('* * * * *', {});
@@ -38,7 +36,6 @@ test('stringify cron expression all stars no seconds (discard seconds)', functio
3836
});
3937

4038
test('stringify cron expression all stars with seconds', function (t) {
41-
4239
try {
4340
var expected = '* * * * * *';
4441
var interval = CronParser.parseExpression('* * * * * *', {});
@@ -55,7 +52,6 @@ test('stringify cron expression all stars with seconds', function (t) {
5552
});
5653

5754
test('stringify cron expression all stars with seconds (discard seconds)', function (t) {
58-
5955
try {
6056
var expected = '* * * * *';
6157
var interval = CronParser.parseExpression('* * * * * *', {});
@@ -72,7 +68,6 @@ test('stringify cron expression all stars with seconds (discard seconds)', funct
7268
});
7369

7470
test('stringify cron expression', function (t) {
75-
7671
try {
7772
var expected = '0 1,2,4-10,20-35/5,57 * * * *';
7873
var interval = CronParser.parseExpression('1,2,4-10,20-35/5,57 * * * *', {});
@@ -89,7 +84,6 @@ test('stringify cron expression', function (t) {
8984
});
9085

9186
test('stringify cron expression (discard seconds)', function (t) {
92-
9387
try {
9488
var expected = '1,2,4-10,20-35/5,57 * * * *';
9589
var interval = CronParser.parseExpression('1,2,4-10,20-35/5,57 * * * *', {});
@@ -106,7 +100,6 @@ test('stringify cron expression (discard seconds)', function (t) {
106100
});
107101

108102
test('stringify cron expression with star range step', function (t) {
109-
110103
try {
111104
var expected = '0 */5 */2 * * *';
112105
var interval = CronParser.parseExpression('*/5 */2 */1 * *', {});
@@ -123,7 +116,6 @@ test('stringify cron expression with star range step', function (t) {
123116
});
124117

125118
test('stringify cron expression with star range step (discard seconds)', function (t) {
126-
127119
try {
128120
var expected = '*/5 */2 * * *';
129121
var interval = CronParser.parseExpression('*/5 */2 */1 * *', {});
@@ -157,7 +149,6 @@ test('stringify cron expression with semi range step', function (t) {
157149
});
158150

159151
test('stringify cron expression with semi range step (discard seconds)', function (t) {
160-
161152
try {
162153
var expected = '5/5 * * * *';
163154
var interval = CronParser.parseExpression('5/5 * * * *', {});
@@ -174,7 +165,6 @@ test('stringify cron expression with semi range step (discard seconds)', functio
174165
});
175166

176167
test('stringify cron expression with L', function (t) {
177-
178168
try {
179169
var expected = '0 * * 1,4-10,L * *';
180170
var interval = CronParser.parseExpression('* * 1,4-10,L * *', {});
@@ -191,7 +181,6 @@ test('stringify cron expression with L', function (t) {
191181
});
192182

193183
test('stringify cron expression with L (discard seconds)', function (t) {
194-
195184
try {
196185
var expected = '* * 1,4-10,L * *';
197186
var interval = CronParser.parseExpression('* * 1,4-10,L * *', {});
@@ -208,7 +197,6 @@ test('stringify cron expression with L (discard seconds)', function (t) {
208197
});
209198

210199
test('stringify cron expression with weekday L', function (t) {
211-
212200
try {
213201
var expected = '0 0 0 * * 1L';
214202
var interval = CronParser.parseExpression(expected, {});
@@ -225,7 +213,6 @@ test('stringify cron expression with weekday L', function (t) {
225213
});
226214

227215
test('stringify cron expression with multiple weekday, one of them with an L', function (t) {
228-
229216
try {
230217
var expected = '0 0 0 * * 4,6L';
231218
var interval = CronParser.parseExpression(expected, {});
@@ -242,7 +229,6 @@ test('stringify cron expression with multiple weekday, one of them with an L', f
242229
});
243230

244231
test('stringify cron expression with multiple weekday, two of them with an L', function (t) {
245-
246232
try {
247233
var expected = '0 0 0 * * 1L,5L';
248234
var interval = CronParser.parseExpression(expected, {});
@@ -259,7 +245,6 @@ test('stringify cron expression with multiple weekday, two of them with an L', f
259245
});
260246

261247
test('stringify cron expression with wildcard day of month and single month value', function (t) {
262-
263248
try {
264249
var expected = '* * * 4 *';
265250
var interval = CronParser.parseExpression(expected, {});
@@ -273,7 +258,6 @@ test('stringify cron expression with wildcard day of month and single month valu
273258
});
274259

275260
test('stringify cron expression with wildcard day of month and month rangee', function (t) {
276-
277261
try {
278262
var expected = '* * * 4-6 *';
279263
var interval = CronParser.parseExpression(expected, {});
@@ -288,7 +272,6 @@ test('stringify cron expression with wildcard day of month and month rangee', fu
288272

289273

290274
test('stringify cron expression with day of month range and single month value', function (t) {
291-
292275
try {
293276
var expected = '* * 1-25 4 *';
294277
var interval = CronParser.parseExpression(expected, {});
@@ -302,7 +285,6 @@ test('stringify cron expression with day of month range and single month value',
302285
});
303286

304287
test('stringify from fields out of order', function (t) {
305-
306288
try {
307289
var expected = '1-5 1 1 1 1 1';
308290
var str = CronParser.fieldsToExpression({
@@ -322,7 +304,6 @@ test('stringify from fields out of order', function (t) {
322304
});
323305

324306
test('stringify from fields out of order (discard seconds)', function (t) {
325-
326307
try {
327308
var expected = '1 1 1 1 1';
328309
var str = CronParser.fieldsToExpression({
@@ -341,6 +322,37 @@ test('stringify from fields out of order (discard seconds)', function (t) {
341322
t.end();
342323
});
343324

325+
test('stringify cron expression with extended day of week range (0,7)', function (t) {
326+
try {
327+
var expected = '* * * * *';
328+
var interval = CronParser.parseExpression('* * * * *');
329+
330+
var str = CronParser.fieldsToExpression({
331+
second: interval.fields.second,
332+
minute: interval.fields.minute,
333+
hour: interval.fields.hour,
334+
month: interval.fields.month,
335+
dayOfMonth: interval.fields.dayOfMonth,
336+
dayOfWeek: [0, 1, 2, 3, 4, 5, 6],
337+
}).stringify();
338+
t.equal(str, expected);
339+
340+
str = CronParser.fieldsToExpression({
341+
second: interval.fields.second,
342+
minute: interval.fields.minute,
343+
hour: interval.fields.hour,
344+
month: interval.fields.month,
345+
dayOfMonth: interval.fields.dayOfMonth,
346+
dayOfWeek: [0, 1, 2, 3, 4, 5, 6, 7],
347+
}).stringify();
348+
t.equal(str, expected);
349+
} catch (err) {
350+
t.error(err, 'Parse read error');
351+
}
352+
353+
t.end();
354+
});
355+
344356
test('validation error - missing seconds', function (t) {
345357
t.throws(function () {
346358
CronParser.fieldsToExpression({

0 commit comments

Comments
 (0)