Skip to content

Commit

Permalink
fix: when more than 5 array element, subtract one (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhonatasfender authored Sep 26, 2021
1 parent 92e67c7 commit fdcb3ce
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 111 deletions.
228 changes: 117 additions & 111 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,69 +30,69 @@ var C2Q = {};
*
*
*/
C2Q.getQuartz = function(crontab) {
C2Q.getQuartz = function (crontab) {

var data = [];
var quartzEntry = [];

// check for cron magic entries
quartzEntry = parseCronMagics(crontab);
var data = [];
var quartzEntry = [];

if (quartzEntry) {
data.push(quartzEntry);
} else {
// check for cron magic entries
quartzEntry = parseCronMagics(crontab);

// if cron magic entries not found, proceed to parsing normal cron format
var crontabEntry = crontab.split(' ');
quartzEntry = parseCronSyntax(crontabEntry);
if (quartzEntry) {
data.push(quartzEntry);
} else {

data.push(quartzEntry);
// if cron magic entries not found, proceed to parsing normal cron format
var crontabEntry = crontab.split(' ');
quartzEntry = parseCronSyntax(crontabEntry);

// Quartz doesn't support both DOM and DOW definitions so if we find such ocurrence we'll need to
// create 2 Quartz entries, one with DOM and one with DOW to create an OR expression
data.push(quartzEntry);

if (crontabEntry[2] !== '*' && crontabEntry[4] !== '*') {
// Quartz doesn't support both DOM and DOW definitions so if we find such ocurrence we'll need to
// create 2 Quartz entries, one with DOM and one with DOW to create an OR expression

// by default, parseCronSyntax() gives priority to parse the DOM first so we reset it now to * to
// make sure we also have a variant of the CRON expression with DOW
crontabEntry[2] = '*';
if (crontabEntry[2] !== '*' && crontabEntry[4] !== '*') {

quartzEntry = parseCronSyntax(crontabEntry);
data.push(quartzEntry);
}
// by default, parseCronSyntax() gives priority to parse the DOM first so we reset it now to * to
// make sure we also have a variant of the CRON expression with DOW
crontabEntry[2] = '*';

}
quartzEntry = parseCronSyntax(crontabEntry);
data.push(quartzEntry);
}

return data;
}

return data;
};


function advanceNumber(str) {

var quartzCompatibleStr = '';
var num;
str.split('').forEach(function(chr) {
var quartzCompatibleStr = '';
var num;
str.split('').forEach(function (chr) {

num = parseInt(chr);
num = parseInt(chr);

// char is an actual number
if (!isNaN(num)) {
// number is in allowed range
if (num >=0 && num <=7) {
quartzCompatibleStr += num + 1;
} else {
// otherwise default to 1, beginning of the week
quartzCompatibleStr = 1;
}
} else {
quartzCompatibleStr += chr;
}
// char is an actual number
if (!isNaN(num)) {
// number is in allowed range
if (num >= 0 && num <= 7) {
quartzCompatibleStr += num + 1;
} else {
// otherwise default to 1, beginning of the week
quartzCompatibleStr = 1;
}
} else {
quartzCompatibleStr += chr;
}



});
});

return quartzCompatibleStr;
return quartzCompatibleStr;
}

/**
Expand All @@ -105,69 +105,74 @@ function advanceNumber(str) {
*/
function parseCronSyntax(crontabEntry) {

var quartzEntry = [];
var quartzEntry = [];

// first we initialize the seconds to 0 by default because linux CRON entries do not include a seconds definition
quartzEntry.push('0');

// first we initialize the seconds to 0 by default because linux CRON entries do not include a seconds definition
quartzEntry.push('0');
// quartz scheduler can't handle an OR definition, and so it doesn't support both DOM and DOW fields to be defined
// for this reason we need to shift one of them to be the value or * and the other to be ?
var toggleQuartzCompat = false;

// quartz scheduler can't handle an OR definition, and so it doesn't support both DOM and DOW fields to be defined
// for this reason we need to shift one of them to be the value or * and the other to be ?
var toggleQuartzCompat = false;
// if the second if passed, it will remove keeping only the 5 elements
if (crontabEntry.length === 6) {
crontabEntry.shift();
}

crontabEntry.forEach(function(item, index, array) {
crontabEntry.forEach(function (item, index, array) {


// index 0 = minutes
// index 1 = hours
// these cron definitions should be compatible with quartz so we push them as is
if (index === 0 || index === 1) {
quartzEntry.push(item);
}
// index 0 = minutes
// index 1 = hours
// these cron definitions should be compatible with quartz so we push them as is
if (index === 0 || index === 1) {
quartzEntry.push(item);
}

// index 2 = DOM = Day of Month
if (index === 2) {
if (item !== '?') {
toggleQuartzCompat = true;
}
// index 2 = DOM = Day of Month
if (index === 2) {
if (item !== '?') {
toggleQuartzCompat = true;
}

if (item === '*') {
toggleQuartzCompat = false;
item = '?';
}

quartzEntry.push(item);
}
if (item === '*') {
toggleQuartzCompat = false;
item = '?';
}

// index 3 = Month
if (index === 3) {
quartzEntry.push(item);
}
quartzEntry.push(item);
}

// index 4 = DOW = Day of Week
if (index === 4) {
// index 3 = Month
if (index === 3) {
quartzEntry.push(item);
}

// day of week needs another adjustments - it is specified as 1-7 in quartz but 0-6 in crontab
var itemAbbreviated = advanceNumber(item);
// index 4 = DOW = Day of Week
if (index === 4) {

if (toggleQuartzCompat === true) {
quartzEntry.push('?');
} else {
quartzEntry.push(itemAbbreviated);
}
}
// day of week needs another adjustments - it is specified as 1-7 in quartz but 0-6 in crontab
var itemAbbreviated = advanceNumber(item);

// beyond index 4 we don't care and exit the loop
if (index >= 5) {
return true;
}
if (toggleQuartzCompat === true) {
quartzEntry.push('?');
} else {
quartzEntry.push(itemAbbreviated);
}
}

});
// beyond index 4 we don't care and exit the loop
if (index >= 5) {
return true;
}

});

// quartz expect a last 7th parameter for scheduling yearly recurrence so we pass * by default for all years
quartzEntry.push('*');

return quartzEntry;
// quartz expect a last 7th parameter for scheduling yearly recurrence so we pass * by default for all years
quartzEntry.push('*');

return quartzEntry;

}

Expand All @@ -182,34 +187,35 @@ function parseCronSyntax(crontabEntry) {
*/
function parseCronMagics(crontab) {

var quartzEntry = false;
var quartzEntry = false;

// @hourly
if (crontab.indexOf('@hourly') === 0) {
quartzEntry = ['0', '0', '*', '*', '*', '?', '*'];
}
// @hourly
if (crontab.indexOf('@hourly') === 0) {
quartzEntry = ['0', '0', '*', '*', '*', '?', '*'];
}

// @daily and @midnight
if (crontab.indexOf('@daily') === 0 || crontab.indexOf('@midnight') === 0) {
quartzEntry = ['0', '0', '0', '*', '*', '?', '*'];
}
// @daily and @midnight
if (crontab.indexOf('@daily') === 0 || crontab.indexOf('@midnight') === 0) {
quartzEntry = ['0', '0', '0', '*', '*', '?', '*'];
}

// @weekly
if (crontab.indexOf('@weekly') === 0) {
quartzEntry = ['0', '0', '0', '?', '*', '1', '*'];
}
// @weekly
if (crontab.indexOf('@weekly') === 0) {
quartzEntry = ['0', '0', '0', '?', '*', '1', '*'];
}

// @monthly
if (crontab.indexOf('@monthly') === 0) {
quartzEntry = ['0', '0', '0', '1', '*', '?', '*'];
}
// @monthly
if (crontab.indexOf('@monthly') === 0) {
quartzEntry = ['0', '0', '0', '1', '*', '?', '*'];
}

// @yearly and @annually
if (crontab.indexOf('@yearly') === 0 || crontab.indexOf('@annually') === 0) {
quartzEntry = ['0', '0', '0', '1', '1', '?', '*'];
}
// @yearly and @annually
if (crontab.indexOf('@yearly') === 0 || crontab.indexOf('@annually') === 0) {
quartzEntry = ['0', '0', '0', '1', '1', '?', '*'];
}

return quartzEntry || false;
return quartzEntry || false;
}

module.exports = C2Q;

57 changes: 57 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,63 @@ describe('CRON to Quartz - Test Suite', () => {
}
)

test(
'CRON if the seconds are passed, remove and consider only the 5 character',
() => {

var quartz = C2Q.getQuartz('0 1 1 * * 1-3')

expect(quartz).toBeInstanceOf(Array)
expect(quartz).toHaveLength(1)

var quartzConfigArray = quartz.pop()
expect(quartzConfigArray).toBeInstanceOf(Array)
expect(quartzConfigArray).toHaveLength(7)

var quartzConfigStr = quartzConfigArray.join(' ')
expect(quartzConfigStr).toBe('0 1 1 ? * 2-4 *')

}
)

test(
'CRON checking if the start of the week default is 1',
() => {

var quartz = C2Q.getQuartz('1 1 * * 1-8')

expect(quartz).toBeInstanceOf(Array)
expect(quartz).toHaveLength(1)

var quartzConfigArray = quartz.pop()
expect(quartzConfigArray).toBeInstanceOf(Array)
expect(quartzConfigArray).toHaveLength(7)

var quartzConfigStr = quartzConfigArray.join(' ')
expect(quartzConfigStr).toBe('0 1 1 ? * 1 *')

}
)

test(
'CRON if the days of months are passed with an interrogation',
() => {

var quartz = C2Q.getQuartz('1 1 ? * *')

expect(quartz).toBeInstanceOf(Array)
expect(quartz).toHaveLength(1)

var quartzConfigArray = quartz.pop()
expect(quartzConfigArray).toBeInstanceOf(Array)
expect(quartzConfigArray).toHaveLength(7)

var quartzConfigStr = quartzConfigArray.join(' ')
expect(quartzConfigStr).toBe('0 1 1 ? * * *')

}
)

})

})

0 comments on commit fdcb3ce

Please sign in to comment.