-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
137 lines (111 loc) · 2.98 KB
/
index.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { holidays as baseHolidays } from 'japanese-public-holidays';
export type HolidayInfo = {
date: string;
endDate: Date;
name: string;
startDate: Date;
};
export type HolidayMap = {
[key: string]: {
[key: string]: {
[key: string]: HolidayInfo;
};
};
};
// NOTE: JST is UTC+9:00
const jstOffsetHour = 1000 * 60 * 60 * 9;
// NOTE: find backward is faster than find forward maybe
export const holidays = baseHolidays.reverse().map(function (holiday) {
const { date, name } = holiday;
const [year, month, day] = date.split('-').map(Number);
if (year === undefined || month === undefined || day === undefined) {
return {
date,
endDate: new Date(''),
name,
startDate: new Date('')
};
}
const startDate = new Date(
Date.UTC(year, month - 1, day, 0, 0, 0, 0) - jstOffsetHour
);
const endDate = new Date(
Date.UTC(year, month - 1, day + 1, 0, 0, 0, 0) - jstOffsetHour - 1
);
return {
date,
endDate,
name,
startDate
};
});
export const holidayMap: HolidayMap = holidays.reduce(function (
result: HolidayMap,
holiday
) {
const { date } = holiday;
const [year, month, day] = date.split('-');
if (year === undefined || month === undefined || day === undefined) {
return result;
}
const yearObject = ensureKey(result, year);
const monthObject = ensureKey(yearObject, month);
monthObject[day] = holiday;
return result;
}, {});
/**
* get holiday info
*
* @param date - target date
* @returns return holiday info if date is holiday, otherwise return null
*/
export function getHolidayInfo(date: Date): HolidayInfo | null {
if (!isDate(date)) {
throw new TypeError('date must be a Date: ' + date);
}
const jstDate = new Date(date.getTime() + jstOffsetHour);
const m = jstDate.getUTCMonth() + 1;
const d = jstDate.getUTCDate();
const year = String(jstDate.getUTCFullYear());
const month = m < 10 ? `0${m}` : String(m);
const day = d < 10 ? `0${d}` : String(d);
const yearObject = ensureKey(holidayMap, year);
const monthObject = ensureKey(yearObject, month);
const result = monthObject[day];
return result === undefined ? null : result;
}
/**
* return true if date is holiday
*
* @param date - target date
* @returns return true if date is holiday
*/
export function isHoliday(date: Date): boolean {
if (!isDate(date)) {
throw new TypeError('date must be a Date: ' + date);
}
return getHolidayInfo(date) !== null;
}
/** cache of Object.prototype.toString */
const toString = Object.prototype.toString;
/**
* return true if date is Date instance
*
* @param value - target value
* @returns return true if value is date
*/
function isDate(value: unknown): value is Date {
return toString.call(value) === '[object Date]';
}
/**
* for TS2532
*
* @param object - target object
* @returns object
*/
function ensureKey<T>(object: Record<string, T | undefined>, key: string): T {
if (!object[key]) {
object[key] = {} as T;
}
return object[key]!;
}