UNPKG

banking-holidays

Version:

A TypeScript package for detecting banking holidays in USA, Puerto Rico, and Dominican Republic

123 lines (122 loc) 4.42 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getDateStatus = getDateStatus; exports.isHoliday = isHoliday; exports.isWorkingDay = isWorkingDay; exports.getHolidays = getHolidays; exports.getNextFourYearsHolidays = getNextFourYearsHolidays; const types_1 = require("./types"); const date_fns_tz_1 = require("date-fns-tz"); const _2024_json_1 = __importDefault(require("./data/holidays/usa/2024.json")); const _2024_json_2 = __importDefault(require("./data/holidays/pr/2024.json")); const _2024_json_3 = __importDefault(require("./data/holidays/rd/2024.json")); // Map of country codes to their holiday data const holidayData = { USA: { '2024': _2024_json_1.default }, PR: { '2024': _2024_json_2.default }, RD: { '2024': _2024_json_3.default } }; /** * Create a date in the target country's timezone */ function createDateInTimezone(dateStr, country) { const timezone = types_1.COUNTRY_CONFIG[country].timezone; // Create date string with timezone offset const zonedDateStr = `${dateStr}T00:00:00`; return (0, date_fns_tz_1.toZonedTime)(new Date(zonedDateStr), timezone); } /** * Get holidays for a specific country and year */ function getHolidaysForYear(country, year) { const countryData = holidayData[country]; if (!countryData) return []; const yearData = countryData[year]; if (!yearData) return []; return yearData.holidays.map(holiday => ({ ...holiday, country })); } /** * Get all holidays for a given period and country */ function getAllHolidays(options = {}) { const { country, startDate, endDate } = options; const startYear = (startDate === null || startDate === void 0 ? void 0 : startDate.getFullYear().toString()) || '2024'; const endYear = (endDate === null || endDate === void 0 ? void 0 : endDate.getFullYear().toString()) || '2024'; const countries = country ? [country] : Object.keys(holidayData); const years = Array.from({ length: parseInt(endYear) - parseInt(startYear) + 1 }, (_, i) => (parseInt(startYear) + i).toString()); return countries.flatMap((country) => years.flatMap((year) => getHolidaysForYear(country, year))).filter((holiday) => { const holidayDate = new Date(holiday.date); if (startDate && holidayDate < startDate) { return false; } if (endDate && holidayDate > endDate) { return false; } return true; }); } /** * Check if a given date is a banking holiday and its working status */ function getDateStatus(date, country) { // If date is a string, create it in the target timezone const countryDate = typeof date === 'string' ? createDateInTimezone(date, country) : (0, date_fns_tz_1.toZonedTime)(date, types_1.COUNTRY_CONFIG[country].timezone); const dateStr = (0, date_fns_tz_1.formatInTimeZone)(countryDate, types_1.COUNTRY_CONFIG[country].timezone, 'yyyy-MM-dd'); const year = countryDate.getFullYear().toString(); const holidays = getHolidaysForYear(country, year); const holiday = holidays.find(h => h.date === dateStr); const dayOfWeek = countryDate.getDay(); const isWeekend = dayOfWeek === 0 || dayOfWeek === 6; return { isHoliday: !!holiday, holiday, isWorkingDay: !isWeekend && !holiday, isWeekend }; } /** * Check if a given date is a banking holiday */ function isHoliday(date, country) { const status = getDateStatus(date, country); return { isHoliday: status.isHoliday, holiday: status.holiday }; } /** * Check if a given date is a working day */ function isWorkingDay(date, country) { return getDateStatus(date, country).isWorkingDay; } /** * Get all holidays for a given period and country */ function getHolidays(options = {}) { return getAllHolidays(options); } /** * Get the next 4 years of holidays for a specific country */ function getNextFourYearsHolidays(country) { const today = new Date(); const fourYearsFromNow = new Date(); fourYearsFromNow.setFullYear(today.getFullYear() + 4); return getHolidays({ country, startDate: today, endDate: fourYearsFromNow }); } // Export other functions and types here