Skip to content

Commit

Permalink
Refactor: replace dayjs with date-fns
Browse files Browse the repository at this point in the history
Because dayjs is commonjs, it is not friendly in testing & used
  • Loading branch information
Bill2015 committed Feb 21, 2024
1 parent 43747e7 commit 3b66120
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@tanstack/react-query": "^5.22.2",
"@tauri-apps/api": "^1.5.3",
"clsx": "^2.1.0",
"date-fns": "^3.3.1",
"dayjs": "^1.11.10",
"embla-carousel-react": "^7.1.0",
"i18next": "^23.9.0",
Expand Down
39 changes: 27 additions & 12 deletions src/utils/date.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
import * as dayjs from 'dayjs';
import * as relativeTime from 'dayjs/plugin/relativeTime';
// related issues: https://github.com/iamkun/dayjs/issues/475#issuecomment-1278002092
dayjs.extend(relativeTime);
import {
format,
parseISO,
differenceInYears,
differenceInMonths,
differenceInWeeks,
differenceInDays,
differenceInHours,
differenceInMinutes,
differenceInSeconds,
} from 'date-fns';

export type DateTimeUnit = Extract<dayjs.OpUnitType, 'years' | 'months' | 'weeks' | 'days' | 'hours' | 'minutes' | 'seconds'>;

const DATE_STAMPS: DateTimeUnit[] = ['years', 'months', 'weeks', 'days', 'hours', 'minutes', 'seconds'];
export type DateTimeUnit = 'years' | 'months' | 'weeks' | 'days' | 'hours' | 'minutes' | 'seconds';

export type DateTimeInterval = { value: number, stamp: DateTimeUnit };

const DIFFS: { diffn: typeof differenceInYears, stamp: DateTimeUnit }[] = [
{ diffn: differenceInYears, stamp: 'years' },
{ diffn: differenceInMonths, stamp: 'months' },
{ diffn: differenceInWeeks, stamp: 'weeks' },
{ diffn: differenceInDays, stamp: 'days' },
{ diffn: differenceInHours, stamp: 'hours' },
{ diffn: differenceInMinutes, stamp: 'minutes' },
{ diffn: differenceInSeconds, stamp: 'seconds' },
];

/**
* Get DateTime interval \
* For examples:
Expand All @@ -23,11 +38,11 @@ export type DateTimeInterval = { value: number, stamp: DateTimeUnit };
* @param targetDate for diff datetime
* @returns largest time interval */
export function getDateTimeInterval(targetDate: string): DateTimeInterval | null {
const current = dayjs();
const target = dayjs(targetDate);
const current = new Date();
const target = parseISO(targetDate);

for (const stamp of DATE_STAMPS) {
const value = current.diff(target, stamp);
for (const { diffn, stamp } of DIFFS) {
const value = diffn(current, target);
if (value > 0) {
return { value, stamp };
}
Expand All @@ -45,5 +60,5 @@ export function getDateTimeInterval(targetDate: string): DateTimeInterval | null
* @param str target string
* @returns new formated datetime */
export function formatDateTime(str: string): string {
return dayjs(str).format('YYYY-MM-DD HH:mm:ss');
return format(parseISO(str), 'yyyy-MM-dd HH:mm:ss');
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
],
"allowJs": false,
"skipLibCheck": true,
"esModuleInterop": false,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
Expand Down

0 comments on commit 3b66120

Please sign in to comment.