@jbouduin/holidays-lib
Version:
Get World-Wide Holidays
95 lines (94 loc) • 3.85 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseFactory = void 0;
var errors_1 = require("../errors");
var holidays_1 = require("../holidays");
var types_1 = require("../types");
var types_2 = require("../types");
var data_extractor_1 = require("./data-extractor");
var factory_result_1 = require("./factory-result");
var BaseFactory = /** @class */ (function () {
//#endregion
//#region Constructor & C°
function BaseFactory() {
this.location = '';
this.dataExtractor = new data_extractor_1.DataExtractor(this.addError.bind(this));
this.errors = new Array();
this.validFrom = holidays_1.BaseHoliday.undefinedValidFrom;
this.validTo = holidays_1.BaseHoliday.undefinedValidTo;
this.cycle = types_1.Cycle.EVERY_YEAR;
this.category = types_2.Category.OFFICIAL_HOLIDAY;
}
//#endregion
//#region IBaseFactory interface methods
BaseFactory.prototype.create = function (location, obj) {
this.location = location;
var key = this.extractKey(obj);
var moves = this.dataExtractor.extractMoves(obj);
this.extractBaseHolidayData(obj);
this.extractData(obj);
if (this.errors.length === 0) {
var holiday_1 = this.createHoliday(key, this.category, this.cycle, this.validFrom, this.validTo);
moves.forEach(function (move) { return holiday_1.moves.push(move); });
return new factory_result_1.FactoryResult(holiday_1, this.errors);
}
else {
return new factory_result_1.FactoryResult(undefined, this.errors);
}
};
//#endregion
//#region Protected helper methods
BaseFactory.prototype.addError = function (key) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
this.errors.push(new errors_1.LoadError(key, this.location, args));
};
//#endregion
//#region Private helper methods
BaseFactory.prototype.extractBaseHolidayData = function (obj) {
if (obj.validFrom) {
this.validFrom = Number(obj.validFrom);
}
if (obj.validTo) {
this.validTo = Number(obj.validTo);
}
if (obj.cycle) {
this.cycle = types_1.Cycle[obj.cycle];
}
if (obj.category) {
this.category = types_2.Category[obj.category];
}
if (this.cycle === undefined) {
this.addError(errors_1.ErrorKey.HOLIDAY_CYCLE_INVALID, obj.cycle);
}
else {
switch (this.cycle) {
case types_1.Cycle.TWO_YEARS:
case types_1.Cycle.FOUR_YEARS:
case types_1.Cycle.FIVE_YEARS:
case types_1.Cycle.SIX_YEARS: {
if (this.validFrom === holidays_1.BaseHoliday.undefinedValidFrom) {
this.addError(errors_1.ErrorKey.HOLIDAY_CYCLE_REQUIRES_VALID_FROM, obj.cycle, obj.validFrom);
break;
}
}
}
}
if (this.category === undefined) {
this.addError(errors_1.ErrorKey.HOLIDAY_CATEGORY_INVALID, obj.category);
}
if (!this.validFrom) {
this.addError(errors_1.ErrorKey.VALID_FROM_INVALID, obj.validFrom);
}
if (!this.validTo) {
this.addError(errors_1.ErrorKey.VALID_TO_INVALID, obj.validTo);
}
if (this.validFrom && this.validTo && this.validFrom > this.validTo) {
this.addError(errors_1.ErrorKey.VALID_TO_BEFORE_VALID_FROM, obj.validTo, obj.validFrom);
}
};
return BaseFactory;
}());
exports.BaseFactory = BaseFactory;