generated from react-component/footer
-
-
Notifications
You must be signed in to change notification settings - Fork 318
/
dateFns.ts
143 lines (133 loc) Β· 4.06 KB
/
dateFns.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
138
139
140
141
142
143
import {
addDays,
addMonths,
addYears,
endOfMonth,
format as formatDate,
getDate,
getDay,
getHours,
getMinutes,
getMonth,
getSeconds,
getWeek,
getYear,
isAfter,
isValid,
parse as parseDate,
setDate,
setHours,
setMilliseconds,
setMinutes,
setMonth,
setSeconds,
setYear,
startOfWeek,
getMilliseconds,
type Locale,
} from 'date-fns';
import * as locales from 'date-fns/locale';
import type { GenerateConfig } from '.';
const getLocale = (locale: string): Locale => {
return (
locales[locale] || locales[locale.replace(/_/g, '')] || locales[locale.replace(/_.*$/g, '')]
);
};
const localeParse = (format: string) => {
return format
.replace(/Y/g, 'y')
.replace(/D/g, 'd')
.replace(/gggg/, 'yyyy')
.replace(/g/g, 'G')
.replace(/([Ww])o/g, 'wo');
};
const parse = (text: string, format: string, locale: string) => {
return parseDate(text, localeParse(format), new Date(), { locale: getLocale(locale) });
}
/**
* Check if the text is a valid date considering the format and locale
*
* This is a strict check, the date string must match the format exactly.
* Date-fns allows some flexibility in parsing dates, for example, it will parse "30/01/2" as "30/01/002".
* This behavior is not desirable in our case, so we need to check if the date string matches the format exactly.
*
* @param text the date string
* @param format the date format to use
* @param locale the locale to use
*/
const isStrictValidDate = (text: string, format: string, locale: string) => {
const date = parse(text, format, locale);
if (!isValid(date)) {
return false;
}
const formattedDate = formatDate(date, format, { locale: getLocale(locale) });
return text === formattedDate;
}
const generateConfig: GenerateConfig<Date> = {
// get
getNow: () => new Date(),
getFixedDate: (string) => new Date(string),
getEndDate: (date) => endOfMonth(date),
getWeekDay: (date) => getDay(date),
getYear: (date) => getYear(date),
getMonth: (date) => getMonth(date),
getDate: (date) => getDate(date),
getHour: (date) => getHours(date),
getMinute: (date) => getMinutes(date),
getSecond: (date) => getSeconds(date),
getMillisecond: (date) => getMilliseconds(date),
// set
addYear: (date, diff) => addYears(date, diff),
addMonth: (date, diff) => addMonths(date, diff),
addDate: (date, diff) => addDays(date, diff),
setYear: (date, year) => setYear(date, year),
setMonth: (date, month) => setMonth(date, month),
setDate: (date, num) => setDate(date, num),
setHour: (date, hour) => setHours(date, hour),
setMinute: (date, minute) => setMinutes(date, minute),
setSecond: (date, second) => setSeconds(date, second),
setMillisecond: (date, millisecond) => setMilliseconds(date, millisecond),
// Compare
isAfter: (date1, date2) => isAfter(date1, date2),
isValidate: (date) => isValid(date),
locale: {
getWeekFirstDay: (locale) => {
const clone = getLocale(locale);
return clone.options.weekStartsOn;
},
getWeekFirstDate: (locale, date) => {
return startOfWeek(date, { locale: getLocale(locale) });
},
getWeek: (locale, date) => {
return getWeek(date, { locale: getLocale(locale) });
},
getShortWeekDays: (locale) => {
const clone = getLocale(locale);
return Array.from({ length: 7 }).map((_, i) => clone.localize.day(i, { width: 'short' }));
},
getShortMonths: (locale) => {
const clone = getLocale(locale);
return Array.from({ length: 12 }).map((_, i) =>
clone.localize.month(i, { width: 'abbreviated' }),
);
},
format: (locale, date, format) => {
if (!isValid(date)) {
return null;
}
return formatDate(date, localeParse(format), {
locale: getLocale(locale),
});
},
parse: (locale, text, formats) => {
for (let i = 0; i < formats.length; i += 1) {
const format = localeParse(formats[i]);
if (isStrictValidDate(text, format, locale)) {
return parse(text, format, locale);
}
}
return null;
},
},
};
export default generateConfig;