public-holidays-cn 0.3.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -1
- package/dist/2022.d.ts +9 -0
- package/dist/2023.d.ts +9 -0
- package/dist/2024.d.ts +9 -0
- package/dist/2025.d.ts +9 -0
- package/dist/2026.d.ts +9 -0
- package/dist/async.d.ts +9 -0
- package/dist/common.d.ts +13 -9
- package/dist/index.d.ts +1 -2
- package/dist/public-holidays-cn.cjs.development.js +85 -58
- package/dist/public-holidays-cn.cjs.development.js.map +1 -1
- package/dist/public-holidays-cn.cjs.production.min.js +1 -1
- package/dist/public-holidays-cn.cjs.production.min.js.map +1 -1
- package/dist/public-holidays-cn.esm.js +85 -56
- package/dist/public-holidays-cn.esm.js.map +1 -1
- package/package.json +4 -5
- package/src/2022.ts +51 -0
- package/src/2023.ts +45 -0
- package/src/2024.ts +49 -0
- package/src/2025.ts +46 -0
- package/src/2026.ts +52 -0
- package/src/async.ts +137 -0
- package/src/common.ts +14 -9
- package/src/index.ts +25 -12
package/src/async.ts
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { holidaysOfLaw } from './common';
|
|
2
|
+
import type { TimeValue, HolidayResult } from './common';
|
|
3
|
+
|
|
4
|
+
async function fetchHolidays(year: number): Promise<HolidayResult> {
|
|
5
|
+
switch (year) {
|
|
6
|
+
case 2016: {
|
|
7
|
+
const res = await import('./2016');
|
|
8
|
+
return {
|
|
9
|
+
holidays: res.holidaysOf2016,
|
|
10
|
+
workdays: res.workdaysOf2016,
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
case 2017: {
|
|
14
|
+
const res = await import('./2017');
|
|
15
|
+
return {
|
|
16
|
+
holidays: res.holidaysOf2017,
|
|
17
|
+
workdays: res.workdaysOf2017,
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
case 2018: {
|
|
21
|
+
const res = await import('./2018');
|
|
22
|
+
return {
|
|
23
|
+
holidays: res.holidaysOf2018,
|
|
24
|
+
workdays: res.workdaysOf2018,
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
case 2019: {
|
|
28
|
+
const res = await import('./2019');
|
|
29
|
+
return {
|
|
30
|
+
holidays: res.holidaysOf2019,
|
|
31
|
+
workdays: res.workdaysOf2019,
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
case 2020: {
|
|
35
|
+
const res = await import('./2020');
|
|
36
|
+
return {
|
|
37
|
+
holidays: res.holidaysOf2020,
|
|
38
|
+
workdays: res.workdaysOf2020,
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
case 2021: {
|
|
42
|
+
const res = await import('./2021');
|
|
43
|
+
return {
|
|
44
|
+
holidays: res.holidaysOf2021,
|
|
45
|
+
workdays: res.workdaysOf2021,
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
case 2022: {
|
|
49
|
+
const res = await import('./2022');
|
|
50
|
+
return {
|
|
51
|
+
holidays: res.holidaysOf2022,
|
|
52
|
+
workdays: res.workdaysOf2022,
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
case 2023: {
|
|
56
|
+
const res = await import('./2023');
|
|
57
|
+
return {
|
|
58
|
+
holidays: res.holidaysOf2023,
|
|
59
|
+
workdays: res.workdaysOf2023,
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
case 2024: {
|
|
63
|
+
const res = await import('./2024');
|
|
64
|
+
return {
|
|
65
|
+
holidays: res.holidaysOf2024,
|
|
66
|
+
workdays: res.workdaysOf2024,
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
case 2025: {
|
|
70
|
+
const res = await import('./2025');
|
|
71
|
+
return {
|
|
72
|
+
holidays: res.holidaysOf2025,
|
|
73
|
+
workdays: res.workdaysOf2025,
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
case 2026: {
|
|
77
|
+
const res = await import('./2026');
|
|
78
|
+
return {
|
|
79
|
+
holidays: res.holidaysOf2026,
|
|
80
|
+
workdays: res.workdaysOf2026,
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
default: {
|
|
84
|
+
return {
|
|
85
|
+
holidays: [],
|
|
86
|
+
workdays: []
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* 判断是否为法定节假日,包括调休放假
|
|
94
|
+
*/
|
|
95
|
+
export async function isHolidayAsync(date: TimeValue): Promise<boolean> {
|
|
96
|
+
const dateWrapper = new Date(date);
|
|
97
|
+
const year = dateWrapper.getFullYear();
|
|
98
|
+
const month = (dateWrapper.getMonth() + 1).toString(10).padStart(2, '0');
|
|
99
|
+
const dayOfMonth = dateWrapper
|
|
100
|
+
.getDate()
|
|
101
|
+
.toString(10)
|
|
102
|
+
.padStart(2, '0');
|
|
103
|
+
const dateFormatted = `${year}-${month}-${dayOfMonth}`; // 'YYYY-MM-DD'
|
|
104
|
+
if (year >= 16 && year <= 2026) {
|
|
105
|
+
const { holidays = [], workdays = [] } = await fetchHolidays(year);
|
|
106
|
+
// 法定放假
|
|
107
|
+
if (holidays.includes(dateFormatted)) {
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
// 法定上班
|
|
111
|
+
if (workdays.includes(dateFormatted)) {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
} else {
|
|
115
|
+
const monthDay = `${month}-${dayOfMonth}`; // 'MM-DD'
|
|
116
|
+
// 法定公历假期
|
|
117
|
+
if (holidaysOfLaw.includes(monthDay)) {
|
|
118
|
+
return true;
|
|
119
|
+
} else {
|
|
120
|
+
console.log(
|
|
121
|
+
`[public-holidays-cn] 暂时无法准确判断 ${year} 年度的法定节假日`
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
// 正常双休日
|
|
126
|
+
if ([0, 6].includes(dateWrapper.getDay())) {
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* 判断是否是工作日,包括调休上班
|
|
134
|
+
*/
|
|
135
|
+
export async function isWorkdayAsync(date: TimeValue): Promise<boolean> {
|
|
136
|
+
return !isHolidayAsync(date);
|
|
137
|
+
}
|
package/src/common.ts
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 全国年节及纪念日放假办法假期
|
|
3
|
-
* 新年,放假1天(1月1日)
|
|
4
|
-
* 春节,放假3天(农历正月初一、初二、初三)
|
|
5
|
-
* 清明节,放假1天(农历清明当日)
|
|
6
|
-
* 劳动节,放假1天(5月1日)
|
|
7
|
-
* 端午节,放假1天(农历端午当日)
|
|
8
|
-
* 中秋节,放假1天(农历中秋当日)
|
|
9
|
-
* 国庆节,放假3天(10月1日、2日、3日)
|
|
3
|
+
* - 新年,放假1天(1月1日)
|
|
4
|
+
* - 春节,放假3天(农历正月初一、初二、初三)
|
|
5
|
+
* - 清明节,放假1天(农历清明当日)
|
|
6
|
+
* - 劳动节,放假1天(5月1日)
|
|
7
|
+
* - 端午节,放假1天(农历端午当日)
|
|
8
|
+
* - 中秋节,放假1天(农历中秋当日)
|
|
9
|
+
* - 国庆节,放假3天(10月1日、2日、3日)
|
|
10
10
|
*/
|
|
11
|
-
const holidaysOfLaw = ['01-01', '05-01', '10-01', '10-02', '10-03'];
|
|
11
|
+
export const holidaysOfLaw = ['01-01', '05-01', '10-01', '10-02', '10-03'];
|
|
12
12
|
|
|
13
|
-
export
|
|
13
|
+
export type TimeValue = Date | string | number;
|
|
14
|
+
|
|
15
|
+
export interface HolidayResult {
|
|
16
|
+
holidays: string[];
|
|
17
|
+
workdays: string[];
|
|
18
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,15 +1,23 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { holidaysOf2026, workdaysOf2026 } from './2026';
|
|
2
|
+
import { holidaysOf2025, workdaysOf2025 } from './2025';
|
|
3
|
+
import { holidaysOf2024, workdaysOf2024 } from './2024';
|
|
4
|
+
import { holidaysOf2023, workdaysOf2023 } from './2023';
|
|
5
|
+
import { holidaysOf2022, workdaysOf2022 } from './2022';
|
|
2
6
|
import { holidaysOf2021, workdaysOf2021 } from './2021';
|
|
3
7
|
import { holidaysOf2020, workdaysOf2020 } from './2020';
|
|
4
8
|
import { holidaysOf2019, workdaysOf2019 } from './2019';
|
|
5
9
|
import { holidaysOf2018, workdaysOf2018 } from './2018';
|
|
6
10
|
import { holidaysOf2017, workdaysOf2017 } from './2017';
|
|
7
11
|
import { holidaysOf2016, workdaysOf2016 } from './2016';
|
|
8
|
-
import { holidaysOfLaw
|
|
9
|
-
|
|
10
|
-
type TimeValue = Date | string | number;
|
|
12
|
+
import { holidaysOfLaw } from './common';
|
|
13
|
+
import type { TimeValue } from './common';
|
|
11
14
|
|
|
12
15
|
const holidayMap = new Map([
|
|
16
|
+
[2026, { holidays: holidaysOf2026, workdays: workdaysOf2026 }],
|
|
17
|
+
[2025, { holidays: holidaysOf2025, workdays: workdaysOf2025 }],
|
|
18
|
+
[2024, { holidays: holidaysOf2024, workdays: workdaysOf2024 }],
|
|
19
|
+
[2023, { holidays: holidaysOf2023, workdays: workdaysOf2023 }],
|
|
20
|
+
[2022, { holidays: holidaysOf2022, workdays: workdaysOf2022 }],
|
|
13
21
|
[2021, { holidays: holidaysOf2021, workdays: workdaysOf2021 }],
|
|
14
22
|
[2020, { holidays: holidaysOf2020, workdays: workdaysOf2020 }],
|
|
15
23
|
[2019, { holidays: holidaysOf2019, workdays: workdaysOf2019 }],
|
|
@@ -22,12 +30,17 @@ const holidayMap = new Map([
|
|
|
22
30
|
* 判断是否为法定节假日,包括调休放假
|
|
23
31
|
*/
|
|
24
32
|
export function isHoliday(date: TimeValue): boolean {
|
|
25
|
-
const dateWrapper =
|
|
26
|
-
const
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
33
|
+
const dateWrapper = new Date(date);
|
|
34
|
+
const year = dateWrapper.getFullYear();
|
|
35
|
+
const month = (dateWrapper.getMonth() + 1).toString(10).padStart(2, '0');
|
|
36
|
+
const dayOfMonth = dateWrapper
|
|
37
|
+
.getDate()
|
|
38
|
+
.toString(10)
|
|
39
|
+
.padStart(2, '0');
|
|
40
|
+
const dateFormatted = `${year}-${month}-${dayOfMonth}`; // 'YYYY-MM-DD'
|
|
41
|
+
let holidayMapValue: ReturnType<typeof holidayMap.get>;
|
|
42
|
+
if (holidayMap.has(year) && (holidayMapValue = holidayMap.get(year))) {
|
|
43
|
+
const { holidays = [], workdays = [] } = holidayMapValue;
|
|
31
44
|
// 法定放假
|
|
32
45
|
if (holidays.includes(dateFormatted)) {
|
|
33
46
|
return true;
|
|
@@ -37,7 +50,7 @@ export function isHoliday(date: TimeValue): boolean {
|
|
|
37
50
|
return false;
|
|
38
51
|
}
|
|
39
52
|
} else {
|
|
40
|
-
const monthDay =
|
|
53
|
+
const monthDay = `${month}-${dayOfMonth}`; // 'MM-DD'
|
|
41
54
|
// 法定公历假期
|
|
42
55
|
if (holidaysOfLaw.includes(monthDay)) {
|
|
43
56
|
return true;
|
|
@@ -48,7 +61,7 @@ export function isHoliday(date: TimeValue): boolean {
|
|
|
48
61
|
}
|
|
49
62
|
}
|
|
50
63
|
// 正常双休日
|
|
51
|
-
if ([0, 6].includes(dateWrapper.
|
|
64
|
+
if ([0, 6].includes(dateWrapper.getDay())) {
|
|
52
65
|
return true;
|
|
53
66
|
}
|
|
54
67
|
return false;
|