Skip to content

Commit

Permalink
479/holidays within (#480)
Browse files Browse the repository at this point in the history
* feat(#479): holidaysWithinInterval

* docs(#479): add holidaysWithinInterval
  • Loading branch information
MauricioRobayo authored Feb 9, 2023
1 parent 6458f40 commit b4104e7
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 6 deletions.
40 changes: 36 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
73 changes: 73 additions & 0 deletions src/utils/holidaysWithinInterval.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
24 changes: 24 additions & 0 deletions src/utils/holidaysWithinInterval.ts
Original file line number Diff line number Diff line change
@@ -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;
});
}
2 changes: 1 addition & 1 deletion src/utils.test.ts → src/utils/isHoliday.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isHoliday } from "./utils";
import { isHoliday } from "./isHoliday";

describe("test isHoliday", () => {
it(`should return true for a holiday date object`, () => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts → src/utils/isHoliday.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import colombianHolidays from ".";
import colombianHolidays from "..";

export function isHoliday(date: Date): boolean {
return colombianHolidays(date.getUTCFullYear()).some(
Expand Down

0 comments on commit b4104e7

Please sign in to comment.