Skip to content

Commit

Permalink
Remove duplicate day of week values upon stringify (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
harrisiirak committed Nov 16, 2022
1 parent cac06b9 commit ff20119
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 deletions.
13 changes: 10 additions & 3 deletions lib/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -816,9 +816,16 @@ CronExpression.prototype.stringify = function stringify(includeSeconds) {
for (var i = includeSeconds ? 0 : 1, c = CronExpression.map.length; i < c; ++i) {
var field = CronExpression.map[i];
var value = this.fields[field];
var constraint = field === 'dayOfMonth' && this.fields.month.length === 1
? { min: 1, max: CronExpression.daysInMonth[this.fields.month[0] - 1] }
: CronExpression.constraints[i];
var constraint = CronExpression.constraints[i];

if (field === 'dayOfMonth' && this.fields.month.length === 1) {
constraint = { min: 1, max: CronExpression.daysInMonth[this.fields.month[0] - 1] };
} else if (field === 'dayOfWeek') {
// Prefer 0-6 range when serializing day of week field
constraint = { min: 0, max: 6 };
value = value[value.length - 1] === 7 ? value.slice(0, -1) : value;
}

resultArr.push(stringifyField(value, constraint.min, constraint.max));
}
return resultArr.join(' ');
Expand Down
50 changes: 31 additions & 19 deletions test/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var test = require('tap').test;
var CronParser = require('../lib/parser');

test('stringify cron expression all stars no seconds', function (t) {

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

test('stringify cron expression all stars no seconds (discard seconds)', function (t) {

try {
var expected = '* * * * *';
var interval = CronParser.parseExpression('* * * * *', {});
Expand All @@ -38,7 +36,6 @@ test('stringify cron expression all stars no seconds (discard seconds)', functio
});

test('stringify cron expression all stars with seconds', function (t) {

try {
var expected = '* * * * * *';
var interval = CronParser.parseExpression('* * * * * *', {});
Expand All @@ -55,7 +52,6 @@ test('stringify cron expression all stars with seconds', function (t) {
});

test('stringify cron expression all stars with seconds (discard seconds)', function (t) {

try {
var expected = '* * * * *';
var interval = CronParser.parseExpression('* * * * * *', {});
Expand All @@ -72,7 +68,6 @@ test('stringify cron expression all stars with seconds (discard seconds)', funct
});

test('stringify cron expression', function (t) {

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

test('stringify cron expression (discard seconds)', function (t) {

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

test('stringify cron expression with star range step', function (t) {

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

test('stringify cron expression with star range step (discard seconds)', function (t) {

try {
var expected = '*/5 */2 * * *';
var interval = CronParser.parseExpression('*/5 */2 */1 * *', {});
Expand Down Expand Up @@ -157,7 +149,6 @@ test('stringify cron expression with semi range step', function (t) {
});

test('stringify cron expression with semi range step (discard seconds)', function (t) {

try {
var expected = '5/5 * * * *';
var interval = CronParser.parseExpression('5/5 * * * *', {});
Expand All @@ -174,7 +165,6 @@ test('stringify cron expression with semi range step (discard seconds)', functio
});

test('stringify cron expression with L', function (t) {

try {
var expected = '0 * * 1,4-10,L * *';
var interval = CronParser.parseExpression('* * 1,4-10,L * *', {});
Expand All @@ -191,7 +181,6 @@ test('stringify cron expression with L', function (t) {
});

test('stringify cron expression with L (discard seconds)', function (t) {

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

test('stringify cron expression with weekday L', function (t) {

try {
var expected = '0 0 0 * * 1L';
var interval = CronParser.parseExpression(expected, {});
Expand All @@ -225,7 +213,6 @@ test('stringify cron expression with weekday L', function (t) {
});

test('stringify cron expression with multiple weekday, one of them with an L', function (t) {

try {
var expected = '0 0 0 * * 4,6L';
var interval = CronParser.parseExpression(expected, {});
Expand All @@ -242,7 +229,6 @@ test('stringify cron expression with multiple weekday, one of them with an L', f
});

test('stringify cron expression with multiple weekday, two of them with an L', function (t) {

try {
var expected = '0 0 0 * * 1L,5L';
var interval = CronParser.parseExpression(expected, {});
Expand All @@ -259,7 +245,6 @@ test('stringify cron expression with multiple weekday, two of them with an L', f
});

test('stringify cron expression with wildcard day of month and single month value', function (t) {

try {
var expected = '* * * 4 *';
var interval = CronParser.parseExpression(expected, {});
Expand All @@ -273,7 +258,6 @@ test('stringify cron expression with wildcard day of month and single month valu
});

test('stringify cron expression with wildcard day of month and month rangee', function (t) {

try {
var expected = '* * * 4-6 *';
var interval = CronParser.parseExpression(expected, {});
Expand All @@ -288,7 +272,6 @@ test('stringify cron expression with wildcard day of month and month rangee', fu


test('stringify cron expression with day of month range and single month value', function (t) {

try {
var expected = '* * 1-25 4 *';
var interval = CronParser.parseExpression(expected, {});
Expand All @@ -302,7 +285,6 @@ test('stringify cron expression with day of month range and single month value',
});

test('stringify from fields out of order', function (t) {

try {
var expected = '1-5 1 1 1 1 1';
var str = CronParser.fieldsToExpression({
Expand All @@ -322,7 +304,6 @@ test('stringify from fields out of order', function (t) {
});

test('stringify from fields out of order (discard seconds)', function (t) {

try {
var expected = '1 1 1 1 1';
var str = CronParser.fieldsToExpression({
Expand All @@ -341,6 +322,37 @@ test('stringify from fields out of order (discard seconds)', function (t) {
t.end();
});

test('stringify cron expression with extended day of week range (0,7)', function (t) {
try {
var expected = '* * * * *';
var interval = CronParser.parseExpression('* * * * *');

var str = CronParser.fieldsToExpression({
second: interval.fields.second,
minute: interval.fields.minute,
hour: interval.fields.hour,
month: interval.fields.month,
dayOfMonth: interval.fields.dayOfMonth,
dayOfWeek: [0, 1, 2, 3, 4, 5, 6],
}).stringify();
t.equal(str, expected);

str = CronParser.fieldsToExpression({
second: interval.fields.second,
minute: interval.fields.minute,
hour: interval.fields.hour,
month: interval.fields.month,
dayOfMonth: interval.fields.dayOfMonth,
dayOfWeek: [0, 1, 2, 3, 4, 5, 6, 7],
}).stringify();
t.equal(str, expected);
} catch (err) {
t.error(err, 'Parse read error');
}

t.end();
});

test('validation error - missing seconds', function (t) {
t.throws(function () {
CronParser.fieldsToExpression({
Expand Down

0 comments on commit ff20119

Please sign in to comment.