-
Notifications
You must be signed in to change notification settings - Fork 0
/
HolidayCalendar.ts
59 lines (56 loc) · 1.67 KB
/
HolidayCalendar.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { tzLocal } from './LocalTimezone'
import { Timezone } from './Timezone'
import { WeekendCalendar } from './WeekendCalendar'
import { startOfDay } from './startOfDay'
/**
* A calendar class which supports weekends and an array of arbitrary dates.
*
* The following constructs a holiday calendar for the US in 2020.
*
* ```js
* import { HolidayCalendar } from '@jetblack/date'
*
* const calUS = new HolidayCalendar(
* 'US',
* [0, 6], // Saturday and Sunday are always holidays.
* [
* new Date('2020-01-01'), // Wed: New Year's Day
* new Date('2020-01-20'), // Mon: Martin Luther King Jr. Day
* new Date('2020-05-25'), // Mon: Memorial Day
* new Date('2020-07-03'), // Fri: Independence Day
* new Date('2020-09-07'), // Mon: Labor Day
* new Date('2020-11-11'), // Wed: Veterans Day
* new Date('2020-11-26'), // Thu: Thanksgiving
* new Date('2020-12-25') // Fri: Christmas Day
* ]
* )
* ```
*
* @category Calendars
*/
export class HolidayCalendar extends WeekendCalendar {
/** @ignore */
#holidays: Set<number>
/**
* Construct a holiday calendar.
*
* @param name The calendar name.
* @param weekends An array of weekdays which are always holidays.
* @param holidays An array of dates which are holidays.
*/
constructor(
name: string,
weekends: number[] = [0, 6],
holidays: Date[],
tz: Timezone = tzLocal
) {
super(name, weekends)
this.#holidays = new Set(holidays.map(x => startOfDay(x, tz).getTime()))
}
isHoliday(date: Date, tz: Timezone = tzLocal): boolean {
return (
this.isWeekend(date, tz) ||
this.#holidays.has(startOfDay(date, tz).getTime())
)
}
}