@jbouduin/holidays-lib
Version:
Get World-Wide Holidays
77 lines (76 loc) • 3.73 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.RelativeHolidayCalculator = void 0;
var configuration_1 = require("../configuration");
var base_calculator_1 = require("./base-calculator");
var RelativeHolidayCalculator = /** @class */ (function (_super) {
__extends(RelativeHolidayCalculator, _super);
//#region Constructor
function RelativeHolidayCalculator() {
return _super.call(this) || this;
}
//#endregion
//#region Abstract method implementation
RelativeHolidayCalculator.prototype.calculateDate = function (holiday, year) {
switch (holiday.holidayType) {
case configuration_1.HolidayType.RELATIVE_BETWEEN_FIXED: {
return this.calculateRelativeBetween(holiday, year);
}
case configuration_1.HolidayType.RELATIVE_TO_DATE: {
return this.calculateRelativeToDate(holiday, year);
}
case configuration_1.HolidayType.RELATIVE_TO_WEEKDAY: {
return this.calculateRelativeToWeekday(holiday, year);
}
}
};
//#endregion
//#region Private methods
RelativeHolidayCalculator.prototype.calculateRelativeBetween = function (holiday, year) {
var from = this.calendarHelper.calculateFixedDate(holiday.fix.from, year);
// const to = this.calendarHelper.calculateFixedDate(holiday.fix.to, year);
var dayOfFrom = from.getDay();
var result = this.calendarHelper.addDays(from, (dayOfFrom > holiday.relation.weekday ?
holiday.relation.weekday - dayOfFrom + 7 :
holiday.relation.weekday - dayOfFrom));
return result;
};
RelativeHolidayCalculator.prototype.calculateRelativeToDate = function (holiday, year) {
return this.applyRelation(holiday.relation, this.calendarHelper.calculateFixedDate(holiday.fix, year));
};
RelativeHolidayCalculator.prototype.calculateRelativeToWeekday = function (holiday, year) {
return this.applyRelation(holiday.relation, this.calendarHelper.calculateFixedWeekday(holiday.fix, year));
};
RelativeHolidayCalculator.prototype.applyRelation = function (relation, date) {
var dayOfFix = date.getDay();
var diff = 0;
if (relation.when === configuration_1.When.BEFORE) {
diff = dayOfFix >= relation.weekday ?
relation.weekday - dayOfFix :
relation.weekday - dayOfFix - 7;
}
else {
diff = dayOfFix > relation.weekday ?
relation.weekday - dayOfFix + 7 :
relation.weekday - dayOfFix;
}
return this.calendarHelper.addDays(date, diff + (relation.which * 7 * relation.when));
};
return RelativeHolidayCalculator;
}(base_calculator_1.BaseCalculator));
exports.RelativeHolidayCalculator = RelativeHolidayCalculator;