-
Notifications
You must be signed in to change notification settings - Fork 360
Expand file tree
/
Copy pathutils.ts
More file actions
545 lines (480 loc) · 14.9 KB
/
utils.ts
File metadata and controls
545 lines (480 loc) · 14.9 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
import { chunk, isFunction } from 'lodash-es';
import dayjs from 'dayjs';
import dayJsIsBetween from 'dayjs/plugin/isBetween';
import weekOfYear from 'dayjs/plugin/weekOfYear';
import weekYear from 'dayjs/plugin/weekYear';
import localeData from 'dayjs/plugin/localeData';
import quarterOfYear from 'dayjs/plugin/quarterOfYear';
import advancedFormat from 'dayjs/plugin/advancedFormat';
import customParseFormat from 'dayjs/plugin/customParseFormat';
import { parseToDayjs } from './format';
dayjs.extend(weekOfYear);
dayjs.extend(weekYear);
dayjs.extend(localeData);
dayjs.extend(quarterOfYear);
dayjs.extend(advancedFormat);
dayjs.extend(customParseFormat);
dayjs.extend(dayJsIsBetween);
/**
* 首字母大写
* @param {String} str 目标字符串
* @returns {String}
*/
export function firstUpperCase(str: string): string {
if (!str) return str;
return str[0].toUpperCase().concat(str.substring(1, str.length));
}
interface DateObj {
year: number;
month: number;
}
/**
* 返回指定年、月的第一天日期
* @param {Object} { year, month }
* @returns {Date}
*/
function getFirstDayOfMonth({ year, month }: DateObj): Date {
return new Date(year, month, 1);
}
/**
* 返回指定年、月的天数
* @param {Object} { year, month }
* @returns {Number}
*/
function getDaysInMonth({ year, month }: DateObj): number {
return new Date(year, month + 1, 0).getDate();
}
/**
* 返回指定年、月的最后一天日期
* @param {Object} { year, month }
* @returns {Date}
*/
function getLastDayOfMonth({ year, month }: DateObj): Date {
return new Date(year, month, getDaysInMonth({ year, month }));
}
function isSameYear(date1: Date, date2: Date): boolean {
return date1.getFullYear() === date2.getFullYear();
}
function isSameQuarter(date1: Date, date2: Date): boolean {
return isSameYear(date1, date2) && dayjs(date1).quarter() === dayjs(date2).quarter();
}
function isSameMonth(date1: Date, date2: Date): boolean {
return isSameYear(date1, date2) && date1.getMonth() === date2.getMonth();
}
function isSameWeek(date1: Date, date2: Date): boolean {
return (
isSameMonth(date1, date2) && dayjs(date1).week() === dayjs(date2).week()
);
}
function isSameDate(date1: Date, date2: Date): boolean {
return isSameMonth(date1, date2) && date1.getDate() === date2.getDate();
}
/**
* 比较两个日期对象的时间戳
* @param {Date} date1 日期1
* @param {Date} date2 日期2
* @returns {Number} 返回 date1.getTime() - date2.getTime() 的差值
*/
function compareAsc(date1: { getTime: () => any }, date2: Date): number {
const d1 = date1.getTime();
const d2 = date2.getTime();
if (d1 < d2) return -1;
if (d1 > d2) return 1;
return 0;
}
/**
* 比较两个 Date 是否是同一天 或则 同一月 或则 同一年
* @param {Date} date1 比较的日期
* @param {Date} date2 比较的日期
* @param {String} type 比较类型,默认比较到『日』 date|month|year
* @returns {Boolean}
*/
export function isSame(date1: Date, date2: Date, type = 'date'): boolean {
const func = {
isSameYear,
isSameQuarter,
isSameMonth,
isSameWeek,
isSameDate,
} as Record<string, (date1: Date, date2: Date) => boolean>;
return func[`isSame${firstUpperCase(type)}`](date1, date2);
}
export function outOfRanges(d: Date, min: any, max: any) {
return (min && compareAsc(d, min) === -1) || (max && compareAsc(d, max) === 1);
}
/**
* @returns {Date} 当天零点的日期对象
*/
export function getToday(): Date {
const now = new Date();
return new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0);
}
/**
* 返回日期对象的年、月、日、小时、分钟、秒、12小时制标识
* @param {Date} date
* @returns {Object}
*/
export function getDateObj(date: Date) {
let tempDate = date;
if (!(date instanceof Date)) {
tempDate = getToday();
}
return {
year: tempDate.getFullYear(),
month: tempDate.getMonth(),
date: tempDate.getDate(),
hours: tempDate.getHours(),
minutes: tempDate.getMinutes(),
seconds: tempDate.getSeconds(),
milliseconds: tempDate.getMilliseconds(),
meridiem: tempDate.getHours() > 11 ? 'PM' : 'AM',
};
}
/**
* 设置日期对象的时间部分
* @param {Date} date 日期
* @param {Number} hours 小时
* @param {Number} minutes 分钟
* @param {Number} seconds 秒
* @param {Number} milliseconds 毫秒
* @returns {Date} 一个新的date
*/
export function setDateTime(
date: Date,
hours: number,
minutes: number,
seconds: number,
milliseconds?: number
): Date {
return dayjs(date)
.hour(hours)
.minute(minutes)
.second(seconds)
.millisecond(milliseconds)
.toDate();
}
/**
* 减少月份
* @param {Date} date 起始日期
* @param {Number} num 月份数
* @returns {Date}
*/
export function subtractMonth(date: Date, num: number): Date {
return dayjs(date).subtract(num, 'month').toDate();
}
/**
* 增加月份
* @param {Date} date 起始日期
* @param {Number} num 月份数
* @returns {Date}
*/
export function addMonth(date: Date, num: number): Date {
return dayjs(date).add(num, 'month').toDate();
}
export type DateValue = string | Date | number;
export interface DisableDateObj { from?: string; to?: string; before?: string; after?: string }
export type DisableDate = Array<DateValue> | DisableDateObj | ((date: DateValue) => boolean);
export interface OptionsType {
firstDayOfWeek: number;
disableDate: DisableDate;
minDate: Date;
maxDate: Date;
showWeekOfYear?: Boolean;
monthLocal?: string[];
quarterLocal?: string[];
cancelRangeSelectLimit?: boolean;
}
export function getWeeks(
{ year, month }: { year: number; month: number },
{
firstDayOfWeek,
showWeekOfYear = false,
disableDate = () => false,
minDate,
maxDate,
cancelRangeSelectLimit = false,
}: OptionsType
) {
const prependDay = getFirstDayOfMonth({ year, month });
const appendDay = getLastDayOfMonth({ year, month });
const maxDays = getDaysInMonth({ year, month });
const daysArr = [];
let i = 1;
const today = getToday();
for (i; i <= maxDays; i++) {
const currentDay = new Date(year, month, i);
daysArr.push({
text: i,
active: false,
value: currentDay,
disabled:
(isFunction(disableDate) && disableDate(currentDay))
|| (!cancelRangeSelectLimit && outOfRanges(currentDay, minDate, maxDate)),
now: isSame(today, currentDay),
firstDayOfMonth: i === 1,
lastDayOfMonth: i === maxDays,
type: 'current-month',
dayjsObj: dayjs(currentDay),
});
}
if (prependDay.getDay() !== firstDayOfWeek) {
prependDay.setDate(0); // 上一月
while (true) {
daysArr.unshift({
text: prependDay.getDate().toString(),
active: false,
value: new Date(prependDay),
disabled: (isFunction(disableDate) && disableDate(prependDay)) || (!cancelRangeSelectLimit && outOfRanges(prependDay, minDate, maxDate)),
additional: true, // 非当前月
type: 'prev-month',
dayjsObj: dayjs(prependDay),
});
prependDay.setDate(prependDay.getDate() - 1);
if (prependDay.getDay() === Math.abs(firstDayOfWeek + 6) % 7) break;
}
}
const LEN = 42; // 显示6周
while (daysArr.length < LEN) {
appendDay.setDate(appendDay.getDate() + 1);
daysArr.push({
text: appendDay.getDate(),
active: false,
value: new Date(appendDay),
disabled: (isFunction(disableDate) && disableDate(appendDay)) || (!cancelRangeSelectLimit && outOfRanges(appendDay, minDate, maxDate)),
additional: true, // 非当前月
type: 'next-month',
dayjsObj: dayjs(appendDay),
});
}
const dataList = chunk(daysArr, 7);
// 显示周数
if (showWeekOfYear) {
dataList.forEach((d) => {
d.unshift({
...d[0],
active: false,
value: d[0].value,
text: dayjs(d[0].value).week(),
dayjsObj: dayjs(d[0].value),
});
});
}
return dataList;
}
export function getQuarters(
year: number,
{
disableDate = () => false,
minDate,
maxDate,
quarterLocal,
cancelRangeSelectLimit = false,
}: OptionsType
) {
const quarterArr = [];
const today = getToday();
for (let i = 1; i <= 4; i++) {
const date = dayjs(new Date(year, 0)).quarter(i).toDate();
quarterArr.push({
value: date,
now: isSame(date, today, 'quarter'),
disabled: (isFunction(disableDate) && disableDate(date)) || (!cancelRangeSelectLimit && outOfRanges(date, minDate, maxDate)),
active: false,
text: quarterLocal[i - 1],
dayjsObj: dayjs(date),
});
}
return chunk(quarterArr, 4);
}
export function getYears(
year: number,
{
disableDate = () => false,
minDate,
maxDate,
cancelRangeSelectLimit = false,
}: OptionsType
) {
const startYear = parseInt((year / 10).toString(), 10) * 10;
const endYear = startYear + 9;
const yearArr = [];
const today = getToday();
for (let i = startYear; i <= endYear; i++) {
const date = new Date(i, 0);
yearArr.push({
value: date,
now: isSame(date, today, 'year'),
disabled: (isFunction(disableDate) && disableDate(date)) || (!cancelRangeSelectLimit && outOfRanges(date, minDate, maxDate)),
active: false,
text: `${date.getFullYear()}`,
dayjsObj: dayjs(date),
});
}
return chunk(yearArr, 3);
}
export function getMonths(year: number, params: OptionsType) {
const {
disableDate = () => false, minDate, maxDate, monthLocal, cancelRangeSelectLimit = false,
} = params;
const MonthArr = [];
const today = getToday();
for (let i = 0; i <= 11; i++) {
const date = new Date(year, i);
MonthArr.push({
value: date,
now: isSame(date, today, 'month'),
disabled: (isFunction(disableDate) && disableDate(date)) || (!cancelRangeSelectLimit && outOfRanges(date, minDate, maxDate)),
active: false,
text: monthLocal[date.getMonth()], // `${date.getMonth() + 1} ${monthText || '月'}`,
dayjsObj: dayjs(date),
});
}
return chunk(MonthArr, 3);
}
export interface DateTime {
additional: boolean;
active: boolean;
highlight: boolean;
hoverHighlight: boolean;
startOfRange: boolean;
endOfRange: boolean;
hoverStartOfRange: boolean;
hoverEndOfRange: boolean;
value: Date;
}
interface FlagActiveOptions {
start: Date;
end: Date;
hoverStart: Date;
hoverEnd: Date;
type: any;
isRange: boolean;
value: DateValue | DateValue[];
multiple: boolean;
}
export function flagActive(data: any[], { ...args }: FlagActiveOptions) {
const {
start,
end,
hoverStart,
hoverEnd,
type = 'date',
isRange = false,
value,
multiple = false,
} = args;
// 周选择器不更改 cell 样式
if (type === 'week') return data;
if (!isRange) {
return data.map((row: any[]) => row.map((item: DateTime) => {
const _item = item;
if (multiple) {
_item.active = (value as DateValue[])?.some?.((val) => isSame(dayjs(val).toDate(), _item.value, type) && !_item.additional);
} else {
_item.active = start && isSame(item.value, start, type) && !_item.additional;
}
return _item;
}));
}
return data.map((row: any[]) => row.map((item: DateTime) => {
const _item = item;
const date = item.value;
const isStart = start && isSame(start, date, type);
const isHoverStart = hoverStart && isSame(hoverStart, date, type);
const isEnd = end && isSame(end, date, type);
const isHoverEnd = hoverEnd && isSame(hoverEnd, date, type);
_item.active = (isStart || isEnd) && !_item.additional;
if (start && end) {
_item.highlight = dayjs(date).isBetween(start, end, type, '[]') && !_item.additional;
_item.startOfRange = isStart;
_item.endOfRange = isEnd;
}
if (hoverStart && hoverEnd) {
_item.hoverHighlight = dayjs(date).isBetween(hoverStart, hoverEnd, type, '[]') && !_item.additional;
_item.hoverStartOfRange = isHoverStart;
_item.hoverEndOfRange = isHoverEnd;
}
return _item;
}));
}
/**
* 返回时间对象的小时、分钟、秒、12小时制标识
* @param {String} timeFormat 'pm 20:11:11:333'
* @returns {Object}
*/
export function extractTimeObj(timeFormat: string = '') {
const matchedMeridiem = timeFormat.match(/[ap]m/i) || [''];
const timeReg = /\d{1,2}(:\d{1,2})?(:\d{1,2})?(:\d{1,3})?/;
const matchedTimeStr = timeFormat.match(timeReg) || ['0:0:0:0'];
const [hours = 0, minutes = 0, seconds = 0, milliseconds = 0] = matchedTimeStr[0].split(':');
return {
hours: +hours,
minutes: +minutes,
seconds: +seconds,
milliseconds: +milliseconds,
meridiem: matchedMeridiem[0],
};
}
/**
* 日期是否可用
* @param {Object} { value, disableDate, mode, format }
* @returns {Boolean}
*/
export function isEnabledDate({
value,
disableDate,
mode,
format,
}: {
value: Date;
mode: 'year' | 'month' | 'date' | 'quarter' | 'week';
format: string;
disableDate: any;
}): boolean {
if (!disableDate) return true;
const availableMode = mode === 'quarter' ? 'date' : mode;
let isEnabled = true;
// 值类型为 Function 则表示返回值为 true 的日期会被禁用
if (isFunction(disableDate)) {
return !disableDate(value);
}
// 禁用日期,示例:['A', 'B'] 表示日期 A 和日期 B 会被禁用。
if (Array.isArray(disableDate)) {
const formattedDisabledDate = disableDate.map((item: string) => parseToDayjs(item, format));
// eslint-disable-next-line
const isIncludes = formattedDisabledDate.some(item => item.isSame(dayjs(value)));
return !isIncludes;
}
// { from: 'A', to: 'B' } 表示在 A 到 B 之间的日期会被禁用(包括A和B)。
// eslint-disable-next-line
const { from, to, before, after } = disableDate;
if (from && to) {
const compareMin = dayjs(from).startOf('day');
const compareMax = dayjs(to).endOf('day');
return !dayjs(value).isBetween(compareMin, compareMax, availableMode, '[]');
}
// 最小时间与最大时间的边界,防止正负时区出现禁用时间不一致的情况
const min = before ? new Date(dayjs(before).startOf('day').format()) : null;
const max = after ? new Date(dayjs(after).endOf('day').format()) : null;
// { before: 'A', after: 'B' } 表示在 A 之前和在 B 之后的日期都会被禁用。
if (max && min) {
const compareMin = dayjs(new Date(min));
const compareMax = dayjs(new Date(max));
isEnabled = dayjs(value).isBetween(compareMin, compareMax, availableMode, '[]');
} else if (min) {
const compareMin = dayjs(new Date(min));
isEnabled = !dayjs(value).isBefore(compareMin, availableMode);
} else if (max) {
const compareMax = dayjs(new Date(max));
isEnabled = !dayjs(value).isAfter(compareMax, availableMode);
}
return isEnabled;
}
/**
* formatDate 方法需要date作为入参,部分场景需要将timestamp或格式化后的时间string转换为date进行使用
*/
export function covertToDate(value: string, valueType: string) {
return valueType === 'time-stamp'
? new Date(value)
: dayjs(value, valueType).toDate();
}