react-application-core
Version:
A react-based application core for the business applications.
602 lines • 22.1 kB
JavaScript
"use strict";
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CronEntity = exports.fromCronExpression = void 0;
var R = require("ramda");
var definition_1 = require("../definition");
var cond_1 = require("./cond");
var di_services_1 = require("../di/di.services");
var join_1 = require("./join");
var object_1 = require("./object");
var sort_1 = require("./sort");
var type_1 = require("./type");
// Examples:
// 1) * * * * * *
// Each minute
//
// 2) */10 9-17 1 * *
// At every 10th minute past every hour from 9 through 17 on day-of-month 1
//
// 3) */10,*/2,1-2 9-17 1 * *
// At every 10th minute, every 2nd minute, and every minute from 1 through 2 past
// every hour from 9 through 17 on day-of-month 1
//
// 4) 1-59/2 * * * *
// At every 2nd minute from 1 through 59 ("Выполняется по нечётным минутам")
//
// 5) * * * * 2L,4L
// Every minute of the last Tuesday and Thursday of the month.
//
// 6) 0 0 26,27,31 * 2L,4L
// At 00:00 on day-of-month 26, 27, and 31 if are the last Tuesday and Thursday of every month (intersection of dates).
// For example:
// "2019-12-26T00:00:00+00:00"
// "2019-12-31T00:00:00+00:00"
// "2020-02-27T00:00:00+00:00"
// "2020-03-26T00:00:00+00:00"
// "2020-03-31T00:00:00+00:00"
//
// 7) 0 0 L * *
// The last day of the month.
//
/**
* @stable [15.11.2019]
*
* * * * * * * *
* | | | | | |
* | | | | | +-- Year (range: 1900-3000)
* | | | | +---- Day of the Week (range: 0-6 (Sunday to Saturday))
* | | | +------ Month of the Year (range: 1-12)
* | | +-------- Day of the Month (range: 1-31)
* | +---------- Hour (range: 0-23)
* +------------ Minute (range: 0-59)
*
* Last days:
* Also, one other character is sometimes available to the DoM and DoW parameters: L. It is used differently for each
* parameter, however.
* - For DoM, a lone “L” means the last day of the month.
* - For DoW, the “L” is added to the day number to indicate the last day of the month. For example, “1L” would indicate
* the last Monday of the month.
*
* @param {string} expression
* @param {Date} from
* @param {Date} to
* @returns {ICronEntity}
*/
exports.fromCronExpression = function (expression, from, to) {
return CronEntity.newInstance()
.withDateFrom(from)
.withDateTo(to)
.fromExpression(expression)
.asPlainObject();
};
/**
* @stable [15.12.2019]
* @param {number[]} values
* @param {CronPartTypesEnum} partType
* @param {boolean} returnNeverExecutablePeriodAsEmptyValue
* @returns {string}
*/
var toCronPart = function (values, partType, returnNeverExecutablePeriodAsEmptyValue) {
if (returnNeverExecutablePeriodAsEmptyValue === void 0) { returnNeverExecutablePeriodAsEmptyValue = false; }
if (!object_1.ObjectUtils.isObjectNotEmpty(values)) {
return returnNeverExecutablePeriodAsEmptyValue
? definition_1.CRON_NEVER_EXECUTABLE_SYMBOL
: definition_1.CRON_ALL_VALUES_SYMBOL; // We can't set never executable period
}
var areAllValuesSelected0 = areAllCronValuesSelected(values, partType);
var result = areAllValuesSelected0
? [definition_1.CRON_ALL_VALUES_SYMBOL]
: values;
if (!areAllValuesSelected0) {
switch (partType) {
case definition_1.CronPartTypesEnum.DAY_OF_MONTH:
if (values.includes(definition_1.CRON_LAST_DAY_OF_MONTH)) {
var filteredValues = values.filter(function (v) { return v !== definition_1.CRON_LAST_DAY_OF_MONTH; });
if (R.isEmpty(filteredValues)) {
result = [definition_1.CRON_LAST_DAY_SYMBOL];
}
else {
result = __spreadArrays(filteredValues, [definition_1.CRON_LAST_DAY_SYMBOL]);
}
}
break;
case definition_1.CronPartTypesEnum.DAY_OF_WEEK:
var lastDays = R
.sort(sort_1.SortUtils.VALUE_DESC_SORTER, values.filter(function (v) { return v < 0; }))
.map(function (v) { return "" + toCronLastDayOfWeek(v) + definition_1.CRON_LAST_DAY_SYMBOL; });
var positiveDays = values.filter(function (v) { return v >= 0; });
result = __spreadArrays(positiveDays, lastDays);
break;
}
}
return join_1.JoinUtils.join(tryingToPack(result), ',');
};
/**
* @stable [14.12.2019]
* @param {number} v
* @returns {number}
*/
var toCronLastDayOfWeek = function (v) { return v * -1 - 1; };
/**
* @stable [15.11.2019]
* @param {string} cronPart
* @param {CronPartTypesEnum} partType
* @param {Date} from
* @param {Date} to
* @returns {number[]}
*/
var fromCronPart = function (cronPart, partType, from, to) {
if (!object_1.ObjectUtils.isObjectNotEmpty(cronPart)) {
return [];
}
var nc = di_services_1.DiServices.numberConverter();
var availableRanges = definition_1.CRON_PART_TYPE_RANGES[partType];
var values = [];
cronPart.replace(/ /g, '').split(',').forEach(function (item) {
var repeatingParts = item.split('/');
var ranges = repeatingParts[0].split('-');
if (ranges[0] === definition_1.CRON_NEVER_EXECUTABLE_SYMBOL) {
return;
}
var period = cond_1.ConditionUtils.orNull(repeatingParts.length > 1, function () { return nc.number(repeatingParts[1]); });
/**/
var lastDaysParts = item.split(definition_1.CRON_LAST_DAY_SYMBOL);
var lastDay = cond_1.ConditionUtils.orNull(lastDaysParts.length > 1, function () {
var lastDayPart = lastDaysParts[0];
return R.isEmpty(lastDayPart)
? definition_1.CRON_LAST_DAY_OF_MONTH // For LDoM
: toCronLastDayOfWeek(nc.number(lastDayPart)); // For LDoW
});
/**/
if (!R.isNil(lastDay)) {
values.push(lastDay);
}
else {
var isFullPeriod = ranges[0] === definition_1.CRON_ALL_VALUES_SYMBOL;
var start = isFullPeriod ? availableRanges[0] : nc.number(ranges[0]);
var end = isFullPeriod ? availableRanges[1] : (ranges.length > 1 ? nc.number(ranges[1]) : start);
var isPeriodPresent = type_1.TypeUtils.isNumber(period);
var j = start;
for (var i = start; i <= end; i++) {
if (isPeriodPresent) {
if (i === start || i === j) {
values.push(i);
j += period;
}
}
else {
values.push(i);
}
}
}
});
return Array.from(new Set(values)).sort(sort_1.SortUtils.NUMBER_COMPARATOR);
};
/**
* @stable [14.12.2019]
* @param {number[]} parts
* @param {CronPartTypesEnum} partType
* @returns {boolean}
*/
var areAllCronValuesSelected = function (parts, partType) {
var availableValues = definition_1.CRON_PART_TYPE_RANGES_VALUES[partType];
return R.intersection(parts, availableValues).length === availableValues.length;
};
/**
* @stable [14.12.2019]
* @param {StringNumberT[]} values
* @returns {StringNumberT[]}
*/
var tryingToPack = function (values) {
var ranges = [];
var range = [];
values.forEach(function (value) {
if (type_1.TypeUtils.isNumber(value)) {
if (R.isEmpty(range)) {
range = [value];
ranges.push(range);
}
else {
if (R.equals(R.last(range) + 1, value)) {
range.push(value);
}
else {
range = [value];
ranges.push(range);
}
}
}
else {
ranges.push([value]);
range = [];
}
});
var result = [];
ranges.forEach(function (range0) {
if (range0.length <= 2) {
result = __spreadArrays(result, range0);
}
else {
result = __spreadArrays(result, [
range0[0] + "-" + R.last(range0)
]);
}
});
return result;
};
/**
* @stable [14.12.2019]
*/
var CronEntity = /** @class */ (function () {
function CronEntity() {
this.daysOfMonths = definition_1.CRON_ALL_DAYS_OF_MONTH_VALUES;
this.daysOfWeeks = definition_1.CRON_ALL_DAYS_OF_WEEK_VALUES;
this.hours = definition_1.CRON_ALL_HOURS_VALUES;
this.minutes = definition_1.CRON_ALL_MINUTES_VALUES;
this.months = definition_1.CRON_ALL_MONTHS_OF_YEAR_VALUES;
this.years = definition_1.CRON_ALL_YEARS_VALUES;
this.allDaysOfMonth = true;
this.allDaysOfWeek = true;
this.allHours = true;
this.allMinutes = true;
this.allMonths = true;
this.allYears = true;
}
CronEntity.newInstance = function () {
return new CronEntity();
};
/**
* @stable [15.12.2019]
* @param {Date} to
* @returns {CronEntity}
*/
CronEntity.prototype.withDateTo = function (to) {
this.to = to;
return this;
};
/**
* @stable [15.12.2019]
* @param {Date} from
* @returns {CronEntity}
*/
CronEntity.prototype.withDateFrom = function (from) {
this.from = from;
return this;
};
/**
* @stable [15.12.2019]
* @param {string} expression
* @returns {CronEntity}
*/
CronEntity.prototype.fromExpression = function (expression) {
var _a, _b, _c, _d, _e;
if (!object_1.ObjectUtils.isObjectNotEmpty(expression)) {
return this
.withMinutes()
.withHours()
.withMonths()
.withYears()
.withDaysOfMonths()
.withDaysOfWeeks();
}
this.expression = expression;
var parts = expression.replace(/ {2,}/g, ' ').split(' ');
return (_a = (_b = (_c = (_d = (_e = this
.withMinutes.apply(this, fromCronPart(parts[0], definition_1.CronPartTypesEnum.MINUTE, this.from, this.to)))
.withHours.apply(_e, fromCronPart(parts[1], definition_1.CronPartTypesEnum.HOUR, this.from, this.to)))
.withMonths.apply(_d, fromCronPart(parts[3], definition_1.CronPartTypesEnum.MONTH_OF_YEAR, this.from, this.to)))
.withYears.apply(_c, fromCronPart(parts[5], definition_1.CronPartTypesEnum.YEAR, this.from, this.to)))
.withDaysOfMonths.apply(_b, fromCronPart(parts[2], definition_1.CronPartTypesEnum.DAY_OF_MONTH, this.from, this.to)))
.withDaysOfWeeks.apply(_a, fromCronPart(parts[4], definition_1.CronPartTypesEnum.DAY_OF_WEEK, this.from, this.to));
};
/**
* @stable [15.12.2019]
* @param {boolean} returnNeverExecutablePeriodAsEmptyValue
* @returns {string}
*/
CronEntity.prototype.toExpression = function (returnNeverExecutablePeriodAsEmptyValue) {
return join_1.JoinUtils.join([
toCronPart(this.minutes, definition_1.CronPartTypesEnum.MINUTE, returnNeverExecutablePeriodAsEmptyValue),
toCronPart(this.hours, definition_1.CronPartTypesEnum.HOUR, returnNeverExecutablePeriodAsEmptyValue),
toCronPart(this.daysOfMonths, definition_1.CronPartTypesEnum.DAY_OF_MONTH, returnNeverExecutablePeriodAsEmptyValue),
toCronPart(this.months, definition_1.CronPartTypesEnum.MONTH_OF_YEAR, returnNeverExecutablePeriodAsEmptyValue),
toCronPart(this.daysOfWeeks, definition_1.CronPartTypesEnum.DAY_OF_WEEK, returnNeverExecutablePeriodAsEmptyValue),
toCronPart(this.years, definition_1.CronPartTypesEnum.YEAR, returnNeverExecutablePeriodAsEmptyValue)
], ' ');
};
/**
* @stable [14.12.2019]
* @returns {ICronPlainEntity}
*/
CronEntity.prototype.asPlainObject = function () {
return {
allDaysOfMonth: this.allDaysOfMonth,
allDaysOfWeek: this.allDaysOfWeek,
allHours: this.allHours,
allMinutes: this.allMinutes,
allMonths: this.allMonths,
allYears: this.allYears,
daysOfMonths: this.daysOfMonths,
daysOfWeeks: this.daysOfWeeks,
hours: this.hours,
minutes: this.minutes,
months: this.months,
years: this.years,
};
};
/**
* @stable [19.12.2019]
* @returns {CronPeriodsEnum}
*/
CronEntity.prototype.asPeriodType = function () {
if (this.minutes.length === 1 && this.hours.length === 1) {
if (this.daysOfMonths.length === 1 && this.months.length === 1) {
if (this.years.length === 1) {
return definition_1.CronPeriodsEnum.NO_REPETITIONS;
}
return definition_1.CronPeriodsEnum.YEARLY;
}
if (this.allYears && this.allMonths) {
if (!this.allDaysOfMonth) {
return definition_1.CronPeriodsEnum.MONTHLY;
}
else if (!this.allDaysOfWeek) {
return definition_1.CronPeriodsEnum.WEEKLY;
}
return definition_1.CronPeriodsEnum.DAILY;
}
}
return definition_1.CronPeriodsEnum.COMPLEX_RULE;
};
/**
* @stable [14.12.2019]
* @param {number} values
* @returns {CronEntity}
*/
CronEntity.prototype.withMonths = function () {
var values = [];
for (var _i = 0; _i < arguments.length; _i++) {
values[_i] = arguments[_i];
}
this.months = this.normalize(values);
this.refreshAllMonths();
return this;
};
/**
* @stable [14.12.2019]
* @param {number} values
* @returns {CronEntity}
*/
CronEntity.prototype.withYears = function () {
var values = [];
for (var _i = 0; _i < arguments.length; _i++) {
values[_i] = arguments[_i];
}
this.years = this.normalize(values);
this.refreshAllYears();
return this;
};
/**
* @stable [14.12.2019]
* @param {number} values
* @returns {CronEntity}
*/
CronEntity.prototype.withMinutes = function () {
var values = [];
for (var _i = 0; _i < arguments.length; _i++) {
values[_i] = arguments[_i];
}
this.minutes = this.normalize(values);
this.refreshAllMinutes();
return this;
};
/**
* @stable [14.12.2019]
* @param {number} values
* @returns {CronEntity}
*/
CronEntity.prototype.withHours = function () {
var values = [];
for (var _i = 0; _i < arguments.length; _i++) {
values[_i] = arguments[_i];
}
this.hours = this.normalize(values);
this.refreshAllHours();
return this;
};
/**
* @stable [14.12.2019]
* @param {number} values
* @returns {CronEntity}
*/
CronEntity.prototype.withDaysOfWeeks = function () {
var values = [];
for (var _i = 0; _i < arguments.length; _i++) {
values[_i] = arguments[_i];
}
this.daysOfWeeks = this.normalize(values);
this.refreshAllDaysOfWeek();
return this;
};
/**
* @stable [14.12.2019]
* @param {number} values
* @returns {CronEntity}
*/
CronEntity.prototype.withLastDaysOfWeeks = function () {
var values = [];
for (var _i = 0; _i < arguments.length; _i++) {
values[_i] = arguments[_i];
}
this.daysOfWeeks = this.normalize(values.map(function (v) { return toCronLastDayOfWeek(v); }));
this.refreshAllDaysOfWeek();
return this;
};
/**
* @stable [14.12.2019]
* @param {number} values
* @returns {CronEntity}
*/
CronEntity.prototype.withDaysOfMonths = function () {
var values = [];
for (var _i = 0; _i < arguments.length; _i++) {
values[_i] = arguments[_i];
}
this.daysOfMonths = this.normalize(values);
this.refreshAllDaysOfMonth();
return this;
};
/**
* @stable [14.12.2019]
* @param {number[]} extraValues
* @returns {CronEntity}
*/
CronEntity.prototype.withLastDayOfMonth = function () {
var extraValues = [];
for (var _i = 0; _i < arguments.length; _i++) {
extraValues[_i] = arguments[_i];
}
this.daysOfMonths = this.normalize(__spreadArrays([
definition_1.CRON_LAST_DAY_OF_MONTH
], extraValues));
this.refreshAllDaysOfMonth();
return this;
};
/**
* @stable [15.12.2019]
* @param {number} value
* @returns {boolean}
*/
CronEntity.prototype.hasDayOfMonth = function (value) {
return this.daysOfMonths.includes(value);
};
/**
* @stable [15.12.2019]
* @param {number} value
* @returns {boolean}
*/
CronEntity.prototype.hasDayOfWeek = function (value) {
return this.daysOfWeeks.includes(value);
};
/**
* @stable [15.12.2019]
* @param {number} values
* @returns {CronEntity}
*/
CronEntity.prototype.addDaysOfMonths = function () {
var values = [];
for (var _i = 0; _i < arguments.length; _i++) {
values[_i] = arguments[_i];
}
this.withDaysOfMonths.apply(this, __spreadArrays(this.daysOfMonths, values));
return this;
};
/**
* @stable [15.12.2019]
* @param {number} values
* @returns {CronEntity}
*/
CronEntity.prototype.addDaysOfWeeks = function () {
var values = [];
for (var _i = 0; _i < arguments.length; _i++) {
values[_i] = arguments[_i];
}
this.withDaysOfWeeks.apply(this, __spreadArrays(this.daysOfWeeks, values));
return this;
};
/**
* @stable [15.12.2019]
* @param {number} values
* @returns {CronEntity}
*/
CronEntity.prototype.excludeDaysOfMonths = function () {
var values = [];
for (var _i = 0; _i < arguments.length; _i++) {
values[_i] = arguments[_i];
}
this.daysOfMonths = this.excludeValues.apply(this, __spreadArrays([this.daysOfMonths], values));
this.refreshAllDaysOfMonth();
return this;
};
/**
* @stable [15.12.2019]
* @param {number} values
* @returns {CronEntity}
*/
CronEntity.prototype.excludeDaysOfWeeks = function () {
var values = [];
for (var _i = 0; _i < arguments.length; _i++) {
values[_i] = arguments[_i];
}
this.daysOfWeeks = this.excludeValues.apply(this, __spreadArrays([this.daysOfWeeks], values));
this.refreshAllDaysOfWeek();
return this;
};
/**
* @stable [15.12.2019]
* @param {number[]} values
* @param {number[]} excludedValues
* @returns {number[]}
*/
CronEntity.prototype.excludeValues = function (values) {
var excludedValues = [];
for (var _i = 1; _i < arguments.length; _i++) {
excludedValues[_i - 1] = arguments[_i];
}
return this.normalize(R.filter(function (day) { return !excludedValues.includes(day); }, values));
};
/**
* @stable [14.10.2020]
* @param values
* @private
*/
CronEntity.prototype.normalize = function (values) {
return R.sort(sort_1.SortUtils.VALUE_ASC_SORTER, Array.from(new Set(values)));
};
/**
* @stable [14.12.2019]
*/
CronEntity.prototype.refreshAllDaysOfWeek = function () {
this.allDaysOfWeek = areAllCronValuesSelected(this.daysOfWeeks, definition_1.CronPartTypesEnum.DAY_OF_WEEK);
};
/**
* @stable [14.12.2019]
*/
CronEntity.prototype.refreshAllDaysOfMonth = function () {
this.allDaysOfMonth = areAllCronValuesSelected(this.daysOfMonths, definition_1.CronPartTypesEnum.DAY_OF_MONTH);
};
/**
* @stable [14.12.2019]
*/
CronEntity.prototype.refreshAllHours = function () {
this.allHours = areAllCronValuesSelected(this.hours, definition_1.CronPartTypesEnum.HOUR);
};
/**
* @stable [14.12.2019]
*/
CronEntity.prototype.refreshAllMinutes = function () {
this.allMinutes = areAllCronValuesSelected(this.minutes, definition_1.CronPartTypesEnum.MINUTE);
};
/**
* @stable [14.12.2019]
*/
CronEntity.prototype.refreshAllYears = function () {
this.allYears = areAllCronValuesSelected(this.years, definition_1.CronPartTypesEnum.YEAR);
};
/**
* @stable [14.12.2019]
*/
CronEntity.prototype.refreshAllMonths = function () {
this.allMonths = areAllCronValuesSelected(this.months, definition_1.CronPartTypesEnum.MONTH_OF_YEAR);
};
return CronEntity;
}());
exports.CronEntity = CronEntity;
//# sourceMappingURL=cron.js.map