Skip to content

Commit 2977438

Browse files
authored
fix: Fix WeekOfYear plugin bug while using BadMutable plugin (#884)
1 parent 0606f42 commit 2977438

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/plugin/weekOfYear/index.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
import { MS, Y, D, W } from '../../constant'
22

3-
export default (o, c) => {
3+
export default (o, c, d) => {
44
const proto = c.prototype
55
proto.week = function (week = null) {
66
if (week !== null) {
77
return this.add((week - this.week()) * 7, D)
88
}
99
const yearStart = this.$locale().yearStart || 1
1010
if (this.month() === 11 && this.date() > 25) {
11-
const nextYearStartDay = this.startOf(Y).add(1, Y).date(yearStart)
12-
const thisEndOfWeek = this.endOf(W)
11+
// d(this) is for badMutable
12+
const nextYearStartDay = d(this).startOf(Y).add(1, Y).date(yearStart)
13+
const thisEndOfWeek = d(this).endOf(W)
1314
if (nextYearStartDay.isBefore(thisEndOfWeek)) {
1415
return 1
1516
}
1617
}
17-
const yearStartDay = this.startOf(Y).date(yearStart)
18+
const yearStartDay = d(this).startOf(Y).date(yearStart)
1819
const yearStartWeek = yearStartDay.startOf(W).subtract(1, MS)
1920
const diffInWeek = this.diff(yearStartWeek, W, true)
2021
if (diffInWeek < 0) {
21-
return this.startOf('week').week()
22+
return d(this).startOf('week').week()
2223
}
2324
return Math.ceil(diffInWeek)
2425
}

test/plugin/badMutable.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import MockDate from 'mockdate'
22
import moment from 'moment'
33
import dayjs from '../../src'
44
import badMutable from '../../src/plugin/badMutable'
5+
import weekOfYear from '../../src/plugin/weekOfYear'
56
import '../../src/locale/zh-cn'
67

78
dayjs.extend(badMutable)
9+
dayjs.extend(weekOfYear)
810

911
beforeEach(() => {
1012
MockDate.set(new Date())
@@ -175,3 +177,10 @@ it('isAfter isBefore isSame', () => {
175177
expect(d.format()).toBe(format)
176178
expect(d.isAfter()).toBe(false)
177179
})
180+
181+
it('WeekOfYear get week won"t change instance', () => {
182+
const d = dayjs()
183+
const format = d.format()
184+
d.week()
185+
expect(d.format()).toBe(format)
186+
})

0 commit comments

Comments
 (0)