-
-
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.
fix: Add weekday (locale aware day of week) plugin
fix #559
- Loading branch information
Showing
3 changed files
with
68 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 @@ | ||
export default (o, c) => { | ||
const proto = c.prototype | ||
proto.weekday = function (input) { | ||
const weekStart = this.$locale().weekStart || 0 | ||
const { $W } = this | ||
const weekday = ($W < weekStart ? $W + 7 : $W) - weekStart | ||
if (this.$utils().u(input)) { | ||
return weekday | ||
} | ||
return this.subtract(weekday, 'day').add(input, 'day') | ||
} | ||
} | ||
|
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,43 @@ | ||
import MockDate from 'mockdate' | ||
import moment from 'moment' | ||
import dayjs from '../../src' | ||
import weekday from '../../src/plugin/weekday' | ||
import '../../src/locale/zh-cn' | ||
import '../../src/locale/ar' | ||
|
||
dayjs.extend(weekday) | ||
|
||
beforeEach(() => { | ||
MockDate.set(new Date()) | ||
}) | ||
|
||
afterEach(() => { | ||
MockDate.reset() | ||
moment.locale('en') | ||
dayjs.locale('en') | ||
}) | ||
|
||
it('Sunday is the first day of the week', () => { | ||
expect(dayjs().weekday()).toBe(moment().weekday()) | ||
expect(dayjs().weekday(0).date()).toBe(moment().weekday(0).date()) | ||
expect(dayjs().weekday(-7).format()).toBe(moment().weekday(-7).format()) | ||
expect(dayjs().weekday(7).format()).toBe(moment().weekday(7).format()) | ||
}) | ||
|
||
it('Monday is the first day of the week', () => { | ||
moment.locale('zh-cn') | ||
dayjs.locale('zh-cn') | ||
expect(dayjs().weekday()).toBe(moment().weekday()) | ||
expect(dayjs().weekday(0).date()).toBe(moment().weekday(0).date()) | ||
expect(dayjs().weekday(-7).format()).toBe(moment().weekday(-7).format()) | ||
expect(dayjs().weekday(7).format()).toBe(moment().weekday(7).format()) | ||
}) | ||
|
||
it('Saturday is the first day of the week', () => { | ||
moment.locale('ar') | ||
dayjs.locale('ar') | ||
expect(dayjs().weekday()).toBe(moment().weekday()) | ||
expect(dayjs().weekday(0).date()).toBe(moment().weekday(0).date()) | ||
expect(dayjs().weekday(-7).valueOf()).toBe(moment().weekday(-7).valueOf()) | ||
expect(dayjs().weekday(7).valueOf()).toBe(moment().weekday(7).valueOf()) | ||
}) |
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,12 @@ | ||
import { PluginFunc } from 'dayjs' | ||
|
||
declare const plugin: PluginFunc | ||
export = plugin | ||
|
||
declare module 'dayjs' { | ||
interface Dayjs { | ||
weekday(): number | ||
|
||
weekday(value: number): Dayjs | ||
} | ||
} |