UNPKG

colombian-holidays

Version:
338 lines (329 loc) 9.29 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { FIRST_HOLIDAY_YEAR: () => FIRST_HOLIDAY_YEAR, LAST_HOLIDAY_YEAR: () => LAST_HOLIDAY_YEAR, colombianHolidays: () => colombianHolidays, default: () => index_default, getHolidaysForYear: () => getHolidaysForYear, holidaysWithinInterval: () => holidaysWithinInterval, isHoliday: () => isHoliday }); module.exports = __toCommonJS(index_exports); // src/helpers.ts var import_pascua = __toESM(require("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 } = (0, import_pascua.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 = (/* @__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; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { FIRST_HOLIDAY_YEAR, LAST_HOLIDAY_YEAR, colombianHolidays, getHolidaysForYear, holidaysWithinInterval, isHoliday });