Skip to content

Commit

Permalink
483/add native date support to holidays within interval (#484)
Browse files Browse the repository at this point in the history
* fix(#483): improve return type

* feat(#483): add native date return option to withinInterval
  • Loading branch information
MauricioRobayo authored Feb 10, 2023
1 parent dc02e50 commit 767808f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ function colombianHolidays(
function colombianHolidays(
year?: number,
options?: { returnNativeDate?: boolean }
): (ColombianHoliday | ColombianHolidayWithNativeDate)[];
): ColombianHoliday[] | ColombianHolidayWithNativeDate[];
function colombianHolidays(
year: number = new Date().getFullYear(),
{ returnNativeDate = false }: { returnNativeDate?: boolean } = {}
): unknown[] {
): unknown {
if (year < FIRST_HOLIDAY_YEAR || year > LAST_HOLIDAY_YEAR) {
throw new Error(
`The year should be between ${FIRST_HOLIDAY_YEAR} and ${LAST_HOLIDAY_YEAR}`
Expand Down
27 changes: 27 additions & 0 deletions src/utils/holidaysWithinInterval.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,33 @@ describe("holidaysWithinInterval", () => {
const result = holidaysWithinInterval({ start, end });

expect(result.length).toBe(18);
expect(result).toEqual(
expect.arrayContaining([
expect.objectContaining({
date: expect.any(String),
celebrationDate: expect.any(String),
}),
])
);
});

it("should return the correct number of holidays for a given year with native JS dates", () => {
const start = new Date("2021-01-01");
const end = new Date("2021-12-31");
const result = holidaysWithinInterval({
start,
end,
returnNativeDate: true,
});
expect(result.length).toBe(18);
expect(result).toEqual(
expect.arrayContaining([
expect.objectContaining({
date: expect.any(Date),
celebrationDate: expect.any(Date),
}),
])
);
});

it("should return the correct number of holidays overlapping three years", () => {
Expand Down
36 changes: 27 additions & 9 deletions src/utils/holidaysWithinInterval.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
import colombianHolidays from "..";
import { ColombianHoliday } from "../types";
import { ColombianHoliday, ColombianHolidayWithNativeDate } from "../types";

export type Interval = {
export function holidaysWithinInterval(options: {
start: Date;
end: Date;
};
returnNativeDate?: false;
}): ColombianHoliday[];
export function holidaysWithinInterval(options: {
start: Date;
end: Date;
returnNativeDate?: true;
}): ColombianHolidayWithNativeDate[];
export function holidaysWithinInterval(options: {
start: Date;
end: Date;
returnNativeDate?: boolean;
}): ColombianHoliday[] | ColombianHolidayWithNativeDate[];
export function holidaysWithinInterval({
start,
end,
}: Interval): ColombianHoliday[] {
returnNativeDate = false,
}: {
start: Date;
end: Date;
returnNativeDate?: boolean;
}): unknown {
const yearEnd = new Date(end).getUTCFullYear();
const yearStart = new Date(start).getUTCFullYear();
let holidays: ColombianHoliday[] = [];

for (let i = yearStart; i <= yearEnd; i += 1) {
holidays = holidays.concat(colombianHolidays(i));
}
const holidays = Array.from({ length: yearEnd - yearStart + 1 }, (_, i) =>
colombianHolidays(i + yearStart, { returnNativeDate })
).flat();

return holidays.filter(({ celebrationDate }) => {
const date = new Date(celebrationDate);
const date =
typeof celebrationDate === "string"
? new Date(celebrationDate)
: celebrationDate;
return date >= start && date <= end;
});
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es2017",
"target": "es2019",
"module": "CommonJS",
"declaration": true,
"sourceMap": true,
Expand Down

0 comments on commit 767808f

Please sign in to comment.