Last active
August 7, 2020 01:19
-
-
Save elamperti/85a539d9b721cdb5dd5cf09f1f084398 to your computer and use it in GitHub Desktop.
Feriados Argentina
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Feriados Argentina | |
* | |
* Extrae los feriados de https://www.argentina.gob.ar/interior/feriados | |
* Trivialmente adaptable a scripts y/o buena referencia para otros lenguajes. | |
* Comentado en inglés porque empezó como parte de un proposal para la API de holidata. | |
* | |
* Si esto te ahorró unas horas de coding o te fue de ayuda dejá un comentario, | |
* pagame un café (?) o escribime un mail! | |
* | |
*/ | |
let holidays = []; | |
// Gets the year from the title or assumes the current year (what could go wrong?) | |
const year = document.title.match(/20\d{2}/)[0] || new Date().getFullYear(); | |
// Walk through each month in the calendar | |
Array.from(document.getElementsByClassName('cont')).forEach((monthCalendar, i) => { | |
// Fixed holidays usually land on the same day every year | |
const fixedDateHolidaysFilter = 'bg-success'; | |
// Variable date holidays are moved around every year | |
const variableDateHolidaysFilter = 'bg-primary'; | |
// Optional holidays depend on religion or employment sector | |
// and they are usually considered business days | |
const optionalHolidaysFilter = 'bg-nl'; | |
const holidaysByDay = Array.from(monthCalendar.getElementsByTagName('p')) | |
.filter(item => item.className.indexOf('text-muted') < 0) | |
.map(item => { | |
const [_, day, description] = item.textContent.match(/(\d+)\. (.+?)( \(|\.)/); | |
return {day, description}; | |
}) | |
.reduce((arr, item) => { | |
item.day.split(/, ?| y /).forEach(day => { | |
arr[parseInt(day)] = item.description; | |
}); | |
return arr; | |
}, []); | |
const walkHolidayList = (classFilter, options) => { | |
const list = monthCalendar.getElementsByClassName(classFilter); | |
for (let holiday of list) { | |
const day = parseInt(holiday.textContent); | |
holidays.push({ | |
holidayDate: new Date(year, i, day), | |
description: holidaysByDay[day], | |
isFixed: false, | |
isVariable: false, | |
isOptional: false, | |
...options, | |
}); | |
} | |
}; | |
walkHolidayList(fixedDateHolidaysFilter, {isFixed: true}); | |
walkHolidayList(variableDateHolidaysFilter, {isVariable: true}); | |
walkHolidayList(optionalHolidaysFilter, {isOptional: true}); | |
}); | |
console.log(`Holidays for ${year}`, holidays); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment