From 5eaf77bd190cba4aaac729179f8b335f94d401e0 Mon Sep 17 00:00:00 2001 From: iamkun Date: Mon, 11 Feb 2019 15:24:27 +0800 Subject: [PATCH] fix: Fix startOf week bug while week start is not Sunday --- src/index.js | 10 +++++----- src/locale/ar.js | 1 + test/manipulate.test.js | 13 +++++++++---- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/index.js b/src/index.js index f0f5fc1db..49e5e440a 100644 --- a/src/index.js +++ b/src/index.js @@ -163,18 +163,18 @@ class Dayjs { (isStartOf ? argumentStart : argumentEnd).slice(slice) ), this) } - + const { $W, $M, $D } = this switch (unit) { case C.Y: return isStartOf ? instanceFactory(1, 0) : instanceFactory(31, 11) case C.M: - return isStartOf ? instanceFactory(1, this.$M) : - instanceFactory(0, this.$M + 1) + return isStartOf ? instanceFactory(1, $M) : + instanceFactory(0, $M + 1) case C.W: { const weekStart = this.$locale().weekStart || 0 - return isStartOf ? instanceFactory(this.$D - (this.$W - weekStart), this.$M) : - instanceFactory(this.$D + (6 - (this.$W - weekStart)), this.$M) + const gap = ($W < weekStart ? $W + 7 : $W) - weekStart + return instanceFactory(isStartOf ? $D - gap : $D + (6 - gap), $M) } case C.D: case C.DATE: diff --git a/src/locale/ar.js b/src/locale/ar.js index 545db4b5b..785341280 100644 --- a/src/locale/ar.js +++ b/src/locale/ar.js @@ -4,6 +4,7 @@ const locale = { name: 'ar', weekdays: 'الأحد_الإثنين_الثلاثاء_الأربعاء_الخميس_الجمعة_السبت'.split('_'), months: 'يناير_فبراير_مارس_أبريل_مايو_يونيو_يوليو_أغسطس_سبتمبر_أكتوبر_نوفمبر_ديسمبر'.split('_'), + weekStart: 6, relativeTime: { future: 'بعد %s', past: 'منذ %s', diff --git a/test/manipulate.test.js b/test/manipulate.test.js index ae0c8fec9..c944027f7 100644 --- a/test/manipulate.test.js +++ b/test/manipulate.test.js @@ -32,10 +32,15 @@ describe('StartOf EndOf', () => { }) it('StartOf week with locale', () => { - const testArr = ['zh-cn'] - testArr.forEach((l) => { - expect(dayjs().locale(l).startOf('week').date()).toBe(moment().locale(l).startOf('week').date()) - expect(dayjs().locale(l).endOf('week').date()).toBe(moment().locale(l).endOf('week').date()) + const testDate = [undefined, '2019-02-10', '2019-02-11', '2019-02-12', '2019-02-13', '2019-02-14', '2019-02-15', '2019-02-16'] + const testLocale = ['zh-cn', 'ar', 'en'] + testDate.forEach((d) => { + testLocale.forEach((l) => { + expect(dayjs(d).locale(l).startOf('week').date()) + .toBe(moment(d).locale(l).startOf('week').date()) + expect(dayjs(d).locale(l).endOf('week').date()) + .toBe(moment(d).locale(l).endOf('week').date()) + }) }) }) })