-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`formatDistanceAgo 表现正常: 分 1`] = `"4分钟前"`; | ||
|
||
exports[`formatDistanceAgo 表现正常: 天 1`] = `"10天前"`; | ||
|
||
exports[`formatDistanceAgo 表现正常: 年 1`] = `"6年前"`; | ||
|
||
exports[`formatDistanceAgo 表现正常: 时 1`] = `"1小时前"`; | ||
|
||
exports[`formatDistanceAgo 表现正常: 月 1`] = `"3个月前"`; | ||
|
||
exports[`formatDistanceAgo 表现正常: 秒 1`] = `"20秒前"`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { formatDistanceAgo } from './formatDistanceAgo' | ||
import { | ||
subDays, | ||
subHours, | ||
subMinutes, | ||
subMonths, | ||
subSeconds, | ||
subYears, | ||
} from 'date-fns/esm' | ||
|
||
describe('formatDistanceAgo', () => { | ||
test('表现正常', () => { | ||
expect(formatDistanceAgo(subYears(new Date(), 6))).toMatchSnapshot('年') | ||
expect(formatDistanceAgo(subMonths(new Date(), 3))).toMatchSnapshot('月') | ||
expect(formatDistanceAgo(subDays(new Date(), 10))).toMatchSnapshot('天') | ||
expect(formatDistanceAgo(subHours(new Date(), 1))).toMatchSnapshot('时') | ||
expect(formatDistanceAgo(subMinutes(new Date(), 4))).toMatchSnapshot('分') | ||
expect(formatDistanceAgo(subSeconds(new Date(), 20))).toMatchSnapshot('秒') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { formatDistanceToNowStrict } from 'date-fns/esm' | ||
import { zhCN } from 'date-fns/esm/locale' | ||
|
||
/** | ||
* 将时间转化为 `xxx前` 表示。 | ||
* | ||
* @param date 时间 | ||
*/ | ||
export function formatDistanceAgo(date: number | Date): string { | ||
const distance = formatDistanceToNowStrict(date, { | ||
locale: zhCN, | ||
}) | ||
return `${distance.replace(/\s+/g, '')}前` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters