UNPKG

@jbouduin/holidays-lib

Version:
120 lines (119 loc) 5.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConfigurationFactory = void 0; var errors_1 = require("../errors"); var configuration_1 = require("../configuration"); var types_1 = require("../types"); var christian_factory_1 = require("./christian-factory"); var fixed_date_factory_1 = require("./fixed-date-factory"); var fixed_weekday_factory_1 = require("./fixed-weekday-factory"); var ethiopian_orthodox_factory_1 = require("./ethiopian-orthodox-factory"); var islamic_factory_1 = require("./islamic-factory"); var relative_between_fixed_factory_1 = require("./relative-between-fixed-factory"); var relative_to_date_factory_1 = require("./relative-to-date-factory"); var relative_to_weekday_factory_1 = require("./relative-to-weekday-factory"); var ConfigurationFactory = /** @class */ (function () { //#region Constructor & C° function ConfigurationFactory() { // nothing to do here } //#endregion //#region IConfigurationFactory interface methods ConfigurationFactory.prototype.loadConfigurationFromString = function (parent, asString) { var configuration = new configuration_1.Configuration('', ''); var obj; try { obj = JSON.parse(asString); } catch (error) { configuration.addError(errors_1.ErrorKey.INVALID_FILE_CONTENTS, 'root', error); return configuration; } return this.loadConfiguration(parent, obj); }; ConfigurationFactory.prototype.loadConfiguration = function (parent, obj) { var _this = this; var hierarchy; var result = new configuration_1.Configuration(obj.hierarchy, obj.description); if (!result.hierarchy) { result.addError(errors_1.ErrorKey.HIERARCHY_NOT_SPECIFIED, parent); hierarchy = parent; } else { hierarchy = "".concat(parent === 'root' ? '' : parent, "/").concat(result.hierarchy); } if (!result.description) { result.addError(errors_1.ErrorKey.DESCRIPTION_NOT_SPECIFIED, hierarchy); } if (!obj.holidays) { result.addError(errors_1.ErrorKey.HOLIDAY_COLLECTION_MISSING, hierarchy); } else if (obj.holidays.length === 0) { result.addError(errors_1.ErrorKey.HOLIDAY_COLLECTION_EMPTY, hierarchy); } if (result.errors.length > 0) { return result; } var holidayNumber = 1; obj.holidays.forEach(function (holiday) { var location = "".concat(hierarchy, "/").concat(holidayNumber); var holidayType = types_1.HolidayType[holiday.holidayType]; switch (holidayType) { case types_1.HolidayType.CHRISTIAN: { _this.processFactoryResult(new christian_factory_1.ChristianFactory().create(location, holiday), result); break; } case types_1.HolidayType.ETHIOPIAN_ORTHODOX: { _this.processFactoryResult(new ethiopian_orthodox_factory_1.EthiopianOrthodoxFactory().create(location, holiday), result); break; } case types_1.HolidayType.FIXED_DATE: { _this.processFactoryResult(new fixed_date_factory_1.FixedDateFactory().create(location, holiday), result); break; } case types_1.HolidayType.FIXED_WEEKDAY: { _this.processFactoryResult(new fixed_weekday_factory_1.FixedWeekdayFactory().create(location, holiday), result); break; } case types_1.HolidayType.ISLAMIC: { _this.processFactoryResult(new islamic_factory_1.IslamicFactory().create(location, holiday), result); break; } case types_1.HolidayType.RELATIVE_BETWEEN_FIXED: { _this.processFactoryResult(new relative_between_fixed_factory_1.RelativeBetweenFixedFactory().create(location, holiday), result); break; } case types_1.HolidayType.RELATIVE_TO_DATE: { _this.processFactoryResult(new relative_to_date_factory_1.RelativeToDateFactory().create(location, holiday), result); break; } case types_1.HolidayType.RELATIVE_TO_WEEKDAY: { _this.processFactoryResult(new relative_to_weekday_factory_1.RelativeToWeekdayFactory().create(location, holiday), result); break; } default: { result.addError(errors_1.ErrorKey.HOLIDAY_TYPE_INVALID, location, holiday.holidayType); } } holidayNumber++; }); if (result.holidays.length === 0) { result.addError(errors_1.ErrorKey.NO_VALID_HOLIDAYS_IN_COLLECTION, hierarchy); } if (obj.subConfigurations) { obj.subConfigurations .forEach(function (sub) { return result.subConfigurations.push(_this.loadConfiguration(hierarchy, sub)); }); } return result; }; ConfigurationFactory.prototype.processFactoryResult = function (factoryResult, configuration) { if (factoryResult.errors.length > 0) { factoryResult.errors.forEach(function (error) { return configuration.errors.push(error); }); } if (factoryResult.holiday) { configuration.holidays.push(factoryResult.holiday); } }; return ConfigurationFactory; }()); exports.ConfigurationFactory = ConfigurationFactory;