Skip to content

Commit

Permalink
feat(#477): add isHoliday function (#478)
Browse files Browse the repository at this point in the history
* feat(#477): add isHoliday function

* docs(#477): include utils section
  • Loading branch information
MauricioRobayo authored Feb 9, 2023
1 parent 4d987ac commit 6458f40
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ If the year is omitted, by default the function will return the holidays for the
const currentYearHolidays = colombianHolidays();
```

## Utils

The package provides the `isHoliday` helper which can be imported from `lib/utils`:

```js
import { isHoliday } from 'colombian-holidays/lib/utils`
const date = new Date("2018-01-01T05:00:00.000Z")
if (isHoliday(date)) {
console.log('it is holiday');
} else {
console.log('it is not');
}
```
### TypeScript
The module is written in TypeScript and type definitions files are included.
Expand Down
10 changes: 10 additions & 0 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { isHoliday } from "./utils";

describe("test isHoliday", () => {
it(`should return true for a holiday date object`, () => {
expect(isHoliday(new Date("2018-01-01T05:00:00.000Z"))).toBe(true);
});
it(`should return false for a non holiday date object`, () => {
expect(isHoliday(new Date("2018-01-02T05:00:00.000Z"))).toBe(false);
});
});
14 changes: 14 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import colombianHolidays from ".";

export function isHoliday(date: Date): boolean {
return colombianHolidays(date.getUTCFullYear()).some(
({ celebrationDate }) => {
const d = new Date(celebrationDate);
return (
d.getUTCDate() === date.getUTCDate() &&
d.getUTCMonth() === date.getUTCMonth() &&
d.getUTCFullYear() === date.getUTCFullYear()
);
}
);
}

0 comments on commit 6458f40

Please sign in to comment.