Skip to content

Commit d1b9cf9

Browse files
committed
fix: Add Calendar plugin
1 parent 71e4bff commit d1b9cf9

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

src/plugin/calendar/index.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export default (o, c, d) => {
2+
const LT = 'h:mm A'
3+
const L = 'MM/DD/YYYY'
4+
const calendarFormat = {
5+
lastDay: `[Yesterday at] ${LT}`,
6+
sameDay: `[Today at] ${LT}`,
7+
nextDay: `[Tomorrow at] ${LT}`,
8+
nextWeek: `dddd [at] ${LT}`,
9+
lastWeek: `[Last] dddd [at] ${LT}`,
10+
sameElse: L
11+
}
12+
const proto = c.prototype
13+
proto.calendar = function (referenceTime, formats) {
14+
const format = formats || calendarFormat
15+
const referenceStartOfDay = d(referenceTime || undefined).startOf('d')
16+
const diff = this.diff(referenceStartOfDay, 'd', true)
17+
const sameElse = 'sameElse'
18+
/* eslint-disable no-nested-ternary */
19+
const retVal = diff < -6 ? sameElse :
20+
diff < -1 ? 'lastWeek' :
21+
diff < 0 ? 'lastDay' :
22+
diff < 1 ? 'sameDay' :
23+
diff < 2 ? 'nextDay' :
24+
diff < 7 ? 'nextWeek' : sameElse
25+
/* eslint-enable no-nested-ternary */
26+
return this.format(format[retVal] || calendarFormat[retVal])
27+
}
28+
}
29+

test/plugin/calendar.test.js

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import MockDate from 'mockdate'
2+
import moment from 'moment'
3+
import dayjs from '../../src'
4+
import calendar from '../../src/plugin/calendar'
5+
6+
dayjs.extend(calendar)
7+
8+
beforeEach(() => {
9+
MockDate.set(new Date())
10+
})
11+
12+
afterEach(() => {
13+
MockDate.reset()
14+
})
15+
16+
it('No argument && null && undefined', () => {
17+
expect(dayjs().calendar()).toEqual(moment().calendar())
18+
expect(dayjs().calendar(null)).toEqual(moment().calendar(null))
19+
expect(dayjs().calendar(undefined)).toEqual(moment().calendar(undefined))
20+
})
21+
22+
it('ReferenceTime', () => {
23+
const now = '2015-01-15T14:21:22.000Z'
24+
const dates = [
25+
{
26+
name: 'nextDay',
27+
date: '2015-01-14T11:23:55.000Z',
28+
result: 'Tomorrow'
29+
},
30+
{
31+
name: 'sameDay',
32+
date: '2015-01-15T11:23:55.000Z',
33+
result: 'Today'
34+
},
35+
{
36+
name: 'nextWeek',
37+
date: '2015-01-09T11:23:55.000Z',
38+
result: 'Thursday'
39+
},
40+
{
41+
name: 'lastDay',
42+
date: '2015-01-16T11:23:55.000Z',
43+
result: 'Yesterday'
44+
},
45+
{
46+
name: 'lastWeek',
47+
date: '2015-01-21T11:23:55.000Z',
48+
result: 'Last'
49+
},
50+
{
51+
name: 'sameElse',
52+
date: '2015-01-01T11:23:55.000Z',
53+
result: '01/15/2015'
54+
},
55+
{
56+
name: 'sameElse',
57+
date: '2015-02-21T11:23:55.000Z',
58+
result: '01/15/2015'
59+
}
60+
]
61+
dates.forEach((d) => {
62+
const dayjsResult = dayjs(now).calendar(d.date)
63+
const momentjsResult = moment(now).calendar(d.date)
64+
expect(dayjsResult)
65+
.toEqual(momentjsResult)
66+
expect(dayjsResult.indexOf(d.result) > -1)
67+
.toBe(true)
68+
})
69+
})
70+
71+
it('Custom format', () => {
72+
const format = {
73+
sameDay: '[sameDay]',
74+
sameElse: '[sameElse]'
75+
}
76+
expect(dayjs().calendar(null, format)).toEqual(moment().calendar(null, format))
77+
const now = '2015-01-15T14:21:22.000Z'
78+
const nextDayWithoutFormat = '2015-01-14T11:23:55.000Z'
79+
expect(dayjs(now).calendar(nextDayWithoutFormat, format))
80+
.toEqual(moment(now).calendar(nextDayWithoutFormat, format))
81+
})

types/plugin/calendar.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { PluginFunc, ConfigType } from 'dayjs'
2+
3+
declare const plugin: PluginFunc
4+
export = plugin
5+
6+
declare module 'dayjs' {
7+
interface Dayjs {
8+
calendar(referenceTime?: ConfigType, formats?: object): string
9+
}
10+
}

0 commit comments

Comments
 (0)