@momo-kits/calendar 0.0.74-beta → 0.72.1

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/src/Util.js CHANGED
@@ -1,225 +1,305 @@
1
1
  import moment from 'moment';
2
2
 
3
- import { SwitchLanguage } from '@momo-kits/core';
3
+ import {SwitchLanguage} from '@momo-kits/core';
4
4
  import LunarDateConverter from './LunarDateConverter';
5
5
  import holiday from './holidayData';
6
6
 
7
7
  const I18N_MAP = {
8
- en: {
9
- w: ['', 'Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun'],
10
- weekday: ['', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
11
- text: {
12
- start: 'Start',
13
- end: 'End',
14
- date: 'Date',
15
- save: 'Save',
16
- clear: 'Reset'
17
- },
18
- date: 'DD / MM'
8
+ en: {
9
+ w: ['', 'Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun'],
10
+ weekday: [
11
+ '',
12
+ 'Monday',
13
+ 'Tuesday',
14
+ 'Wednesday',
15
+ 'Thursday',
16
+ 'Friday',
17
+ 'Saturday',
18
+ 'Sunday',
19
+ ],
20
+ text: {
21
+ start: 'Start',
22
+ end: 'End',
23
+ date: 'Date',
24
+ save: 'Save',
25
+ clear: 'Reset',
19
26
  },
20
- vi: {
21
- w: ['', 'T2', 'T3', 'T4', 'T5', 'T6', 'T7', 'CN'],
22
- weekday: ['', 'Thứ hai', 'Thứ ba', 'Thứ tư', 'Thứ năm', 'Thứ sáu', 'Thứ bảy', 'Chủ nhật'],
23
- text: {
24
- start: 'Start',
25
- end: 'End',
26
- date: 'Date',
27
- save: 'Save',
28
- clear: 'Reset'
29
- },
30
- date: 'DD / MM'
31
- }
27
+ date: 'DD / MM',
28
+ },
29
+ vi: {
30
+ w: ['', 'T2', 'T3', 'T4', 'T5', 'T6', 'T7', 'CN'],
31
+ weekday: [
32
+ '',
33
+ 'Thứ hai',
34
+ 'Thứ ba',
35
+ 'Thứ ',
36
+ 'Thứ năm',
37
+ 'Thứ sáu',
38
+ 'Thứ bảy',
39
+ 'Chủ nhật',
40
+ ],
41
+ text: {
42
+ start: 'Start',
43
+ end: 'End',
44
+ date: 'Date',
45
+ save: 'Save',
46
+ clear: 'Reset',
47
+ },
48
+ date: 'DD / MM',
49
+ },
32
50
  };
33
51
 
34
- const Solar = (momentDate) => ({
35
- solarDay: momentDate.date(),
36
- solarMonth: momentDate.month() + 1,
37
- solarYear: momentDate.year()
52
+ const Solar = momentDate => ({
53
+ solarDay: momentDate.date(),
54
+ solarMonth: momentDate.month() + 1,
55
+ solarYear: momentDate.year(),
38
56
  });
39
57
 
40
- const formatYYYYMMDD = (dd, mm, yyyy) => `${yyyy}-${mm < 10 ? `0${mm}` : mm}-${dd < 10 ? `0${dd}` : dd}`;
41
-
42
- const formatDDMM = (dd, mm) => `${dd < 10 ? `0${dd}` : dd}/${mm < 10 ? `0${mm}` : mm}`;
43
-
44
- const groupHolidaysByDate = (holidays) => {
45
- const groupedHolidays = {};
46
- if (holidays && holidays.length > 0) {
47
- holidays.forEach((item) => {
48
- const {
49
- day, month, lunar, label
50
- } = item;
51
- const key = formatDDMM(day, month);
52
- let holidaysObj = groupedHolidays[key] ? { ...groupedHolidays[key] } : { ...item };
53
- if (groupedHolidays[key]) {
54
- if (holidaysObj.lunar && !lunar) {
55
- holidaysObj = {
56
- ...holidaysObj,
57
- label,
58
- mixedLabel: `${label}, ${holidaysObj.label}`
59
- };
60
- } else if (!holidaysObj.lunar && lunar) {
61
- holidaysObj = {
62
- ...holidaysObj,
63
- label: holidaysObj.lunar,
64
- mixedLabel: `${holidaysObj.lunar}, ${label}`
65
- };
66
- }
67
- }
68
- groupedHolidays[key] = holidaysObj;
69
- });
70
- }
58
+ const formatYYYYMMDD = (dd, mm, yyyy) =>
59
+ `${yyyy}-${mm < 10 ? `0${mm}` : mm}-${dd < 10 ? `0${dd}` : dd}`;
60
+
61
+ const formatDDMM = (dd, mm) =>
62
+ `${dd < 10 ? `0${dd}` : dd}/${mm < 10 ? `0${mm}` : mm}`;
71
63
 
72
- return groupedHolidays;
64
+ const groupHolidaysByDate = holidays => {
65
+ const groupedHolidays = {};
66
+ if (holidays && holidays.length > 0) {
67
+ holidays.forEach(item => {
68
+ const {day, month, lunar, label} = item;
69
+ const key = formatDDMM(day, month);
70
+ let holidaysObj = groupedHolidays[key]
71
+ ? {...groupedHolidays[key]}
72
+ : {...item};
73
+ if (groupedHolidays[key]) {
74
+ if (holidaysObj.lunar && !lunar) {
75
+ holidaysObj = {
76
+ ...holidaysObj,
77
+ label,
78
+ mixedLabel: `${label}, ${holidaysObj.label}`,
79
+ };
80
+ } else if (!holidaysObj.lunar && lunar) {
81
+ holidaysObj = {
82
+ ...holidaysObj,
83
+ label: holidaysObj.lunar,
84
+ mixedLabel: `${holidaysObj.lunar}, ${label}`,
85
+ };
86
+ }
87
+ }
88
+ groupedHolidays[key] = holidaysObj;
89
+ });
90
+ }
91
+
92
+ return groupedHolidays;
73
93
  };
74
94
 
75
- const sortByDate = (arr) => {
76
- if (arr && arr.length > 1) {
77
- arr.sort((a, b) => {
78
- if (a.month > b.month || (a.month === b.month && a.day > b.day)) {
79
- return 1;
80
- }
81
- return -1;
82
- });
83
- }
95
+ const sortByDate = arr => {
96
+ if (arr && arr.length > 1) {
97
+ arr.sort((a, b) => {
98
+ if (a.month > b.month || (a.month === b.month && a.day > b.day)) {
99
+ return 1;
100
+ }
101
+ return -1;
102
+ });
103
+ }
84
104
 
85
- return groupHolidaysByDate(arr);
105
+ return groupHolidaysByDate(arr);
86
106
  };
87
107
 
88
108
  module.exports = {
109
+ WEEKDAYS: ['T2', 'T3', 'T4', 'T5', 'T6', 'T7', 'CN'],
89
110
 
90
- WEEKDAYS: [
91
- 'T2', 'T3', 'T4', 'T5', 'T6', 'T7', 'CN',
92
- ],
111
+ WEEKDAYSFROMMOMENT: [
112
+ SwitchLanguage.sun,
113
+ SwitchLanguage.mon,
114
+ SwitchLanguage.tue,
115
+ SwitchLanguage.wed,
116
+ SwitchLanguage.thu,
117
+ SwitchLanguage.fri,
118
+ SwitchLanguage.sat,
119
+ ],
93
120
 
94
- WEEKDAYSFROMMOMENT: [SwitchLanguage.sun, SwitchLanguage.mon, SwitchLanguage.tue, SwitchLanguage.wed, SwitchLanguage.thu, SwitchLanguage.fri, SwitchLanguage.sat],
121
+ WEEKDAYSNAME: [
122
+ 'Thứ 2',
123
+ 'Thứ 3',
124
+ 'Thứ 4',
125
+ 'Thứ 5',
126
+ 'Thứ 6',
127
+ 'Thứ 7',
128
+ 'Chủ nhật',
129
+ ],
95
130
 
96
- WEEKDAYSNAME: [
97
- 'Thứ 2', 'Thứ 3', 'Thứ 4', 'Thứ 5', 'Thứ 6', 'Thứ 7', 'Chủ nhật',
98
- ],
131
+ MONTHS: [
132
+ 'Tháng 1',
133
+ 'Tháng 2',
134
+ 'Tháng 3',
135
+ 'Tháng 4',
136
+ 'Tháng 5',
137
+ 'Tháng 6',
138
+ 'Tháng 7',
139
+ 'Tháng 8',
140
+ 'Tháng 9',
141
+ 'Tháng 10',
142
+ 'Tháng 11',
143
+ 'Tháng 12',
144
+ ],
99
145
 
100
- MONTHS: [
101
- 'Tháng 1', 'Tháng 2', 'Tháng 3', 'Tháng 4', 'Tháng 5', 'Tháng 6', 'Tháng 7',
102
- 'Tháng 8', 'Tháng 9', 'Tháng 10', 'Tháng 11', 'Tháng 12'
103
- ],
146
+ MAX_ROWS: 7,
104
147
 
105
- MAX_ROWS: 7,
148
+ MAX_COLUMNS: 7,
106
149
 
107
- MAX_COLUMNS: 7,
150
+ mapWeeKDate(i) {
151
+ const date = [
152
+ '',
153
+ SwitchLanguage.mon,
154
+ SwitchLanguage.tue,
155
+ SwitchLanguage.wed,
156
+ SwitchLanguage.thu,
157
+ SwitchLanguage.fri,
158
+ SwitchLanguage.sat,
159
+ SwitchLanguage.sun,
160
+ ];
161
+ return date[i];
162
+ },
108
163
 
109
- mapWeeKDate(i) {
110
- const date = ['', SwitchLanguage.mon, SwitchLanguage.tue, SwitchLanguage.wed, SwitchLanguage.thu, SwitchLanguage.fri, SwitchLanguage.sat, SwitchLanguage.sun];
111
- return date[i];
112
- },
164
+ mapMonth(i) {
165
+ const month = new Map([
166
+ [1, SwitchLanguage.jan],
167
+ [2, SwitchLanguage.feb],
168
+ [3, SwitchLanguage.mar],
169
+ [4, SwitchLanguage.apr],
170
+ [5, SwitchLanguage.may],
171
+ [6, SwitchLanguage.jun],
172
+ [7, SwitchLanguage.jul],
173
+ [8, SwitchLanguage.aug],
174
+ [9, SwitchLanguage.sep],
175
+ [10, SwitchLanguage.oct],
176
+ [11, SwitchLanguage.nov],
177
+ [12, SwitchLanguage.dec],
178
+ ]);
179
+ return month.get(i);
180
+ },
113
181
 
114
- mapMonth(i) {
115
- const month = new Map([
116
- [1, SwitchLanguage.jan],
117
- [2, SwitchLanguage.feb],
118
- [3, SwitchLanguage.mar],
119
- [4, SwitchLanguage.apr],
120
- [5, SwitchLanguage.may],
121
- [6, SwitchLanguage.jun],
122
- [7, SwitchLanguage.jul],
123
- [8, SwitchLanguage.aug],
124
- [9, SwitchLanguage.sep],
125
- [10, SwitchLanguage.oct],
126
- [11, SwitchLanguage.nov],
127
- [12, SwitchLanguage.dec],
128
- ]);
129
- return month.get(i);
130
- },
182
+ mapMonthShorten(i) {
183
+ const month = new Map([
184
+ [1, 'Jan'],
185
+ [2, 'Feb'],
186
+ [3, 'Mar'],
187
+ [4, 'Apr'],
188
+ [5, 'May'],
189
+ [6, 'Jun'],
190
+ [7, 'Jul'],
191
+ [8, 'Aug'],
192
+ [9, 'Sep'],
193
+ [10, 'Oct'],
194
+ [11, 'Nov'],
195
+ [12, 'Dec'],
196
+ ]);
197
+ return month.get(i);
198
+ },
131
199
 
132
- mapMonthShorten(i) {
133
- const month = new Map([
134
- [1, 'Jan'],
135
- [2, 'Feb'],
136
- [3, 'Mar'],
137
- [4, 'Apr'],
138
- [5, 'May'],
139
- [6, 'Jun'],
140
- [7, 'Jul'],
141
- [8, 'Aug'],
142
- [9, 'Sep'],
143
- [10, 'Oct'],
144
- [11, 'Nov'],
145
- [12, 'Dec'],
146
- ]);
147
- return month.get(i);
148
- },
200
+ getDaysInMonth(month, year) {
201
+ const lastDayOfMonth = new Date(year, month + 1, 0);
202
+ return lastDayOfMonth.getDate();
203
+ },
149
204
 
150
- getDaysInMonth(month, year) {
151
- const lastDayOfMonth = new Date(year, month + 1, 0);
152
- return lastDayOfMonth.getDate();
153
- },
205
+ getHolidaysInMonth(headerInfo) {
206
+ if (headerInfo) {
207
+ const today = moment();
208
+ const converter = new LunarDateConverter();
209
+ const startDate = moment(headerInfo).startOf('month');
210
+ const endDate = moment(headerInfo).endOf('month');
211
+ const minLunarDate = converter.SolarToLunar(Solar(startDate));
212
+ const maxLunarDate = converter.SolarToLunar(Solar(endDate));
213
+ const holidays = [];
214
+ const currentYear =
215
+ minLunarDate.lunarYear !== maxLunarDate.lunarYear
216
+ ? [minLunarDate.lunarYear, maxLunarDate.lunarYear]
217
+ : minLunarDate.lunarYear;
154
218
 
155
- getHolidaysInMonth(headerInfo) {
156
- if (headerInfo) {
157
- const today = moment();
158
- const converter = new LunarDateConverter();
159
- const startDate = moment(headerInfo).startOf('month');
160
- const endDate = moment(headerInfo).endOf('month');
161
- const minLunarDate = converter.SolarToLunar(Solar(startDate));
162
- const maxLunarDate = converter.SolarToLunar(Solar(endDate));
163
- const holidays = [];
164
- const currentYear = minLunarDate.lunarYear !== maxLunarDate.lunarYear ? [minLunarDate.lunarYear, maxLunarDate.lunarYear] : minLunarDate.lunarYear;
165
-
166
- // Handle Solar holidays
167
- if (holiday.solar[headerInfo.month() + 1]) {
168
- holiday.solar[headerInfo.month() + 1].forEach((date) => {
169
- const dateAsMoment = moment({ year: headerInfo.year(), month: date.month - 1, date: date.day });
170
- if (dateAsMoment.isSameOrAfter(today, 'date')) {
171
- holidays.push(date);
172
- }
173
- });
174
- }
175
-
176
- holiday.lunar.forEach((item) => {
177
- if (currentYear instanceof Array) {
178
- const solar1 = converter.LunarToSolar({
179
- lunarDay: item.lunarDay,
180
- lunarMonth: item.lunarMonth,
181
- lunarYear: currentYear[0]
182
- });
183
- const solar2 = converter.LunarToSolar({
184
- lunarDay: item.lunarDay,
185
- lunarMonth: item.lunarMonth,
186
- lunarYear: currentYear[1]
187
- });
188
- const solar1AsMoment = moment(formatYYYYMMDD(solar1.solarDay, solar1.solarMonth, solar1.solarYear));
189
- const solar2AsMoment = moment(formatYYYYMMDD(solar2.solarDay, solar2.solarMonth, solar2.solarYear));
190
- if (solar1AsMoment.isBetween(startDate, endDate) && solar1AsMoment.isSameOrAfter(today, 'date')) {
191
- holidays.push({
192
- ...item,
193
- day: solar1.solarDay,
194
- month: solar1.solarMonth
195
- });
196
- } else if (solar2AsMoment.isBetween(startDate, endDate) && solar2AsMoment.isSameOrAfter(today, 'date')) {
197
- holidays.push({
198
- ...item,
199
- day: solar2.solarDay,
200
- month: solar2.solarMonth
201
- });
202
- }
203
- } else {
204
- const solar = converter.LunarToSolar({
205
- lunarDay: item.lunarDay,
206
- lunarMonth: item.lunarMonth,
207
- lunarYear: currentYear
208
- });
209
- const solarAsMoment = moment(formatYYYYMMDD(solar.solarDay, solar.solarMonth, solar.solarYear));
210
- if (solarAsMoment.isBetween(startDate, endDate) && solarAsMoment.isSameOrAfter(today, 'date')) {
211
- holidays.push({
212
- ...item,
213
- day: solar.solarDay,
214
- month: solar.solarMonth
215
- });
216
- }
217
- }
218
- });
219
+ // Handle Solar holidays
220
+ if (holiday.solar[headerInfo.month() + 1]) {
221
+ holiday.solar[headerInfo.month() + 1].forEach(date => {
222
+ const dateAsMoment = moment({
223
+ year: headerInfo.year(),
224
+ month: date.month - 1,
225
+ date: date.day,
226
+ });
227
+ if (dateAsMoment.isSameOrAfter(today, 'date')) {
228
+ holidays.push(date);
229
+ }
230
+ });
231
+ }
219
232
 
220
- return sortByDate(holidays);
233
+ holiday.lunar.forEach(item => {
234
+ if (currentYear instanceof Array) {
235
+ const solar1 = converter.LunarToSolar({
236
+ lunarDay: item.lunarDay,
237
+ lunarMonth: item.lunarMonth,
238
+ lunarYear: currentYear[0],
239
+ });
240
+ const solar2 = converter.LunarToSolar({
241
+ lunarDay: item.lunarDay,
242
+ lunarMonth: item.lunarMonth,
243
+ lunarYear: currentYear[1],
244
+ });
245
+ const solar1AsMoment = moment(
246
+ formatYYYYMMDD(
247
+ solar1.solarDay,
248
+ solar1.solarMonth,
249
+ solar1.solarYear,
250
+ ),
251
+ );
252
+ const solar2AsMoment = moment(
253
+ formatYYYYMMDD(
254
+ solar2.solarDay,
255
+ solar2.solarMonth,
256
+ solar2.solarYear,
257
+ ),
258
+ );
259
+ if (
260
+ solar1AsMoment.isBetween(startDate, endDate) &&
261
+ solar1AsMoment.isSameOrAfter(today, 'date')
262
+ ) {
263
+ holidays.push({
264
+ ...item,
265
+ day: solar1.solarDay,
266
+ month: solar1.solarMonth,
267
+ });
268
+ } else if (
269
+ solar2AsMoment.isBetween(startDate, endDate) &&
270
+ solar2AsMoment.isSameOrAfter(today, 'date')
271
+ ) {
272
+ holidays.push({
273
+ ...item,
274
+ day: solar2.solarDay,
275
+ month: solar2.solarMonth,
276
+ });
277
+ }
278
+ } else {
279
+ const solar = converter.LunarToSolar({
280
+ lunarDay: item.lunarDay,
281
+ lunarMonth: item.lunarMonth,
282
+ lunarYear: currentYear,
283
+ });
284
+ const solarAsMoment = moment(
285
+ formatYYYYMMDD(solar.solarDay, solar.solarMonth, solar.solarYear),
286
+ );
287
+ if (
288
+ solarAsMoment.isBetween(startDate, endDate) &&
289
+ solarAsMoment.isSameOrAfter(today, 'date')
290
+ ) {
291
+ holidays.push({
292
+ ...item,
293
+ day: solar.solarDay,
294
+ month: solar.solarMonth,
295
+ });
296
+ }
221
297
  }
222
- return [];
223
- },
224
- I18N_MAP,
298
+ });
299
+
300
+ return sortByDate(holidays);
301
+ }
302
+ return [];
303
+ },
304
+ I18N_MAP,
225
305
  };