UNPKG

geoshell

Version:

A CLI to fetch real-time geo-data from your terminal

86 lines (73 loc) 2.93 kB
/** * Holidays command implementation */ const api = require('../utils/api'); const { getCountryCode } = require('../utils/helpers'); const { CountryNotFound } = require('../utils/errors'); /** * Get national holidays for a country * * @param {string} country - Country name or ISO code * @param {Object} options - Options * @param {number} options.year - Year to get holidays for * @param {boolean} options.upcoming - Show only upcoming holidays * @returns {Promise<Array>} List of holidays */ async function holidays(country, options = {}) { try { const year = options.year || new Date().getFullYear(); const countryCode = getCountryCode(country); const url = `${api.BASE_URLS.holidays}/PublicHolidays/${year}/${countryCode}`; const data = await api.makeRequest(url); if (!data || !Array.isArray(data)) { throw new CountryNotFound(`Holidays for '${country}' not found`); } const holidays = data.map(holiday => ({ name: holiday.name, date: holiday.date, type: holiday.type || 'National' })); // Filter upcoming holidays if requested if (options.upcoming) { const today = new Date().toISOString().split('T')[0]; return holidays.filter(holiday => holiday.date >= today); } return holidays; } catch (error) { if (error instanceof CountryNotFound) { throw error; } // Return mock data if API fails return getMockHolidays(country, options); } } /** * Get mock holiday data * * @param {string} country - Country name * @param {Object} options - Options * @returns {Array} Mock holiday data */ function getMockHolidays(country, options = {}) { const year = options.year || new Date().getFullYear(); const mockHolidays = [ { name: "New Year's Day", date: `${year}-01-01`, type: 'National' }, { name: "Valentine's Day", date: `${year}-02-14`, type: 'Observance' }, { name: "St. Patrick's Day", date: `${year}-03-17`, type: 'Observance' }, { name: "Earth Day", date: `${year}-04-22`, type: 'Observance' }, { name: "Labor Day", date: `${year}-05-01`, type: 'National' }, { name: "Independence Day", date: `${year}-07-04`, type: 'National' }, { name: "Halloween", date: `${year}-10-31`, type: 'Observance' }, { name: "Veterans Day", date: `${year}-11-11`, type: 'National' }, { name: "Thanksgiving Day", date: `${year}-11-25`, type: 'National' }, { name: "Christmas Day", date: `${year}-12-25`, type: 'National' }, { name: "New Year's Eve", date: `${year}-12-31`, type: 'Observance' } ]; // Filter upcoming holidays if requested if (options.upcoming) { const today = new Date().toISOString().split('T')[0]; return mockHolidays.filter(holiday => holiday.date >= today); } return mockHolidays; } module.exports = holidays;