Skip to content

Commit

Permalink
allow queries before 1984 (#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
MauricioRobayo authored Feb 9, 2023
1 parent 1a2e07a commit 9169061
Show file tree
Hide file tree
Showing 5 changed files with 380 additions and 158 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ npm install colombian-holidays

The module exports a single function to get all the holidays for a given year, returning an array with the holidays for the requested year.

The `year` should be between 1984 and 4099.
The `year` should be between 1583 and 4099.

### CommonJS

Expand Down
26 changes: 15 additions & 11 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import pascua from 'pascua';
import { Holiday, EasterHoliday, ColombianHoliday } from './types';
import pascua from "pascua";
import type { Holiday, EasterHoliday, ColombianHoliday } from "./types";

// 1984 is the year when the current holidays scheme is enforced
// http://www.alcaldiabogota.gov.co/sisjur/normas/Norma1.jsp?i=4954
export const NEW_HOLIDAY_SCHEMA_START_YEAR = 1984;

function getNextDayOfWeek(date: Date, dayOfWeek: number): Date {
const resultDate = new Date(date);
Expand All @@ -13,7 +17,7 @@ function getNextMonday(date: Date): Date {
}

function isEasterHoliday(holiday: Holiday): holiday is EasterHoliday {
return 'offset' in holiday;
return "offset" in holiday;
}

function getHolidayDate(holiday: Holiday, year: number): Date {
Expand All @@ -22,28 +26,28 @@ function getHolidayDate(holiday: Holiday, year: number): Date {
return new Date(year, month - 1, day + holiday.offset);
}

const [month, day] = holiday.date.split('-');
const [month, day] = holiday.date.split("-");
return new Date(year, Number(month) - 1, Number(day));
}

function formatDate(date: Date): string {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
return `${year}-${month}-${day}`;
}

function getHoliday(holiday: Holiday, year: number): ColombianHoliday {
const holidayDate = getHolidayDate(holiday, year);
const celebrationDate = holiday.nextMonday
? getNextMonday(holidayDate)
: holidayDate;

const celebrationDate =
year >= NEW_HOLIDAY_SCHEMA_START_YEAR && holiday.nextMonday
? getNextMonday(holidayDate)
: holidayDate;
return {
date: formatDate(holidayDate),
celebrationDate: formatDate(celebrationDate),
name: holiday.name,
nextMonday: holiday.nextMonday,
nextMonday: year >= NEW_HOLIDAY_SCHEMA_START_YEAR && holiday.nextMonday,
};
}

Expand Down
38 changes: 19 additions & 19 deletions src/holidays.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { DateHoliday, EasterHoliday } from './types';
import type { DateHoliday, EasterHoliday } from "./types";

const dateHolidays: DateHoliday[] = [
{ date: '01-01', name: 'Año Nuevo', nextMonday: false },
{ date: '05-01', name: 'Día del Trabajo', nextMonday: false },
{ date: '07-20', name: 'Grito de la Independencia', nextMonday: false },
{ date: '08-07', name: 'Batalla de Boyacá', nextMonday: false },
{ date: '12-08', name: 'Inmaculada Concepción', nextMonday: false },
{ date: '12-25', name: 'Navidad', nextMonday: false },
{ date: '01-06', name: 'Reyes Magos', nextMonday: true },
{ date: '03-19', name: 'San José', nextMonday: true },
{ date: '06-29', name: 'San Pedro y San Pablo', nextMonday: true },
{ date: '08-15', name: 'Asunción de la Virgen', nextMonday: true },
{ date: '10-12', name: 'Día de la Raza', nextMonday: true },
{ date: '11-01', name: 'Todos los Santos', nextMonday: true },
{ date: '11-11', name: 'Independencia de Cartagena', nextMonday: true },
{ date: "01-01", name: "Año Nuevo", nextMonday: false },
{ date: "05-01", name: "Día del Trabajo", nextMonday: false },
{ date: "07-20", name: "Grito de la Independencia", nextMonday: false },
{ date: "08-07", name: "Batalla de Boyacá", nextMonday: false },
{ date: "12-08", name: "Inmaculada Concepción", nextMonday: false },
{ date: "12-25", name: "Navidad", nextMonday: false },
{ date: "01-06", name: "Reyes Magos", nextMonday: true },
{ date: "03-19", name: "San José", nextMonday: true },
{ date: "06-29", name: "San Pedro y San Pablo", nextMonday: true },
{ date: "08-15", name: "Asunción de la Virgen", nextMonday: true },
{ date: "10-12", name: "Día de la Raza", nextMonday: true },
{ date: "11-01", name: "Todos los Santos", nextMonday: true },
{ date: "11-11", name: "Independencia de Cartagena", nextMonday: true },
];

// We could simplify the calculation by setting the offset to match Monday.
Expand All @@ -23,11 +23,11 @@ const dateHolidays: DateHoliday[] = [
// and then get the next monday instead of directly using 64 as the offset.
// https://www.alcaldiabogota.gov.co/sisjur/normas/Norma1.jsp?i=4954
const easterHolidays: EasterHoliday[] = [
{ offset: -3, name: 'Jueves Santo', nextMonday: false },
{ offset: -2, name: 'Viernes Santo', nextMonday: false },
{ offset: 39, name: 'Ascensión del Señor', nextMonday: true },
{ offset: 60, name: 'Corpus Christi', nextMonday: true },
{ offset: 68, name: 'Sagrado Corazón de Jesús', nextMonday: true },
{ offset: -3, name: "Jueves Santo", nextMonday: false },
{ offset: -2, name: "Viernes Santo", nextMonday: false },
{ offset: 39, name: "Ascensión del Señor", nextMonday: true },
{ offset: 60, name: "Corpus Christi", nextMonday: true },
{ offset: 68, name: "Sagrado Corazón de Jesús", nextMonday: true },
];

export default [...dateHolidays, ...easterHolidays];
Loading

0 comments on commit 9169061

Please sign in to comment.