UNPKG

cron-quartz-ts

Version:

A library that generates quartz corresponding cron expressions

250 lines (249 loc) 11.1 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatYears = exports.formatMonths = exports.formatDays = exports.formatHours = exports.formatMinutes = exports.formatSeconds = void 0; var constants_1 = require("./constants"); var validate_1 = require("./validate"); var splice_into_position_1 = require("./splice_into_position"); var defaultOptions = { type: 'every', list: [], cron: '* * * ? * * *', }; var defaultCron = '* * * ? * * *'; var formatSeconds = function (options) { var _a = __assign(__assign({}, defaultOptions), options), type = _a.type, list = _a.list, cron = _a.cron; switch (type) { case 'every': return (0, splice_into_position_1.spliceIntoPosition)(cron, 0, '*'); case 'start': return (0, validate_1.start)([ { min: 0, max: 59 }, { min: 1, max: 60 }, ], cron, 0, list); case 'specific': return (0, validate_1.specific)({ min: 0, max: 59 }, cron, 0, list); case 'between': return (0, validate_1.between)({ min: 0, max: 59 }, cron, 0, list); default: return defaultCron; } }; exports.formatSeconds = formatSeconds; var formatMinutes = function (options) { var _a = __assign(__assign({}, defaultOptions), options), type = _a.type, list = _a.list, cron = _a.cron; switch (type) { case 'every': return (0, splice_into_position_1.spliceIntoPosition)(cron, 1, '*'); case 'start': return (0, validate_1.start)([ { min: 0, max: 59 }, { min: 1, max: 60 }, ], cron, 1, list); case 'specific': return (0, validate_1.specific)({ min: 0, max: 59 }, cron, 1, list); case 'between': return (0, validate_1.between)({ min: 0, max: 59 }, cron, 1, list); default: return defaultCron; } }; exports.formatMinutes = formatMinutes; var formatHours = function (options) { var _a = __assign(__assign({}, defaultOptions), options), type = _a.type, list = _a.list, cron = _a.cron; switch (type) { case 'every': return (0, splice_into_position_1.spliceIntoPosition)(cron, 2, '*'); case 'start': return (0, validate_1.start)([ { min: 0, max: 23 }, { min: 1, max: 24 }, ], cron, 2, list); case 'specific': return (0, validate_1.specific)({ min: 0, max: 23 }, cron, 2, list); case 'between': return (0, validate_1.between)({ min: 0, max: 23 }, cron, 2, list); default: return defaultCron; } }; exports.formatHours = formatHours; var formatDays = function (options) { var _a = __assign(__assign({}, defaultOptions), options), type = _a.type, list = _a.list, cron = _a.cron; switch (type) { case 'every': return (0, splice_into_position_1.spliceIntoPosition)((0, splice_into_position_1.spliceIntoPosition)(cron, 3, '?'), 5, '*'); case 'start': if (list && (0, validate_1.isArray)(list) && list.length) { if (list.length > 2) { throw Error(constants_1.MORE_THAN_TWO); } list.forEach(function (item) { return (0, validate_1.isInRange)(item, 1, 7); }); return (0, splice_into_position_1.spliceIntoPosition)((0, splice_into_position_1.spliceIntoPosition)(cron, 3, '?'), 5, list.join('/')); } else { throw Error(constants_1.IS_EMPTY); } case 'start_on_month': if (list && (0, validate_1.isArray)(list) && list.length) { if (list.length > 2) { throw Error(constants_1.MORE_THAN_TWO); } list.forEach(function (item) { return (0, validate_1.isInRange)(item, 1, 31); }); return (0, splice_into_position_1.spliceIntoPosition)((0, splice_into_position_1.spliceIntoPosition)(cron, 5, '?'), 3, list.join('/')); } else { throw Error(constants_1.IS_EMPTY); } case 'specific': if (list && (0, validate_1.isArray)(list)) { if (list.length) { list.forEach(function (item) { return (0, validate_1.isInRange)(item, 1, 7); }); } var char = list.length ? list.join(',') : 1; return (0, splice_into_position_1.spliceIntoPosition)((0, splice_into_position_1.spliceIntoPosition)(cron, 3, '?'), 5, char); } case 'specific_on_month': if (list && (0, validate_1.isArray)(list)) { if (list.length) { list.forEach(function (item) { return (0, validate_1.isInRange)(item, 1, 31); }); } var char = list.length ? list.join(',') : 1; return (0, splice_into_position_1.spliceIntoPosition)((0, splice_into_position_1.spliceIntoPosition)(cron, 5, '?'), 3, char); } case 'last_day_on_month': return (0, splice_into_position_1.spliceIntoPosition)((0, splice_into_position_1.spliceIntoPosition)(cron, 5, '?'), 3, 'L'); case 'last_week_on_month': return (0, splice_into_position_1.spliceIntoPosition)((0, splice_into_position_1.spliceIntoPosition)(cron, 5, '?'), 3, 'LW'); case 'last_select_day_on_month': if (list && (0, validate_1.isArray)(list) && list.length) { if (list.length === 1) { (0, validate_1.isInRange)(list[0], 1, 7); return (0, splice_into_position_1.spliceIntoPosition)((0, splice_into_position_1.spliceIntoPosition)(cron, 3, '?'), 5, "".concat(list.pop(), "L")); } else { throw Error(constants_1.MORE_THAN_ONE); } } else { throw Error(constants_1.IS_EMPTY); } case 'before': if (list && (0, validate_1.isArray)(list) && list.length) { if (list.length === 1) { (0, validate_1.isInRange)(list[0], 1, 31); return (0, splice_into_position_1.spliceIntoPosition)((0, splice_into_position_1.spliceIntoPosition)(cron, 5, '?'), 3, "L-".concat(list.pop())); } else { throw Error(constants_1.MORE_THAN_ONE); } } else { throw Error(constants_1.IS_EMPTY); } case 'near': if (list && (0, validate_1.isArray)(list) && list.length) { if (list.length === 1) { (0, validate_1.isInRange)(list[0], 1, 31); return (0, splice_into_position_1.spliceIntoPosition)((0, splice_into_position_1.spliceIntoPosition)(cron, 5, '?'), 3, "".concat(list.pop(), "W")); } else { throw Error(constants_1.MORE_THAN_ONE); } } else { throw Error(constants_1.IS_EMPTY); } case 'day_on_every_month': if (list && (0, validate_1.isArray)(list) && list.length) { if (list.length > 2) { throw Error(constants_1.MORE_THAN_TWO); } (0, validate_1.isInRange)(list[0], 1, 7); (0, validate_1.isInRange)(list[1], 1, 5); return (0, splice_into_position_1.spliceIntoPosition)((0, splice_into_position_1.spliceIntoPosition)(cron, 3, '?'), 5, list.join('#')); } else { throw Error(constants_1.IS_EMPTY); } case 'between': if (list && (0, validate_1.isArray)(list)) { if (list.length > 2) { throw Error(constants_1.MORE_THAN_TWO); } if (list.length) { list.forEach(function (item) { return (0, validate_1.isInRange)(item, 1, 7); }); } return (0, splice_into_position_1.spliceIntoPosition)((0, splice_into_position_1.spliceIntoPosition)(cron, 3, '?'), 5, list.join('-')); } default: return defaultCron; } }; exports.formatDays = formatDays; var formatMonths = function (options) { var _a = __assign(__assign({}, defaultOptions), options), type = _a.type, list = _a.list, cron = _a.cron; switch (type) { case 'every': return (0, splice_into_position_1.spliceIntoPosition)(cron, 4, '*'); case 'start': return (0, validate_1.start)([{ min: 1, max: 12 }], cron, 4, list, true); case 'specific': return (0, validate_1.specific)({ min: 1, max: 12 }, cron, 4, list, 1); case 'between': return (0, validate_1.between)({ min: 1, max: 12 }, cron, 4, list); default: return defaultCron; } }; exports.formatMonths = formatMonths; var formatYears = function (options) { var _a = __assign(__assign({}, defaultOptions), options), type = _a.type, list = _a.list, cron = _a.cron; switch (type) { case 'every': return (0, splice_into_position_1.spliceIntoPosition)(cron, 6, '*'); case 'start': if (list && (0, validate_1.isArray)(list) && list.length) { if (list.length > 2) { throw Error(constants_1.MORE_THAN_TWO); } return (0, splice_into_position_1.spliceIntoPosition)(cron, 6, list.join('/')); } else { throw Error(constants_1.IS_EMPTY); } case 'specific': if (list && (0, validate_1.isArray)(list) && list.length) { return (0, splice_into_position_1.spliceIntoPosition)(cron, 6, list.join(',')); } else { throw Error(constants_1.IS_EMPTY); } case 'between': if (list && (0, validate_1.isArray)(list) && list.length) { if (list.length > 2) { throw Error(constants_1.MORE_THAN_TWO); } if (list[0] > list[1]) { throw Error('start must come before end following normal calendar sequence.'); } return (0, splice_into_position_1.spliceIntoPosition)(cron, 6, list.join('-')); } else { throw Error(constants_1.IS_EMPTY); } default: return defaultCron; } }; exports.formatYears = formatYears;