UNPKG

colombian-holidays

Version:
296 lines (289 loc) 7.38 kB
// src/helpers.ts import pascua from "pascua"; var 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 } = pascua(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 = (/* @__PURE__ */ new Date()).getUTCFullYear(), valueAsDate = false } = {}) { const holidayDate = getHolidayDate(holiday, year); const celebrationDate = year >= 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 >= NEW_HOLIDAY_SCHEMA_START_YEAR && holiday.nextMonday }; } var helpers_default = getHoliday; // src/holidays.ts var dateHolidays = [ { month: 1, day: 1, name: { es: "A\xF1o Nuevo", en: "New Year's Day" }, nextMonday: false }, { month: 1, day: 6, name: { es: "Reyes Magos", en: "Epiphany" }, nextMonday: true }, { month: 3, day: 19, name: { es: "San Jos\xE9", en: "Saint Joseph's Day" }, nextMonday: true }, { month: 5, day: 1, name: { es: "D\xEDa del Trabajo", en: "Labour Day" }, nextMonday: false }, { month: 6, day: 29, name: { es: "San Pedro y San Pablo", en: "Saint Peter and Saint Paul" }, nextMonday: true }, { month: 7, day: 20, name: { es: "Grito de la Independencia", en: "Declaration of Independence" }, nextMonday: false }, { month: 8, day: 7, name: { es: "Batalla de Boyac\xE1", en: "Battle of Boyac\xE1" }, nextMonday: false }, { month: 8, day: 15, name: { es: "Asunci\xF3n de la Virgen", en: "Assumption of Mary" }, nextMonday: true }, { month: 10, day: 12, name: { es: "D\xEDa de la Raza", en: "Columbus Day" }, nextMonday: true }, { month: 11, day: 1, name: { es: "Todos los Santos", en: "All Saints\u2019 Day" }, nextMonday: true }, { month: 11, day: 11, name: { es: "Independencia de Cartagena", en: "Independence of Cartagena" }, nextMonday: true }, { month: 12, day: 8, name: { es: "Inmaculada Concepci\xF3n", en: "Immaculate Conception" }, nextMonday: false }, { month: 12, day: 25, name: { es: "Navidad", en: "Christmas" }, nextMonday: false } ]; var easterHolidays = [ { offset: -3, name: { es: "Jueves Santo", en: "Maundy Thursday" }, nextMonday: false }, { offset: -2, name: { es: "Viernes Santo", en: "Good Friday" }, nextMonday: false }, { offset: 39, name: { es: "Ascensi\xF3n del Se\xF1or", en: "Ascension of Jesus" }, nextMonday: true }, { offset: 60, name: { es: "Corpus Christi", en: "Corpus Christi" }, nextMonday: true }, { offset: 68, name: { es: "Sagrado Coraz\xF3n de Jes\xFAs", en: "Sacred Heart" }, nextMonday: true } ]; var holidays_default = [...dateHolidays, ...easterHolidays]; // src/utils/getHolidaysByYear.ts var holidaysWithNativeDateCache = /* @__PURE__ */ new Map(); var holidaysCache = /* @__PURE__ */ new Map(); function getHolidaysForYear(year, { valueAsDate = false } = {}) { if (valueAsDate) { const cachedHolidays2 = holidaysWithNativeDateCache.get(year); if (cachedHolidays2) { return cachedHolidays2; } const holidays2 = index_default({ year, valueAsDate }); holidaysWithNativeDateCache.set(year, holidays2); return holidays2; } const cachedHolidays = holidaysCache.get(year); if (cachedHolidays) { return cachedHolidays; } const holidays = index_default({ year, valueAsDate }); holidaysCache.set(year, holidays); return holidays; } // src/utils/holidaysWithinInterval.ts function holidaysWithinInterval({ start, end, valueAsDate = false }) { if (start >= end) { throw new Error("end date should be greater than start date"); } const yearEnd = end.getUTCFullYear(); const yearStart = start.getUTCFullYear(); const holidays = Array.from( { length: yearEnd - yearStart + 1 }, (_, i) => getHolidaysForYear(i + yearStart, { valueAsDate: true }) ).flat(); const holidaysWithin = holidays.filter( ({ celebrationDate }) => celebrationDate >= start && celebrationDate <= end ); if (valueAsDate) { return holidaysWithin; } return holidaysWithin.map((holiday) => ({ ...holiday, date: holiday.date.toISOString().slice(0, 10), celebrationDate: holiday.celebrationDate.toISOString().slice(0, 10) })); } // src/utils/helpers.ts function isSameDate(date1, date2) { return date1.getUTCDate() === date2.getUTCDate() && date1.getUTCMonth() === date2.getUTCMonth() && date1.getUTCFullYear() === date2.getUTCFullYear(); } // src/utils/isHoliday.ts function isHoliday(date) { const holidays = getHolidaysForYear(date.getUTCFullYear(), { valueAsDate: true }); return holidays.some( ({ celebrationDate }) => isSameDate(celebrationDate, date) ); } // src/index.ts var FIRST_HOLIDAY_YEAR = 1583; var LAST_HOLIDAY_YEAR = 4099; function colombianHolidays({ year = (/* @__PURE__ */ new Date()).getUTCFullYear(), month, valueAsDate = false } = {}) { if (year < FIRST_HOLIDAY_YEAR || year > LAST_HOLIDAY_YEAR) { throw new Error( `The year should be between ${FIRST_HOLIDAY_YEAR} and ${LAST_HOLIDAY_YEAR}` ); } return holidays_default.map((holiday) => helpers_default(holiday, { year, valueAsDate })).filter((holiday) => { if (month === void 0) { return true; } if (typeof holiday.celebrationDate === "string") { return Number(holiday.celebrationDate.slice(5, 7)) === month; } return holiday.celebrationDate.getUTCMonth() + 1 === month; }).sort((a, b) => { if (a.celebrationDate instanceof Date && b.celebrationDate instanceof Date) { return a.celebrationDate.getTime() - b.celebrationDate.getTime(); } if (typeof a.celebrationDate === "string" && typeof b.celebrationDate === "string") { return a.celebrationDate.localeCompare(b.celebrationDate); } throw new Error("Invariant violation: this state is not possible."); }); } var index_default = colombianHolidays; export { FIRST_HOLIDAY_YEAR, LAST_HOLIDAY_YEAR, colombianHolidays, index_default as default, getHolidaysForYear, holidaysWithinInterval, isHoliday };