@jbouduin/holidays-lib
Version:
Get World-Wide Holidays
101 lines (100 loc) • 4.58 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.RelativeBetweenFixedFactory = void 0;
var errors_1 = require("../errors");
var holidays_1 = require("../holidays");
var types_1 = require("../types");
var types_2 = require("../types");
var base_factory_1 = require("./base-factory");
var RelativeBetweenFixedFactory = /** @class */ (function (_super) {
__extends(RelativeBetweenFixedFactory, _super);
//#endregion
//#region Constructor & C°
function RelativeBetweenFixedFactory() {
return _super.call(this) || this;
}
//#endregion
//#region Abstract methods implementation
RelativeBetweenFixedFactory.prototype.createHoliday = function (key, category, cycle, validFrom, validTo) {
return new holidays_1.RelativeHoliday(types_1.HolidayType.RELATIVE_BETWEEN_FIXED, key, category, cycle, validFrom, validTo, this.relation, this.fix);
};
RelativeBetweenFixedFactory.prototype.extractData = function (obj) {
var canContinue = true;
if (!obj.fix) {
this.addError(errors_1.ErrorKey.RELATIVE_FIX_MISSING);
canContinue = false;
}
else {
if (!obj.fix.from && !obj.fix.to) {
this.addError(errors_1.ErrorKey.RELATIVE_FIX_EMPTY);
canContinue = false;
}
else {
if (!obj.fix.from) {
this.addError(errors_1.ErrorKey.RELATIVE_BETWEEN_FIXED_FROM_MISSING);
canContinue = false;
}
if (!obj.fix.to) {
this.addError(errors_1.ErrorKey.RELATIVE_BETWEEN_FIXED_TO_MISSING);
canContinue = false;
}
}
}
if (!obj.relation) {
this.addError(errors_1.ErrorKey.RELATIVE_RELATION_MISSING);
}
else {
if (!obj.relation.weekday && obj.relation.weekday !== '') {
this.addError(errors_1.ErrorKey.RELATIVE_RELATION_EMPTY);
}
else {
var weekday = types_2.Weekday[obj.relation.weekday];
if (weekday === undefined) {
this.addError(errors_1.ErrorKey.RELATION_WEEKDAY_INVALID, obj.weekday);
}
else {
this.relation = { weekday: weekday };
}
}
}
if (canContinue) {
this.fix = {
from: this.dataExtractor.extractFixedDate(obj.fix.from),
to: this.dataExtractor.extractFixedDate(obj.fix.to)
};
if (this.fix.from.day !== 0 && this.fix.to.day !== 0) {
var fromDate = new Date(1986, this.fix.from.month, this.fix.from.day);
var toDate = new Date(1986, this.fix.to.month, this.fix.to.day);
if (!isNaN(fromDate.getTime()) && !isNaN(toDate.getTime())) {
var timeSpan = toDate.getTime() - fromDate.getTime();
if (timeSpan < 0) {
this.addError(errors_1.ErrorKey.RELATIVE_BETWEEN_FIXED_FIX_TO_BEFORE_FROM, obj.fix);
}
else if (timeSpan !== 6 * 24 * 60 * 60 * 1000) { // 518400000 is a week
this.addError(errors_1.ErrorKey.RELATIVE_BETWEEN_FIXED_SPAN_INVALID, obj.fix);
}
}
}
}
};
RelativeBetweenFixedFactory.prototype.extractKey = function (obj) {
return this.dataExtractor.extractStringKey(obj);
};
return RelativeBetweenFixedFactory;
}(base_factory_1.BaseFactory));
exports.RelativeBetweenFixedFactory = RelativeBetweenFixedFactory;