From b4104e73986cbcd622be28436d3155fb0f0bfa29 Mon Sep 17 00:00:00 2001 From: Mauricio Robayo Date: Thu, 9 Feb 2023 10:24:37 -0500 Subject: [PATCH] 479/holidays within (#480) * feat(#479): holidaysWithinInterval * docs(#479): add holidaysWithinInterval --- README.md | 40 +++++++++- src/utils/holidaysWithinInterval.test.ts | 73 +++++++++++++++++++ src/utils/holidaysWithinInterval.ts | 24 ++++++ .../isHoliday.test.ts} | 2 +- src/{utils.ts => utils/isHoliday.ts} | 2 +- 5 files changed, 135 insertions(+), 6 deletions(-) create mode 100644 src/utils/holidaysWithinInterval.test.ts create mode 100644 src/utils/holidaysWithinInterval.ts rename src/{utils.test.ts => utils/isHoliday.test.ts} (88%) rename src/{utils.ts => utils/isHoliday.ts} (91%) diff --git a/README.md b/README.md index 60b7bf4..7e5a8c1 100644 --- a/README.md +++ b/README.md @@ -74,20 +74,52 @@ const currentYearHolidays = colombianHolidays(); ## Utils -The package provides the `isHoliday` helper which can be imported from `lib/utils`: +The package provides two helper functions which can be imported from `lib/utils`: + +### isHoliday + +Returns true if the given date is a colombian holiday, otherwise returns false. ```js -import { isHoliday } from 'colombian-holidays/lib/utils` +import { isHoliday } from 'colombian-holidays/lib/utils/isHoliday` const date = new Date("2018-01-01T05:00:00.000Z") if (isHoliday(date)) { - console.log('it is holiday'); + console.log('it is a colombian holiday'); } else { - console.log('it is not'); + console.log('it is NOT a colombian holiday'); } ``` +### holidaysWithinInterval + +Returns an with the colombian holidays within two dates: + +```js +import { holidaysWithinInterval } from 'colombian-holidays/lib/utils/holidaysWithinInterval` + +const start = new Date("2021-01-01"); +const end = new Date("2021-01-11"); +const holidays = holidaysWithinInterval({ start, end }); +/* +[ + { + celebrationDate: "2021-01-01", + date: "2021-01-01", + name: "Año Nuevo", + nextMonday: false, + }, + { + celebrationDate: "2021-01-11", + date: "2021-01-06", + name: "Reyes Magos", + nextMonday: true, + }, +] +*/ +``` + ### TypeScript The module is written in TypeScript and type definitions files are included. diff --git a/src/utils/holidaysWithinInterval.test.ts b/src/utils/holidaysWithinInterval.test.ts new file mode 100644 index 0000000..a74cc4b --- /dev/null +++ b/src/utils/holidaysWithinInterval.test.ts @@ -0,0 +1,73 @@ +import { holidaysWithinInterval } from "./holidaysWithinInterval"; + +describe("holidaysWithinInterval", () => { + it("should return the correct number of holidays overlapping two years", () => { + const start = new Date("2020-12-01"); + const end = new Date("2021-01-15"); + const result = holidaysWithinInterval({ end, start }); + + expect(result).toEqual([ + { + celebrationDate: "2020-12-08", + date: "2020-12-08", + name: "Inmaculada Concepción", + nextMonday: false, + }, + { + celebrationDate: "2020-12-25", + date: "2020-12-25", + name: "Navidad", + nextMonday: false, + }, + { + celebrationDate: "2021-01-01", + date: "2021-01-01", + name: "Año Nuevo", + nextMonday: false, + }, + { + celebrationDate: "2021-01-11", + date: "2021-01-06", + name: "Reyes Magos", + nextMonday: true, + }, + ]); + }); + + it("should return the correct number of holidays inclusive", () => { + const start = new Date("2021-01-01"); + const end = new Date("2021-01-11"); + const result = holidaysWithinInterval({ start, end }); + + expect(result).toEqual([ + { + celebrationDate: "2021-01-01", + date: "2021-01-01", + name: "Año Nuevo", + nextMonday: false, + }, + { + celebrationDate: "2021-01-11", + date: "2021-01-06", + name: "Reyes Magos", + nextMonday: true, + }, + ]); + }); + + it("should return the correct number of holidays for a given year", () => { + const start = new Date("2021-01-01"); + const end = new Date("2021-12-31"); + const result = holidaysWithinInterval({ start, end }); + + expect(result.length).toBe(18); + }); + + it("should return the correct number of holidays overlapping three years", () => { + const start = new Date("2014-07-20"); + const end = new Date("2016-03-25"); + const result = holidaysWithinInterval({ start, end }); + + expect(result.length).toBe(31); + }); +}); diff --git a/src/utils/holidaysWithinInterval.ts b/src/utils/holidaysWithinInterval.ts new file mode 100644 index 0000000..a4d1fbb --- /dev/null +++ b/src/utils/holidaysWithinInterval.ts @@ -0,0 +1,24 @@ +import colombianHolidays from ".."; +import { ColombianHoliday } from "../types"; + +export type Interval = { + start: Date; + end: Date; +}; +export function holidaysWithinInterval({ + start, + end, +}: Interval): ColombianHoliday[] { + 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)); + } + + return holidays.filter(({ celebrationDate }) => { + const date = new Date(celebrationDate); + return date >= start && date <= end; + }); +} diff --git a/src/utils.test.ts b/src/utils/isHoliday.test.ts similarity index 88% rename from src/utils.test.ts rename to src/utils/isHoliday.test.ts index fb098e4..5fb2662 100644 --- a/src/utils.test.ts +++ b/src/utils/isHoliday.test.ts @@ -1,4 +1,4 @@ -import { isHoliday } from "./utils"; +import { isHoliday } from "./isHoliday"; describe("test isHoliday", () => { it(`should return true for a holiday date object`, () => { diff --git a/src/utils.ts b/src/utils/isHoliday.ts similarity index 91% rename from src/utils.ts rename to src/utils/isHoliday.ts index f337747..eef97c4 100644 --- a/src/utils.ts +++ b/src/utils/isHoliday.ts @@ -1,4 +1,4 @@ -import colombianHolidays from "."; +import colombianHolidays from ".."; export function isHoliday(date: Date): boolean { return colombianHolidays(date.getUTCFullYear()).some(