UNPKG

@jbouduin/holidays-lib

Version:
192 lines (191 loc) 10.3 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HierarchyCalculator = void 0; var configuration_1 = require("../configuration"); var helpers_1 = require("./helpers"); var christian_holiday_calculator_1 = require("./christian-holiday-calculator"); var fixed_holiday_calculator_1 = require("./fixed-holiday-calculator"); var fixed_weekday_calculator_1 = require("./fixed-weekday-calculator"); var relative_holiday_calculator_1 = require("./relative-holiday-calculator"); var CalculatedHoliday = /** @class */ (function () { function CalculatedHoliday(date, fullPath, hierarchy, key, translationKey) { this.date = date; this.fullPath = fullPath; this.hierarchy = hierarchy; this.key = key; this.name = key; this.translationKey = translationKey; } return CalculatedHoliday; }()); var HierarchyCalculator = /** @class */ (function () { //#endregion //#region Constructor function HierarchyCalculator(language, fileProvider) { this.currentLanguage = language; this.fileProvider = fileProvider; } //#endregion //#region IHierarchyCalculator interface methods HierarchyCalculator.prototype.getHolidays = function (hierarchy, year, deep) { return __awaiter(this, void 0, void 0, function () { var root, loadPromises; var _this = this; return __generator(this, function (_a) { root = hierarchy.split('/')[0]; loadPromises = [ this.fileProvider.loadConfiguration(root), this.fileProvider.loadHolidayTranslations(this.currentLanguage), this.fileProvider.loadHolidayTranslations() ]; return [2 /*return*/, Promise.all(loadPromises).then(function (strings) { var configuration = new configuration_1.ConfigurationFactory().loadConfigurationFromString('root', strings[0]); var translations = JSON.parse(strings[1]); var fallback = JSON.parse(strings[2]); var holidays = new helpers_1.HierarchyFilter().filterConfigurationByHierarchy(configuration, hierarchy, deep); var calculatedHolidays = new Array(); holidays.forEach(function (holiday) { return calculatedHolidays.push(_this.calculateHoliday(holiday, year - 1)); }); holidays.forEach(function (holiday) { return calculatedHolidays.push(_this.calculateHoliday(holiday, year)); }); holidays.forEach(function (holiday) { return calculatedHolidays.push(_this.calculateHoliday(holiday, year + 1)); }); var result = calculatedHolidays.filter(function (holiday) { return holiday !== undefined && holiday.date.getFullYear() === year; }); result.forEach(function (calculated) { return _this.translateHoliday(calculated, translations, fallback); }); return result; })]; }); }); }; HierarchyCalculator.prototype.getHierarchyTree = function () { return __awaiter(this, void 0, void 0, function () { var loadPromises; var _this = this; return __generator(this, function (_a) { loadPromises = [ this.fileProvider.loadHierarchies(), this.fileProvider.loadHierarchyTranslations(this.currentLanguage), this.fileProvider.loadHierarchyTranslations() ]; return [2 /*return*/, Promise.all(loadPromises).then(function (strings) { var result = JSON.parse(strings[0]); var translations = JSON.parse(strings[1]); var fallback = JSON.parse(strings[2]); return _this.translateHierarchyTree(result, translations, fallback); })]; }); }); }; HierarchyCalculator.prototype.getSupportedLanguages = function () { return __awaiter(this, void 0, void 0, function () { var dataString, parse; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.fileProvider.loadLanguages()]; case 1: dataString = _a.sent(); parse = JSON.parse(dataString); return [2 /*return*/, parse]; } }); }); }; //#endregion //#region Private methods HierarchyCalculator.prototype.calculateHoliday = function (holiday, year) { var date; switch (holiday.holiday.holidayType) { case configuration_1.HolidayType.CHRISTIAN: { date = new christian_holiday_calculator_1.ChristianHolidayCalculator().calculate(holiday.holiday, year); break; } case configuration_1.HolidayType.ETHIOPIAN_ORTHODOX: { date = undefined; break; } case configuration_1.HolidayType.FIXED_DATE: { date = new fixed_holiday_calculator_1.FixedHolidayCalculator().calculate(holiday.holiday, year); break; } case configuration_1.HolidayType.FIXED_WEEKDAY: { date = new fixed_weekday_calculator_1.FixedWeekdayCalculator().calculate(holiday.holiday, year); break; } case configuration_1.HolidayType.ISLAMIC: { date = undefined; break; } case configuration_1.HolidayType.RELATIVE_BETWEEN_FIXED: case configuration_1.HolidayType.RELATIVE_TO_DATE: case configuration_1.HolidayType.RELATIVE_TO_WEEKDAY: { date = new relative_holiday_calculator_1.RelativeHolidayCalculator().calculate(holiday.holiday, year); } } return date ? new CalculatedHoliday(date, holiday.fullPath, holiday.hierarchy, holiday.holiday.stringKey, holiday.holiday.translationKey) : undefined; }; HierarchyCalculator.prototype.translateHierarchyTree = function (tree, translations, fallback) { var _this = this; tree.forEach(function (hierarchy) { return _this.translateHierarchy(hierarchy, translations, fallback); }); return tree; }; HierarchyCalculator.prototype.translateHierarchy = function (hierachy, translations, fallback) { var _this = this; if (hierachy.children) { hierachy.children.forEach(function (hierarchy) { return _this.translateHierarchy(hierarchy, translations, fallback); }); } if (translations[hierachy.fullPath]) { hierachy.description = translations[hierachy.fullPath]; } else if (fallback[hierachy.fullPath]) { hierachy.description = fallback[hierachy.fullPath]; } }; HierarchyCalculator.prototype.translateHoliday = function (holiday, translations, fallback) { if (translations[holiday.translationKey]) { holiday.name = translations[holiday.translationKey]; } else if (fallback[holiday.translationKey]) { holiday.name = fallback[holiday.translationKey]; } else { holiday.name = holiday.translationKey; } }; return HierarchyCalculator; }()); exports.HierarchyCalculator = HierarchyCalculator;