-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
3 changed files
with
120 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,29 @@ | ||
export default (o, c, d) => { | ||
const LT = 'h:mm A' | ||
const L = 'MM/DD/YYYY' | ||
const calendarFormat = { | ||
lastDay: `[Yesterday at] ${LT}`, | ||
sameDay: `[Today at] ${LT}`, | ||
nextDay: `[Tomorrow at] ${LT}`, | ||
nextWeek: `dddd [at] ${LT}`, | ||
lastWeek: `[Last] dddd [at] ${LT}`, | ||
sameElse: L | ||
} | ||
const proto = c.prototype | ||
proto.calendar = function (referenceTime, formats) { | ||
const format = formats || calendarFormat | ||
const referenceStartOfDay = d(referenceTime || undefined).startOf('d') | ||
const diff = this.diff(referenceStartOfDay, 'd', true) | ||
const sameElse = 'sameElse' | ||
/* eslint-disable no-nested-ternary */ | ||
const retVal = diff < -6 ? sameElse : | ||
diff < -1 ? 'lastWeek' : | ||
diff < 0 ? 'lastDay' : | ||
diff < 1 ? 'sameDay' : | ||
diff < 2 ? 'nextDay' : | ||
diff < 7 ? 'nextWeek' : sameElse | ||
/* eslint-enable no-nested-ternary */ | ||
return this.format(format[retVal] || calendarFormat[retVal]) | ||
} | ||
} | ||
|
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,81 @@ | ||
import MockDate from 'mockdate' | ||
import moment from 'moment' | ||
import dayjs from '../../src' | ||
import calendar from '../../src/plugin/calendar' | ||
|
||
dayjs.extend(calendar) | ||
|
||
beforeEach(() => { | ||
MockDate.set(new Date()) | ||
}) | ||
|
||
afterEach(() => { | ||
MockDate.reset() | ||
}) | ||
|
||
it('No argument && null && undefined', () => { | ||
expect(dayjs().calendar()).toEqual(moment().calendar()) | ||
expect(dayjs().calendar(null)).toEqual(moment().calendar(null)) | ||
expect(dayjs().calendar(undefined)).toEqual(moment().calendar(undefined)) | ||
}) | ||
|
||
it('ReferenceTime', () => { | ||
const now = '2015-01-15T14:21:22.000Z' | ||
const dates = [ | ||
{ | ||
name: 'nextDay', | ||
date: '2015-01-14T11:23:55.000Z', | ||
result: 'Tomorrow' | ||
}, | ||
{ | ||
name: 'sameDay', | ||
date: '2015-01-15T11:23:55.000Z', | ||
result: 'Today' | ||
}, | ||
{ | ||
name: 'nextWeek', | ||
date: '2015-01-09T11:23:55.000Z', | ||
result: 'Thursday' | ||
}, | ||
{ | ||
name: 'lastDay', | ||
date: '2015-01-16T11:23:55.000Z', | ||
result: 'Yesterday' | ||
}, | ||
{ | ||
name: 'lastWeek', | ||
date: '2015-01-21T11:23:55.000Z', | ||
result: 'Last' | ||
}, | ||
{ | ||
name: 'sameElse', | ||
date: '2015-01-01T11:23:55.000Z', | ||
result: '01/15/2015' | ||
}, | ||
{ | ||
name: 'sameElse', | ||
date: '2015-02-21T11:23:55.000Z', | ||
result: '01/15/2015' | ||
} | ||
] | ||
dates.forEach((d) => { | ||
const dayjsResult = dayjs(now).calendar(d.date) | ||
const momentjsResult = moment(now).calendar(d.date) | ||
expect(dayjsResult) | ||
.toEqual(momentjsResult) | ||
expect(dayjsResult.indexOf(d.result) > -1) | ||
.toBe(true) | ||
}) | ||
}) | ||
|
||
it('Custom format', () => { | ||
const format = { | ||
sameDay: '[sameDay]', | ||
sameElse: '[sameElse]' | ||
} | ||
expect(dayjs().calendar(null, format)).toEqual(moment().calendar(null, format)) | ||
const now = '2015-01-15T14:21:22.000Z' | ||
const nextDayWithoutFormat = '2015-01-14T11:23:55.000Z' | ||
expect(dayjs(now).calendar(nextDayWithoutFormat, format)) | ||
.toEqual(moment(now).calendar(nextDayWithoutFormat, format)) | ||
}) |
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,10 @@ | ||
import { PluginFunc, ConfigType } from 'dayjs' | ||
|
||
declare const plugin: PluginFunc | ||
export = plugin | ||
|
||
declare module 'dayjs' { | ||
interface Dayjs { | ||
calendar(referenceTime?: ConfigType, formats?: object): string | ||
} | ||
} |
d1b9cf9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍