From 0e45b0ace9e1a22c679fa351d4391e306ca9b25d Mon Sep 17 00:00:00 2001 From: Yue Yang Date: Mon, 19 Aug 2019 13:38:45 +0800 Subject: [PATCH] fix: Handle locale in WeekOfYear plugin (#658) --- src/plugin/weekOfYear/index.js | 13 +++++++++++-- test/plugin/weekOfYear.test.js | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/plugin/weekOfYear/index.js b/src/plugin/weekOfYear/index.js index e2fc9b3e8..9c7ae6951 100644 --- a/src/plugin/weekOfYear/index.js +++ b/src/plugin/weekOfYear/index.js @@ -6,13 +6,22 @@ export default (o, c, d) => { if (week !== null) { return this.add((week - this.week()) * 7, 'day') } + + const weekStart = this.$locale().weekStart || 0 + // d(this) clone is for badMutable plugin const endOfYear = d(this).endOf(Y) - if (endOfYear.day() !== 6 && this.month() === 11 && (31 - this.date()) <= endOfYear.day()) { + if ( + weekStart === 0 && + endOfYear.day() !== 6 && + this.month() === 11 && + 31 - this.date() <= endOfYear.day() + ) { return 1 } + const startOfYear = d(this).startOf(Y) - const compareDay = startOfYear.subtract(startOfYear.day(), D).subtract(1, MS) + const compareDay = startOfYear.subtract(startOfYear.day() - weekStart, D).subtract(1, MS) const diffInWeek = this.diff(compareDay, W, true) return Math.ceil(diffInWeek) } diff --git a/test/plugin/weekOfYear.test.js b/test/plugin/weekOfYear.test.js index 3736c08bb..30adf6df8 100644 --- a/test/plugin/weekOfYear.test.js +++ b/test/plugin/weekOfYear.test.js @@ -2,6 +2,7 @@ import moment from 'moment' import MockDate from 'mockdate' import dayjs from '../../src' import weekOfYear from '../../src/plugin/weekOfYear' +import '../../src/locale/en-gb' dayjs.extend(weekOfYear) @@ -14,6 +15,8 @@ afterEach(() => { }) it('Week of year', () => { + dayjs.locale('en') + const day = '2018-12-31T10:59:09+08:00' const week = 27 expect(dayjs(day).week()).toBe(moment(day).week()) @@ -24,3 +27,15 @@ it('Week of year', () => { expect(dayjs().weeks(55).week()).toBe(moment().weeks(55).week()) expect(dayjs().weeks()).toBe(moment().weeks()) }) + +it('Week of year with locale', () => { + dayjs.locale('en-gb') + moment.locale('en-gb') + + const day = '2019-07-28' + expect(dayjs(day).week()).toBe(moment(day).week()) + + // Edges + expect(dayjs('2018-12-30').week()).toBe(moment('2018-12-30').week()) + expect(dayjs('2019-12-29').week()).toBe(moment('2019-12-29').week()) +})