|
| 1 | +import { DateTime, Info } from 'luxon'; |
| 2 | + |
| 3 | +import type { GenerateConfig } from '.'; |
| 4 | + |
| 5 | +const weekDayFormatMap = { |
| 6 | + zh_CN: 'narrow', |
| 7 | + zh_TW: 'narrow', |
| 8 | +}; |
| 9 | + |
| 10 | +const weekDayLengthMap = { |
| 11 | + en_US: 2, |
| 12 | + en_GB: 2, |
| 13 | +}; |
| 14 | + |
| 15 | +/** |
| 16 | + * Normalizes part of a moment format string that should |
| 17 | + * not be escaped to a luxon compatible format string. |
| 18 | + * |
| 19 | + * @param part string |
| 20 | + * @returns string |
| 21 | + */ |
| 22 | +const normalizeFormatPart = (part: string): string => |
| 23 | + part |
| 24 | + .replace(/Y/g, 'y') |
| 25 | + .replace(/D/g, 'd') |
| 26 | + .replace(/gg/g, 'kk') |
| 27 | + .replace(/Q/g, 'q') |
| 28 | + .replace(/([Ww])o/g, 'WW'); |
| 29 | + |
| 30 | +/** |
| 31 | + * Normalizes a moment compatible format string to a luxon compatible format string |
| 32 | + * |
| 33 | + * @param format string |
| 34 | + * @returns string |
| 35 | + */ |
| 36 | +const normalizeFormat = (format: string): string => |
| 37 | + format |
| 38 | + // moment escapes strings contained in brackets |
| 39 | + .split(/[[\]]/) |
| 40 | + .map((part, index) => { |
| 41 | + const shouldEscape = index % 2 > 0; |
| 42 | + |
| 43 | + return shouldEscape ? part : normalizeFormatPart(part); |
| 44 | + }) |
| 45 | + // luxon escapes strings contained in single quotes |
| 46 | + .join("'"); |
| 47 | + |
| 48 | +/** |
| 49 | + * Normalizes language tags used to luxon compatible |
| 50 | + * language tags by replacing underscores with hyphen-minus. |
| 51 | + * |
| 52 | + * @param locale string |
| 53 | + * @returns string |
| 54 | + */ |
| 55 | +const normalizeLocale = (locale: string): string => locale.replace(/_/g, '-'); |
| 56 | + |
| 57 | +const generateConfig: GenerateConfig<DateTime> = { |
| 58 | + // get |
| 59 | + getNow: () => DateTime.local(), |
| 60 | + getFixedDate: string => DateTime.fromFormat(string, 'yyyy-MM-dd'), |
| 61 | + getEndDate: date => date.endOf('month'), |
| 62 | + getWeekDay: date => date.weekday, |
| 63 | + getYear: date => date.year, |
| 64 | + getMonth: date => date.month - 1, // getMonth should return 0-11, luxon month returns 1-12 |
| 65 | + getDate: date => date.day, |
| 66 | + getHour: date => date.hour, |
| 67 | + getMinute: date => date.minute, |
| 68 | + getSecond: date => date.second, |
| 69 | + |
| 70 | + // set |
| 71 | + addYear: (date, diff) => date.plus({ year: diff }), |
| 72 | + addMonth: (date, diff) => date.plus({ month: diff }), |
| 73 | + addDate: (date, diff) => date.plus({ day: diff }), |
| 74 | + setYear: (date, year) => date.set({ year }), |
| 75 | + setMonth: (date, month) => date.set({ month: month + 1 }), // setMonth month argument is 0-11, luxon months are 1-12 |
| 76 | + setDate: (date, day) => date.set({ day }), |
| 77 | + setHour: (date, hour) => date.set({ hour }), |
| 78 | + setMinute: (date, minute) => date.set({ minute }), |
| 79 | + setSecond: (date, second) => date.set({ second }), |
| 80 | + |
| 81 | + // Compare |
| 82 | + isAfter: (date1, date2) => date1 > date2, |
| 83 | + isValidate: date => date.isValid, |
| 84 | + |
| 85 | + locale: { |
| 86 | + getWeekFirstDate: (locale, date) => date.setLocale(normalizeLocale(locale)).startOf('week'), |
| 87 | + getWeekFirstDay: locale => |
| 88 | + DateTime.local().setLocale(normalizeLocale(locale)).startOf('week').weekday, |
| 89 | + getWeek: (locale, date) => date.setLocale(normalizeLocale(locale)).weekNumber, |
| 90 | + getShortWeekDays: locale => { |
| 91 | + const weekdays = Info.weekdays(weekDayFormatMap[locale] || 'short', { |
| 92 | + locale: normalizeLocale(locale), |
| 93 | + }); |
| 94 | + |
| 95 | + const shifted = weekdays.map(weekday => weekday.slice(0, weekDayLengthMap[locale])); |
| 96 | + |
| 97 | + // getShortWeekDays should return weekday labels starting from Sunday. |
| 98 | + // luxon returns them starting from Monday, so we have to shift the results. |
| 99 | + shifted.unshift(shifted.pop() as string); |
| 100 | + |
| 101 | + return shifted; |
| 102 | + }, |
| 103 | + getShortMonths: locale => Info.months('short', { locale: normalizeLocale(locale) }), |
| 104 | + format: (locale, date, format) => { |
| 105 | + if (!date || !date.isValid) { |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
| 109 | + return date.setLocale(normalizeLocale(locale)).toFormat(normalizeFormat(format)); |
| 110 | + }, |
| 111 | + parse: (locale, text, formats) => { |
| 112 | + for (let i = 0; i < formats.length; i += 1) { |
| 113 | + const normalizedFormat = normalizeFormat(formats[i]); |
| 114 | + |
| 115 | + const date = DateTime.fromFormat(text, normalizedFormat, { |
| 116 | + locale: normalizeLocale(locale), |
| 117 | + }); |
| 118 | + |
| 119 | + if (date.isValid) { |
| 120 | + return date; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return null; |
| 125 | + }, |
| 126 | + }, |
| 127 | +}; |
| 128 | + |
| 129 | +export default generateConfig; |
0 commit comments