From 1295591ee5ab6ad85ccd54195e3f649c57746201 Mon Sep 17 00:00:00 2001 From: iamkun Date: Tue, 5 Feb 2019 15:49:38 +0800 Subject: [PATCH] fix: Allow set start day of week in locale && Allow set week in weekOfYear plugin --- src/index.js | 3 +-- src/locale/zh-cn.js | 1 + test/manipulate.test.js | 5 +++++ test/plugin/weekOfYear.test.js | 1 + 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index d118279e4..f0f5fc1db 100644 --- a/src/index.js +++ b/src/index.js @@ -172,8 +172,7 @@ class Dayjs { return isStartOf ? instanceFactory(1, this.$M) : instanceFactory(0, this.$M + 1) case C.W: { - const l = this.$locale() - const weekStart = l && l.weekStart === 1 ? 1 : 0 + 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) } diff --git a/src/locale/zh-cn.js b/src/locale/zh-cn.js index f30c297e9..bc28c7edf 100644 --- a/src/locale/zh-cn.js +++ b/src/locale/zh-cn.js @@ -8,6 +8,7 @@ const locale = { months: '一月_二月_三月_四月_五月_六月_七月_八月_九月_十月_十一月_十二月'.split('_'), monthsShort: '1月_2月_3月_4月_5月_6月_7月_8月_9月_10月_11月_12月'.split('_'), ordinal: n => `${n}日`, + weekStart: 1, formats: { LT: 'HH:mm', LTS: 'HH:mm:ss', diff --git a/test/manipulate.test.js b/test/manipulate.test.js index 98663ded7..fd5cb72d1 100644 --- a/test/manipulate.test.js +++ b/test/manipulate.test.js @@ -1,6 +1,7 @@ import moment from 'moment' import MockDate from 'mockdate' import dayjs from '../src' +import '../src/locale/zh-cn' beforeEach(() => { MockDate.set(new Date()) @@ -28,6 +29,10 @@ describe('StartOf EndOf', () => { expect(dayjs().startOf('otherString').valueOf()).toBe(moment().startOf('otherString').valueOf()) expect(dayjs().endOf('otherString').valueOf()).toBe(moment().endOf('otherString').valueOf()) }) + + it('StartOf week with locale', () => { + expect(dayjs().locale('zh-cn').startOf('week').format()).toBe(moment().locale('zh-cn').startOf('week').format()) + }) }) diff --git a/test/plugin/weekOfYear.test.js b/test/plugin/weekOfYear.test.js index 82a080c6f..3736c08bb 100644 --- a/test/plugin/weekOfYear.test.js +++ b/test/plugin/weekOfYear.test.js @@ -22,4 +22,5 @@ it('Week of year', () => { expect(dayjs().weeks(week).week()).toBe(moment().weeks(week).week()) expect(dayjs().weeks(-week).week()).toBe(moment().weeks(-week).week()) expect(dayjs().weeks(55).week()).toBe(moment().weeks(55).week()) + expect(dayjs().weeks()).toBe(moment().weeks()) })