diff --git a/src/index.ts b/src/index.ts index 8ae6892..6aefa0a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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}` diff --git a/src/utils/holidaysWithinInterval.test.ts b/src/utils/holidaysWithinInterval.test.ts index a74cc4b..ee86ace 100644 --- a/src/utils/holidaysWithinInterval.test.ts +++ b/src/utils/holidaysWithinInterval.test.ts @@ -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", () => { diff --git a/src/utils/holidaysWithinInterval.ts b/src/utils/holidaysWithinInterval.ts index a4d1fbb..2eb92b6 100644 --- a/src/utils/holidaysWithinInterval.ts +++ b/src/utils/holidaysWithinInterval.ts @@ -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; }); } diff --git a/tsconfig.json b/tsconfig.json index 9cec24e..19e6530 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "target": "es2017", + "target": "es2019", "module": "CommonJS", "declaration": true, "sourceMap": true,