UNPKG

colombian-holidays

Version:
50 lines 2.11 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.NEW_HOLIDAY_SCHEMA_START_YEAR = void 0; const pascua_1 = __importDefault(require("pascua")); // 1984 is the year when the current holidays scheme is enforced // http://www.alcaldiabogota.gov.co/sisjur/normas/Norma1.jsp?i=4954 exports.NEW_HOLIDAY_SCHEMA_START_YEAR = 1984; function getNextDayOfWeek(date, dayOfWeek) { const resultDate = new Date(date); resultDate.setUTCDate(date.getUTCDate() + ((7 + dayOfWeek - date.getUTCDay()) % 7)); return resultDate; } function getNextMonday(date) { const MONDAY = 1; return getNextDayOfWeek(date, MONDAY); } function isEasterHoliday(holiday) { return "offset" in holiday; } function getHolidayDate(holiday, year) { if (isEasterHoliday(holiday)) { const { month, day } = (0, pascua_1.default)(year); const date = new Date(generateUtcStringFromDateParts(year, month, 1)); date.setUTCDate(day + holiday.offset); return date; } return new Date(generateUtcStringFromDateParts(year, holiday.month, holiday.day)); } function generateUtcStringFromDateParts(year, month, day) { return `${year}-${String(month).padStart(2, "0")}-${String(day).padStart(2, "0")}`; } function getHoliday(holiday, { year = new Date().getUTCFullYear(), valueAsDate = false, } = {}) { const holidayDate = getHolidayDate(holiday, year); const celebrationDate = year >= exports.NEW_HOLIDAY_SCHEMA_START_YEAR && holiday.nextMonday ? getNextMonday(holidayDate) : holidayDate; return { date: valueAsDate ? holidayDate : holidayDate.toISOString().slice(0, 10), celebrationDate: valueAsDate ? celebrationDate : celebrationDate.toISOString().slice(0, 10), name: holiday.name, nextMonday: year >= exports.NEW_HOLIDAY_SCHEMA_START_YEAR && holiday.nextMonday, }; } exports.default = getHoliday; //# sourceMappingURL=helpers.js.map