@jbouduin/holidays-lib
Version:
Get World-Wide Holidays
80 lines (79 loc) • 3.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CalendarHelper = void 0;
var configuration_1 = require("../../configuration");
var configuration_2 = require("../../configuration");
var configuration_3 = require("../../configuration");
var CalendarHelper = /** @class */ (function () {
//#region Constructor & C°
// eslint-disable-next-line @typescript-eslint/no-empty-function
function CalendarHelper() {
}
//#endregion
//#region ICalendar interface methods
CalendarHelper.prototype.addDays = function (date, days) {
return new Date(date.getTime() + (days * 1000 * 60 * 60 * 24));
};
CalendarHelper.prototype.calculateFixedDate = function (fix, year) {
return new Date(Date.UTC(year, fix.month, fix.day));
};
CalendarHelper.prototype.calculateFixedWeekday = function (fix, year) {
if (fix.which === configuration_2.Which.LAST) {
var lastDayOfMonth = new Date(Date.UTC(year, fix.month + 1, 0));
return this.addDays(lastDayOfMonth, fix.weekday - lastDayOfMonth.getDay());
}
else {
var firstDayOfMonth = new Date(Date.UTC(year, fix.month, 1));
var dayOfFirstDayOfMonth = firstDayOfMonth.getDay();
return this.addDays(firstDayOfMonth, (dayOfFirstDayOfMonth > fix.weekday ?
fix.weekday - dayOfFirstDayOfMonth + 7 :
fix.weekday - dayOfFirstDayOfMonth) +
fix.which * 7);
}
};
CalendarHelper.prototype.generalizedModulo = function (a, b) {
// generalized modulo function (m mod n) - also valid for negative values of m
return ((a % b) + b) % b;
};
CalendarHelper.prototype.occurs = function (holiday, year) {
var result = (holiday.validFrom === configuration_3.BaseHoliday.undefinedValidFrom || holiday.validFrom <= year) &&
(holiday.validTo === configuration_3.BaseHoliday.undefinedValidTo || holiday.validTo >= year) &&
this.isValidForCyle(holiday, year);
return result;
};
//#endregion
//#region Private methods
CalendarHelper.prototype.isValidForCyle = function (holiday, year) {
var cycleYears = 1;
switch (holiday.cycle) {
case configuration_1.Cycle.EVERY_YEAR: {
return true;
}
case configuration_1.Cycle.ODD_YEARS: {
return year % 2 !== 0;
}
case configuration_1.Cycle.EVEN_YEARS: {
return year % 2 === 0;
}
case configuration_1.Cycle.TWO_YEARS: {
cycleYears = 2;
break;
}
case configuration_1.Cycle.FOUR_YEARS: {
cycleYears = 4;
break;
}
case configuration_1.Cycle.FIVE_YEARS: {
cycleYears = 5;
break;
}
case configuration_1.Cycle.SIX_YEARS: {
cycleYears = 6;
break;
}
}
return (year - holiday.validFrom) % cycleYears === 0;
};
return CalendarHelper;
}());
exports.CalendarHelper = CalendarHelper;