From 64143957113ec9287be778353ecba1948bc4c043 Mon Sep 17 00:00:00 2001 From: iamkun Date: Mon, 15 Apr 2019 12:16:45 +0800 Subject: [PATCH] fix: Add weekday (locale aware day of week) plugin fix #559 --- src/plugin/weekday/index.js | 13 +++++++++++ test/plugin/weekday.test.js | 43 +++++++++++++++++++++++++++++++++++++ types/plugin/weekday.d.ts | 12 +++++++++++ 3 files changed, 68 insertions(+) create mode 100644 src/plugin/weekday/index.js create mode 100644 test/plugin/weekday.test.js create mode 100644 types/plugin/weekday.d.ts diff --git a/src/plugin/weekday/index.js b/src/plugin/weekday/index.js new file mode 100644 index 000000000..c3c86ef9b --- /dev/null +++ b/src/plugin/weekday/index.js @@ -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') + } +} + diff --git a/test/plugin/weekday.test.js b/test/plugin/weekday.test.js new file mode 100644 index 000000000..703f3ec84 --- /dev/null +++ b/test/plugin/weekday.test.js @@ -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()) +}) diff --git a/types/plugin/weekday.d.ts b/types/plugin/weekday.d.ts new file mode 100644 index 000000000..87a8025a3 --- /dev/null +++ b/types/plugin/weekday.d.ts @@ -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 + } +}